Hey guys, I need a little help on my file uploading php/html script.
I want users to be able to upload any image file up to 2MB but when I try my code, it just downloads the php file to my downloads folder.
Here's my html form:
Here's my php script:
What am I doing wrong...?
I want users to be able to upload any image file up to 2MB but when I try my code, it just downloads the php file to my downloads folder.
Here's my html form:
<html>
<body>
<form action="upload_file.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>
Here's my php script:
<?php
$allowedExts = array("gif", "jpeg", "jpg", "png");
$extension = end(explode(".", $_FILES["file"]["name"]));
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/jpg")
|| ($_FILES["file"]["type"] == "image/pjpeg")
|| ($_FILES["file"]["type"] == "image/x-png")
|| ($_FILES["file"]["type"] == "image/png"))
&& ($_FILES["file"]["size"] < 200000)
&& 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"] / 200000) . " 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";
}
?>
What am I doing wrong...?
11
got it all working now: minesnap.hopto.org
thanks guys
thanks guys
better pay a professional web hosting company.
its about $60 a year with domain included.
its about $60 a year with domain included.
Sorry for these nooby questions ahaha I am relatively new to web design, and being on linux it is usually hard for answers
For development I recommend XAMPP: https://www.apachefriends.org/index.html
It has everything you need including apache and MySQL
It has everything you need including apache and MySQL
Starting XAMPP for Linux 1.8.3-4...
XAMPP: Starting Apache...fail.
XAMPP: Another web server is already running.
XAMPP: Starting MySQL...already running.
XAMPP: Starting ProFTPD...fail.
XAMPP: Another FTP daemon is already running.
XAMPP: Starting Apache...fail.
XAMPP: Another web server is already running.
XAMPP: Starting MySQL...already running.
XAMPP: Starting ProFTPD...fail.
XAMPP: Another FTP daemon is already running.
Switch the ports the server is running on. I forgot how but look around in options until you find something like Web Server Port. Change it to something like 4321 (Just make the port different.)
But you may already have one running:
Try going to your browser and typing is localhost:8080 as the URL.
Then try all of these:
localhost:80
localhost:8081
localhost:8180
localhost:81
Thats just me guessing at the port. So just try those.
If those don't work just install XAMPP and be sure you change the port.
But you may already have one running:
Try going to your browser and typing is localhost:8080 as the URL.
Then try all of these:
localhost:80
localhost:8081
localhost:8180
localhost:81
Thats just me guessing at the port. So just try those.
If those don't work just install XAMPP and be sure you change the port.
Thanks!
1. Since php is run serverside you need to run a server software like apache or nginx for the php to actually work
2. The way you're checking the files is insecure what happens if I upload an image named image.php.jpg?
2. The way you're checking the files is insecure what happens if I upload an image named image.php.jpg?
Well it looks like its checking the MIME type. Its still not the most secure.
It looks like its the code from: http://www.w3schools.com/php/php_file_upload.asp
It looks like its the code from: http://www.w3schools.com/php/php_file_upload.asp
Wait, what do I do with the server software when it is running?
1. Ah okay thank you! I will look into it
2.Yes I know I am just trying to get the script working first aha
2.Yes I know I am just trying to get the script working first aha
