Php image upload
First of all, create a folder named "imageupload" .
Also, create a file named "index.php". Now put the "index.php" inside "imgeupload" folder
Finally, create a folder named "upload" inside "imageupload" folder.
Please see the below code for "index.php" file.
<?php if(isset($_POST['submit'])){ if(isset($_FILES["photo"]) && $_FILES["photo"]["error"] == 0){ $allowed = array("jpg" => "image/jpg", "jpeg" => "image/jpeg","png" => "image/png"); $filename = $_FILES["photo"]["name"]; $filetype = $_FILES["photo"]["type"]; $filesize = $_FILES["photo"]["size"]; $ext = pathinfo($filename, PATHINFO_EXTENSION); if(!array_key_exists($ext, $allowed)) die("Error: Please select a valid file format."); $maxsize = 2 * 1024 * 1024; if($filesize > $maxsize) die("Error: File size should be less than 2 MB."); if(in_array($filetype, $allowed)){ $uniqid=uniqid(); if(file_exists("upload/" . $uniqid."_".$filename)){ echo $uniqid."_".$filename . " already exists."; } else{ copy($_FILES["photo"]["tmp_name"], "upload/". $uniqid."_".$filename); echo "Your file was uploaded successfully."; } } else{ echo "Error in uploading image."; } } else{ echo "Error: " . $_FILES["photo"]["error"]; } } ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Image Upload</title> </head> <body> <form action="index.php" method="post" enctype="multipart/form-data"> <h2>Upload File</h2> <label for="photo">Image:</label> <input type="file" name="photo" id="photo"> <input type="submit" name="submit" value="Submit"> <p>Only .jpg, .jpeg, .png are allowed (max fie size 2 MB.)</p> </form> </body> </html>
Subscribe
Login
0 Comments