php - 更改PHP上传文件的权限

标签 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!";
}

最佳答案

PHP 手册 chmod http://php.net/manual/en/function.chmod.php

chmod("/somedir/somefile", 0755);

在上下文中;

$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!";
}

关于php - 更改PHP上传文件的权限,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4714825/

相关文章:

apache - 在 Chrome 中更改 IP 后尝试查看网站

php - Wamp Server 3.0.6 作为本地主机正常工作,但不能在网络上工作

php - 如何让数字在居中的 CSS 元素上左右扩展?

php - 使用 HTML/PHP 发布后的空字段?

PHP偶数或奇数脚本

Apache 无法确定 docker 容器上的服务器名

php - 如何使用PHP解析Excel文件

linux - Perl 脚本无法更改 Linux 上使用 apache 上传的文件的组

linux - 将多个域托管到同一服务器

c++ - FastCGI 清理代码在 Windows 下不起作用