PHP上传权限问题 [英] PHP upload permission problem

查看:141
本文介绍了PHP上传权限问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正确的人使用下面的上传表单遇到了文件权限问题。一个文本文件被全球用户传递给上传/目录。

  mysite $ ls -l 
drwxrwxrwx 2个用户用户4096 2010-09-24 13:07上传

但是因为我没有以root身份登录,上传到域的新文件保存在上传/目录中,并具有限制权限,不能修改。例如。

  upload $ ls -l 
-rw-r - r-- 1 www-data www-data 3067 2010-09-24 13:07 Readme.txt

这个问题显然与所有添加的文件相同到全球用户的上传文件夹。一旦文件上传,我需要一种方法来更改文件权限,而不需要将root密码嵌入到在域中运行的php脚本。请帮助!

是否有任何方法将相同的权限关联到包含文件夹的文件添加新文件?



提交表单:

 < html> 
< body>

< form action =upload_file.phpmethod =post
enctype =multipart / form-data>
< label for =file>文件名:< / label>
< input type =filename =fileid =file/>
< br />
< input type =submitname =submitvalue =Submit/>
< / form>

< / body>
< / html>

upload_file.php:

 {
if($ _FILES [file] [error]> 0)
{
echo返回码:。 $ _FILES [file] [error]。 < br />;
}
else
{
echoUpload:。 $ _FILES [file] [name]。 < br />;
回显类型:。 $ _FILES [file] [type]。 < br />;
回声大小:。 ($ _FILES [file] [size] / 1024)。 Kb< br />;
回显临时文件:。 $ _FILES [file] [tmp_name]。 < br />;
$ b $ if(file_exists(/ home / user / mysite / upload /。$ _FILES [file] [name]))
{
echo $ _FILES [文件名] 。 已经存在。 ;


$ $ b $ move_uploaded_file($ _ FILES [file] [tmp_name],
/ home / user / mysite / upload /。 $ _FILES [ 文件] [ 名称]);
回声存储在:。 上传/。 $ _FILES [ 文件] [ 名称];





echoInvalid file;
}
?>


解决方案

最初将文件写入 / upload目录,是启动运行PHP模块的Apache实例的目录。换句话说,PHP是所有上传文件的所有者,通过PHP脚本,您可以更改所有相关上传文件的权限,而不必提供任何凭据:



PHP chmod函数





<$ p $ ($ _ $ FILES [file] [tmp_name],
$ f =/ home / user / mysite / upload / 。$ _FILES [file] [name]);
chmod($ f,0777);
回声存储在:。 上传/。 $ _FILES [ 文件] [ 名称];
}


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.

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!

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:

<?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";
  }
?>

解决方案

The user, that initially writes the files into your "/upload" directory, is the one that started the Apache instance running a PHP module.

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 chmod function

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"]; 
      }

这篇关于PHP上传权限问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆