【问题标题】:How do I assign a chmod value of 0777 at the point it the file is uploaded (PHP)?如何在文件上传时分配 chmod 值 0777 (PHP)?
【发布时间】:2012-03-14 07:08:27
【问题描述】:

我的系统有一个对话区,可以写入和提取 .txt 文件。我显然能够通过我的 FTP 客户端设置权限,但我希望将可写功能应用于通过我的系统在 PHP 中上传的所有文件。我要补充一点,安全不是问题。

    <?php

$adminID = $_GET['adminID'];

$name = stripslashes(trim($_POST['name']));
$area = stripslashes(trim($_POST['area']));

mysql_query("INSERT INTO chat_rooms (id, name, area, adminID) VALUES ('', '$name', '$area', '$adminID')");

$image = ($_FILES['image_url']['name']);
$empty = '';


if ($image == $empty)

{
    echo 'NO IMAGE';


}else
{

 $target = "room/test/"; 
 $target = $target .basename($_FILES['image_url']['name']); 
 $target2 = basename($_FILES['image_url']['name']); 



 $image_url = ($_FILES['image_url']['name']);
 $adminarea = 'admin-index.php'; 


mysql_query("UPDATE chat_rooms set file='$image_url' WHERE name = '$name'");



 if(move_uploaded_file($_FILES['image_url']['tmp_name'], $target))
 {

 echo "";
 echo "Your room has been created using the file " . basename( $_FILES['image_url']['name']) ." <br /> <br /> Click <a href=".$adminarea."> here </a> to return to the admin area"; 
 } 
 else { 

 echo "Sorry, there was a problem uploading your file."; 
 } 


}
?>

对于我的代码的布局和一般语法,我深表歉意

【问题讨论】:

  • 如果您的文件系统支持它,chmod 是一个好的开始。如果您创建文件/目录,请参阅umask。文件是如何上传的?

标签: php chmod writable 777


【解决方案1】:

上传文件后,只需在文件上调用chmod('thefile.txt', 0777)即可。

http://php.net/manual/en/function.chmod.php

【讨论】:

  • 我还是遇到了一些麻烦,我已将我的代码段包含在原始帖子中,希望有人能够确定我应该在哪里应用 chmod。
  • @OllieJones 如果返回true,则将其放在move_uploaded_file 之后。您需要将$target 传递给chmod()
【解决方案2】:

chmod

chmod('/path/to/file.txt', 0777);

【讨论】:

    猜你喜欢
    • 2011-05-22
    • 1970-01-01
    • 2012-05-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-04
    • 2011-01-21
    相关资源
    最近更新 更多