I'm trying to create an image uploader for my web application. I've tried searching online for solutions on how to create one, but haven't had much luck finding anything of real help. I'm also not sure what words I should be using to search for the right results. I did find something on the W3Schools website, (http://www.w3schools.com/php/php_file_upload.asp), but I still can't get it to upload a file to my server. Not certain of what the issue is since i followed the instructions on the website to set it up. I also found this (JavaScript: Upload file), but it's not quite exactly what I want it to do. I still need the php script since the image is not the only thing i'm going to be sending to the server. I will also be sending other information to be stored into my database on the server, such as image name and other information associated with the image.
The php script that I'm using, which i got off the W3S website, is down below. Where I am stuck at is that once I hit the submit button my page crashes or I should say I get a Server Error back, but with no information as to what caused it. Any help would be greatly appreciated
<?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/pjepg")
|| ($_FILES["file"]["type"] == "image/x-png")
|| ($_FILES["file"]["type"] == "image/png"))
&& ($_FILES["file"]["size"] < 20000)
&& in_array($extension, $allowedExts)) {
if ($_FILES["file"]["error"] > 0) {
echo "Error: " . $_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 "Stored in: " . $_FILES["file"]["tmp_name"];
if (file_exists("public_hmtl/uploads/" . $_FILES["file"]["name"])) {
echo $_FILES["file"]["name"] . " already exists. ";
}
else {
move_uploaded_file($_FILES["file"]["tmp_name"], "public_hmtl/uploads/" . $_FILES["file"]["name"]);
echo "Stored in: " . "public_hmtl/uploads/" . $_FILES["file"]["name"];
}
}
}
else {
echo "Invalid file";
}
?>
Here's also the html for the form.
<!DOCTYPE html> <html> <head> <title>Location details</title> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> </head> <body> <div> <form enctype="multipart/form-data" method="post" action="scripts/snap.php"> <!--<p> <input id="plat" class="plat" name="plat" type="hidden" value="" /> <input id="plon" class="plon" name="plon" type="hidden" value="" /> </p>--> <div class="row"> <label for="file">Select an image to Upload</label><br /> <input type="file" name="file" id="file" /> </div> <div id="filename"></div> <div id="fileSize"></div> <div id="fileType"></div> <div class="row"> <input type="submit" name="submit" value="Submit" /> <!--<input type="button" onclick="uploadFile()" value="Upload" />--> </div> <div id="progressNumber"></div> </form> </div>