无法通过PHP上传文件?

时间:2016-02-24 23:22:12

标签: php

我正在尝试通过php上传文件

Html表格

<!DOCTYPE html>
<html>
<body>

<form action="upload.php" method="post" enctype="multipart/form-data">
    Select image to upload:</br>
    <input type="file" name="fileToUpload" id="fileToUpload"></br>
    <input type="submit" value="Upload Image" name="submit">
</form>

</body>
</html> 

PHP

<!DOCTYPE html>
<html>
<body>

<form action="upload.php" method="post" enctype="multipart/form-data">
    Select image to upload:</br>
    <input type="file" name="fileToUpload" id="fileToUpload"></br>
    <input type="submit" value="Upload Image" name="submit">
</form>

</body>
</html> 
rohit@joed:/var/www/html$ cat upload.php 
<?php
$target_dir = "/home/rohit/uploads";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
    $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
    if($check !== false) {
        echo "File is an image - " . $check["mime"] . ".";
        $uploadOk = 1;
    } else {
        echo "File is not an image.";
        $uploadOk = 0;
    }
}
// Check if file already exists
if (file_exists($target_file)) {
    echo "Sorry, file already exists.";
    $uploadOk = 0;
}
// Check file size
if ($_FILES["fileToUpload"]["size"] > 500000) {
    echo "Sorry, your file is too large.";
    $uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" && $imageFileType != "gif" ) {
    echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
    $uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
    echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
    if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
        echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
    } else {
        echo "Sorry, there was an error uploading your file.";
    }
}
?> 

Apache日志

move_uploaded_file failed to open stream: Permission denied in /var/www/html/upload.php 
第37行

,引用者:http://localhost/filetest.htm

我已经尝试了here所提到的所有内容,但仍然面临着这个问题。

以下是文件夹的权限

drwxr-xr-x  2 www-data rohit   4096 Feb 24 15:58 tmp_uploads
drwxr-xr-x  2 www-data rohit   4096 Feb 24 15:05 uploads

我从

获得了所有者
<?php echo exec('whoami'); ?>

chown user destination_dir
chmod 755 destination_dir

然后更改该用户的文件夹所有者。

有人可以帮我解锁吗?

3 个答案:

答案 0 :(得分:3)

看起来问题在于你如何构建$ target_file:

$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);

你需要放一个&#39; /&#39;在$ target_dir和上传文件的基本名称之间,如:

$target_file = $target_dir . '/'. basename($_FILES["fileToUpload"]["name"]);

或者只需将$ target_dir设置为(请注意结束&#39; /&#39;):

$target_dir = "/home/rohit/uploads/"

没有它,它正试图在&#39; / home / rohit&#39;中移动文件。哪个www-data用户可能没有相关权限。

答案 1 :(得分:0)

这看起来可能过于简单,但如果您获得权限被拒绝,您是否尝试过将权限设置为0777并进行测试。如果有效,请尝试0775(而不是0755)。如果可行,则该组会导致问题。如果是这种情况,请尝试切换所有者和组。

chown rohit:www-data tmp_uploads
chown rohit:www-data uploads

答案 2 :(得分:0)

根据特定的操作系统/软件包配置,Apache无法在文档根目录外编写。

顺便说一句,在大多数情况下,问题可能只在目录树中:目标路径的每个父目录应该设置执行位(x) Apache用户(或其用户组,或所有用户)。 执行位,而不是读/写权限(显然,写入目录除外)。

因此,在您的具体情况下,检查执行位是否设置为

  • /家
  • /家庭/罗希特夏尔
  • /家庭/罗希特夏尔/上传

让我知道它是否有效。对我来说,它适用于大多数情况。