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";
}
}
/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