Answer the question
In order to leave comments, you need to log in
File upload to server in PHP
Hello.
I'm not very strong in PHP, but after scrolling through a few pages of Google, I realized that my situation is not typical.
I need to upload a file to the server using a script. To do this, I found a very simple html file on the Internet:
<html>
<body>
<form action="upload.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>
<?php
$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<br />";
print_r($_FILES);
}
?>
Array ( [file] => Array ( [name] => avatar-95x95.jpg ) )
Answer the question
In order to leave comments, you need to log in
And, everything is clear , you need to change the version of PHP. You just have buggy 5.3.8 installed
I ran your example. everything is working.
The only thing I had to change:
1. increase the size limit
$_FILES["file"]["size"] < 20000
(19Kb is already very little) $extension = strtolower(end(explode(".", $_FILES["file"]["name"])));
(the photo that came to hand had the extension .JPG, not .jpg)
Have a look at phpinfo() to see if you are looking at the correct php.ini and if the new settings are loaded.
1. Is the file exactly less than 2M?
2. Does the web server have write access to /tmp?
Never, ever trust $_FILES["file"]["type"] as it means absolutely nothing. This data comes from an HTTP request, which is easy to fake. Check the MIME type of the file when it is already local (php-libs or even `file - ib image.jpg`.
But even then, it’s not a fact that what you defined is there. Headers can also be faked, and put php inside -the code.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question