简体   繁体   English

我的php程序未将上传的图片移动到目录(ubuntu)

[英]My php program is not moving uploaded image to a directory (ubuntu)

I am a beginning in Php. 我是Php的起点。

I have been trying for several hours to move an uploaded image to a directory without success. 我已经尝试了好几个小时,无法成功将上传的图像移动到目录中。 I have already read other questions about the subject, but I have not been able to solve the problem: 我已经阅读了有关该主题的其他问题,但无法解决该问题:

1) I have already checked if the directory exists (see output below); 1)我已经检查了目录是否存在(请参见下面的输出);

2) It seems that the directory is not writable, but I am not only trying to save the image in a kind of system directory, but also in a simple directory of my computer. 2)该目录似乎不可写,但是我不仅试图将图像保存在一种系统目录中,而且还试图将其保存在计算机的一个简单目录中。

3) The image seems to be uploaded correctly. 3)图像似乎已正确上传。

4) I have already used "sudo chmod -R 775 /home/daniel/NetBeansProjects/NewPhpProject/photos", but this should not be the problem, since this is not a system directory! 4)我已经使用过“ sudo chmod -R 775 / home / daniel / NetBeansProjects / NewPhpProject / photos”,但这不是问题,因为这不是系统目录!

I have no idea what is happening. 我不知道发生了什么事。

Daniel 丹尼尔

See my code below: 请参阅下面的代码:

uploadImage.php uploadImage.php

<html>
    <body>
        <h2> Upload your photo. </h2>
        <form action="moveImage.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>

movingImage.php movingImage.php

    <?php
    if ($_FILES["file"]["error"] > 0) {
        echo "Error: " . $_FILES["file"]["error"] . "<br>";
    } else {

        //$upload_dir = $_SERVER['DOCUMENT_ROOT'] . "/photos/";
        //$upload_dir = dirname(__FILE__) . "/photos/";
        $upload_dir ="/home/daniel/NetBeansProjects/NewPhpProject". "/photos/";
        echo "upload_dir: " . $upload_dir . "<br>";
        if (file_exists($upload_dir)) {
            if (is_writable($upload_dir)) {
                $target = $upload_dir; //"dirname(__FILE__)" . "photos/";
                $target = $target . basename($_FILES['file']['name']);
                $moved = move_uploaded_file($_FILES['file']['name'], "$target");
            } else {
                echo 'Upload directory is not writable<br>';
            }
        } else {
            echo 'Upload directory does not exist.<br>';
        }
        echo $target . "<br>";
    //  echo dirname(__FILE__)."<br>";
    echo "Upload: " . $_FILES["file"]["name"] . "<br>";
    echo "Type: " . $_FILES["file"]["type"] . "<br>";
    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
    echo "Stored in: " . $_FILES["file"]["tmp_name"];
}
?> 

Output: 输出:

upload_dir: /home/daniel/NetBeansProjects/NewPhpProject/photos/ upload_dir:/ home / daniel / NetBeansProjects / NewPhpProject / photos /

Upload directory is not writable 上载目录不可写

Upload: FotoCaju.jpeg 上传:FotoCaju.jpeg

Type: image/jpeg 类型:图片/ jpeg

Size: 0.666015625 kB 大小:0.666015625 kB

Stored in: /tmp/phpmTJWMi 存储在:/ tmp / phpmTJWMi

You need to change the directory permissions for /home/daniel/NetBeansProjects/NewPhpProject/photos/ . 您需要更改/home/daniel/NetBeansProjects/NewPhpProject/photos/的目录权限。 For this, you will use the chmod command. 为此,您将使用chmod命令。

$: chmod +w /home/daniel/NetBeansProjects/NewPhpProject/photos/ $: chmod +w /home/daniel/NetBeansProjects/NewPhpProject/photos/

PHP.net site comment is_writable: PHP.net网站评论is_writable:

Note: The results of this function are cached. See clearstatcache() for more details.

Try it. 试试吧。 Maybe the result is cached with the result before you have changed the permissions of the folder. 可能在更改文件夹的权限之前,将结果与结果一起缓存。

The problem is probably that every directory along the way to /home/daniel/NetBeansProjects/NewPhpProject/photos/ must also be writable by Apache. 问题可能出在/home/daniel/NetBeansProjects/NewPhpProject/photos/上的每个目录也必须可由Apache写入。

Since changing permissions to your home directory is generally not a good idea, you should make a symbolic link somewhere that Apache has access to that is outside of public_html (like /var/www ) that points to your target directory, which should have the correct permissions for the Apache user. 由于更改主目录的权限通常不是一个好主意,因此您应该在Apache可以访问的某个位置进行符号链接 ,该链接位于public_html (例如/var/www )之外,指向您的目标目录,该目录应具有正确的目录Apache用户的权限。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM