Im using a tutorial to upload imageas to my server only I can’t seem to get it to actually place the image in my folder on my server?
I have the following…
if (isset($_POST['submit'])) {
$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"] < 20000)
&& in_array($extension, $allowedExts))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
}
else
{
echo "Upload: " . $_FILES["file"]["name"] . "<br>";
echo "Type: " . $_FILES["file"]["type"] . "<br>";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";
if (file_exists("/upload/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"/upload/" . $_FILES["file"]["name"]);
echo "Stored in: " . "/upload/" . $_FILES["file"]["name"];
}
}
}
else
{
echo "Invalid file";
}
};
Can anybody see whats wrong? As ive said i receive a success message yet nothing seems to appear in the correct folder…
html
<form action="#" 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>
Array
(
[file] => Array
(
[name] => Screen shot 2012-12-16 at 8.40.49 PM.png
[type] => image/png
[tmp_name] => /tmp/phpVr7glY
[error] => 0
[size] => 6289
)
)
This looks like a path-problem to me.
Have you tried changing the path to “upload/”:
And ofcourse that the name is upload and not uploads or something like that.
Relative and absolute paths can be somewhat tricky, and to be honest I don’t really recall how for example apache/php handles absolute paths.
Also this is asuming that you’re on Linux and using apache, if you’re on windows and using ISS I recall having problems with the temp-folder. Howvever I don’t have a solution for you.
Okay so after looking at this for a while and finally testing it. It seems that calling end on the returnvalue of explode is causing all the problems. So if you change it to something like this it should work:
The error output was this:
So look over your error-reporting and try installing a debugger like xdebug.