Linux PHP上传权限问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3787680/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP upload permission problem
提问by JB87
right guys ive ran into a problem with file permissions with the following upload form. a text file is passed to the upload/ dir by global users.
正确的人我遇到了以下上传表单的文件权限问题。全局用户将文本文件传递到上传/目录。
mysite$ ls -l
drwxrwxrwx 2 user user 4096 2010-09-24 13:07 upload
but as I am not logged in as root, the new file uploaded to the domain saved itself in the upload/ dir with limiting permissions and cannot be modified. eg.
但由于我没有以 root 身份登录,上传到域的新文件将自身保存在上传/目录中,权限有限,无法修改。例如。
upload$ ls -l
-rw-r--r-- 1 www-data www-data 3067 2010-09-24 13:07 Readme.txt
this problem is obviously the same for all files added to the upload folder by global users. once the file is uploaded I need a way of changing the file rights without embedding the root password into a php script running on the domain. please help!
对于全局用户添加到上传文件夹的所有文件,此问题显然是相同的。文件上传后,我需要一种更改文件权限的方法,而无需将 root 密码嵌入在域上运行的 php 脚本中。请帮忙!
is there any way to associate the same rights to files as the containing folder when new files are added?
添加新文件时,有什么方法可以将与包含文件夹相同的文件权限关联到文件?
submit form:
提交表格:
<html>
<body>
<form action="upload_file.php" method="post"
enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file" />
<br />
<input type="submit" name="submit" value="Submit" />
</form>
</body>
</html>
upload_file.php:
上传_file.php:
<?php
if ($_FILES["file"]["type"] == "text/plain")
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
}
else
{
echo "Upload: " . $_FILES["file"]["name"] . "<br />";
echo "Type: " . $_FILES["file"]["type"] . "<br />";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";
if (file_exists("/home/user/mysite/upload/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"/home/user/mysite/upload/" . $_FILES["file"]["name"]);
echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
}
}
}
else
{
echo "Invalid file";
}
?>
采纳答案by Saul
The user, that initially writes the files into your "/upload" directory, is the one that started the Apache instance running a PHP module.
最初将文件写入“/upload”目录的用户是启动运行 PHP 模块的 Apache 实例的用户。
In other words, PHP is the "owner" of all uploaded files and through a PHP script you can change the permissions of all relevant uploaded files without providing any credentials at all:
换句话说,PHP 是所有上传文件的“所有者”,您可以通过 PHP 脚本更改所有相关上传文件的权限,而无需提供任何凭据:
A quick and dirty hack to make uploaded files writable to all users would be
使上传的文件对所有用户都可写的快速而肮脏的黑客将是
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
$f="/home/user/mysite/upload/" . $_FILES["file"]["name"]);
chmod($f, 0777);
echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
}
回答by Jeff Rupert
回答by Ibrahim
move_uploaded_file uses umask(600). Use copy($source, $dest)
instead of move.
move_uploaded_file 使用 umask(600)。使用copy($source, $dest)
代替移动。
回答by milad
wen you start a local server in linux with xampp or other way there is very limitation. Because local host created in root directory for example xampp is in (/opt/lampp) if you want Removing all restrictions insert bottom code in terminal
你用 xampp 或其他方式在 linux 中启动本地服务器有很大的限制。因为在根目录中创建的本地主机例如 xampp 在 (/opt/lampp) 中,如果您想删除所有限制,请在终端中插入底部代码
sudo chmod 777 /opt
after that
在那之后
sudo chmod 777 /opt/*
after that
在那之后
sudo chmod 777 /opt/*/*
after that
在那之后
sudo chmod 777 /opt/*/*/*
and Continue to add (/*) until you get error
并继续添加 (/*) 直到出现错误
good luck
祝你好运