php trying to upload file: “No Such file or directory”

一世执手 提交于 2019-12-02 15:21:42

问题


I want to upload a file into an existing folder, /media/images/avatars/, but receive the following error: No such file or directory. What am I doing wrong? I'm using Ubuntu, if that matters.

Here is my code:

if (!empty($_FILES['file']['name']))
{
    $allowedExts = array("jpg", "jpeg", "gif", "png");
    $extension = end(explode(".", $_FILES["file"]["name"]));
    if ((($_FILES["file"]["type"] == "image/gif")
      || ($_FILES["file"]["type"] == "image/jpeg")
      || ($_FILES["file"]["type"] == "image/png")
      || ($_FILES["file"]["type"] == "image/pjpeg"))
      && ($_FILES["file"]["size"] < 64000)
      && in_array($extension, $allowedExts))
    {
        if ($_FILES["file"]["error"] > 0)
        {
            echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
        }
        else
        {
            if (file_exists("/media/images/avatars/" . $_FILES["file"]["name"]))
            {
                echo $_FILES["file"]["name"] . " already exists. ";
            }
            else
            {
                move_uploaded_file($_FILES["file"]["tmp_name"],
                "/media/images/avatars/" . $_FILES["file"]["name"]);
            }
        }
        $ins['avatar'] = $_FILES["file"]["name"];
    }
    else
    {
        echo "Invalid file";
    }
}

回答1:


/media/images/avatars/ is an existing folder

I am pretty sure you are wrong.
Most likely you are referring to a web-server directory, though using absolute path from the filesystem root.

So, you have to prepend your path with document root path, which you can find in the $_SERVER['DOCUMENT_ROOT'] variable on a well-configured server



来源:https://stackoverflow.com/questions/15432050/php-trying-to-upload-file-no-such-file-or-directory

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!