Html
1 2 3 4 5 6 7 8 9 10 11 12 |
<!DOCTYPE html> <html> <body> <form method="post" enctype="multipart/form-data"> Select image to upload: <input type="file" name="filedata"> <input type="submit" value="Upload Image" name="submit"> </form> </body> </html> |
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
<?php if(isset($_FILES["filedata"]["name"])){ // avoid undefine error $filename = $_FILES["filedata"]["name"]; $target_dir = "files/"; // set uploading file path $target_file = $target_dir . $filename; $upload = true; $tmp = explode(".",$filename); $extension = strtolower($tmp[count($tmp)-1]); // extracting file extension // Check file size if($_FILES["filedata"]["size"] > 500000){ echo "Sorry, your file is too large."; $upload = false; } if($extension == "jpg" || $extension == "png" || $extension == "jpeg" || $extension == "gif" ){ // allow only few file extension $upload = true; }else{ echo "Invalid file format."; $upload = false; } if($upload){ if (move_uploaded_file($_FILES["filedata"]["tmp_name"], $target_file)) { echo "The file ". $filename. " has been uploaded."; } else { echo "Sorry, there was an error uploading your file."; } } } ?> |
Leave a reply