更改PHP上传的文件的权限

Max*_*Max 13 php apache

我为我正在处理的网站创建了一个小规模的CMS,并且有一个表单可以上传要在网站上使用的图像文件.它成功上传文件,但它设置的权限不允许在浏览器中查看文件.

这是我目前上传文件的PHP代码

$typepath = $_POST['filetype'];

$target_path = "../../images/uploads/".$typepath."/";

$target_path = $target_path . basename( $_FILES['uploadedfile']['name']); 

if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
    echo "<p>The file ".  basename( $_FILES['uploadedfile']['name']). 
    " has been uploaded</p>\n<p>To the directory:  <span style=\"font-weight:bold;\">".substr($target_path, 6)."</span></p>";
} else{
    echo "There was an error uploading the file, please try again!";
}
Run Code Online (Sandbox Code Playgroud)

Mat*_*den 22

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

chmod("/somedir/somefile", 0755);
Run Code Online (Sandbox Code Playgroud)

在上下文中;

$typepath = $_POST['filetype'];

$target_path = "../../images/uploads/".$typepath."/";

$target_path = $target_path . basename( $_FILES['uploadedfile']['name']); 

if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
    chmod($target_path, 0755);
    echo "<p>The file ".  basename( $_FILES['uploadedfile']['name']). 
    " has been uploaded</p>\n<p>To the directory:  <span style=\"font-weight:bold;\">".substr($target_path, 6)."</span></p>";
} else{
    echo "There was an error uploading the file, please try again!";
}
Run Code Online (Sandbox Code Playgroud)

  • 但请注意:使用chmod 755意味着所有者可以读取,写入和执行。该组现在可以读写。“其他”(其他任何人)都可以读写。现在,我假设人们将无法进入目录,但是如果这样,他们可能会在您的文件中注入有害内容。我总是会选择chmod 750。 (2认同)