This is my image upload solution:
<form action="upload.php" method="post" enctype="multipart/form-data"> Select image to upload: <input type="file" name="fileToUpload" id="fileToUpload"> <input type="submit" value="Upload Image" name="submit"> <br> By clicking the Upload button you accept that you are the owner of the file. </form> <?php $target_dir = "./i/"; $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]); $ext = pathinfo(basename($_FILES["fileToUpload"]["name"]), PATHINFO_EXTENSION); $uploadOk = 1; $FILENAME = hash_file("md5",$_FILES["fileToUpload"]["tmp_name"]).".".$ext; $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION); // Check if image file is a actual image or fake image if(isset($_POST["submit"])) { $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]); if($check !== false) { $uploadOk = 1; } else { echo "<h5>File is not an image.</h5>"; $uploadOk = 0; } // Check if file already exists if (file_exists("./i/".$FILENAME)) { echo "<h5>Sorry, file already exists.</h5>"; create_output($FILENAME); $uploadOk = 0; } // Check file size if ($_FILES["fileToUpload"]["size"] > 5000000) { echo "<h5>Sorry, your file is too large.</h5>"; $uploadOk = 0; } // Allow certain file formats if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" && $imageFileType != "gif" && $imageFileType != "svg" ) { echo "<h5>Sorry, only JPG, JPEG, SVG PNG & GIF files are allowed.</h5>"; $uploadOk = 0; } // Check if $uploadOk is set to 0 by an error if ($uploadOk == 1){ if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], "./i/".$FILENAME)) { echo "<h5>The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.</h5>"; create_output($FILENAME); } else { echo "<h5>Sorry, there was an error uploading your file.</h5>"; } } }else{ echo "<br><br><br><br>Recent uploaded images:<br>"; $files = scandir($target_dir); $count = 0; foreach($files as $result) { if( $result != "." && $result !=".."){ echo "\n<a href='http://scheinast.eu/i/$result' target='_blank'><img src='http://scheinast.eu/i/$result' style='width:304px;height:228px'></a>"; } if($count > 10){break;}++$count; } } function create_output($FILE){ echo "<a href='http://scheinast.eu/i/$FILE'>http://scheinast.eu/i/$FILE</a>"; echo "<a href='http://scheinast.eu/i/$FILE'><img src='http://scheinast.eu/i/$FILE'></img></a>"; } ?> </body> </html>
The name its stored is always an md5 hash of the file to avoid duplicate files.