0

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";
    }
}
2
  • Is that an absolute or a relative path? The first thing I would do for troubleshooting is make sure that you're using an absolute path. Commented Mar 15, 2013 at 12:10
  • It's located in var/www/aaa/media/images/avatars/ Commented Mar 15, 2013 at 12:13

1 Answer 1

2

/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

Sign up to request clarification or add additional context in comments.

1 Comment

"I am pretty sure you are wrong." It turns out, I should have put media/images/avatars/. Without the first slash before media. It works now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.