Advertisement
Akintunde102

Single File Upload With PHP

Dec 6th, 2016
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.54 KB | None | 0 0
  1. <?php
  2. /*
  3. *      Simple file Upload system with PHP.
  4. *      Created By Tech Stream
  5. *      Original Source at http://techstream.org/Web-Development/PHP/Single-File-Upload-With-PHP
  6. *      This program is free software; you can redistribute it and/or modify
  7. *      it under the terms of the GNU General Public License as published by
  8. *      the Free Software Foundation; either version 2 of the License, or
  9. *      (at your option) any later version.
  10. *      
  11. *      This program is distributed in the hope that it will be useful,
  12. *      but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. *      GNU General Public License for more details.
  15. *    
  16. */
  17.     if(isset($_FILES['image'])){
  18.         $errors= array();
  19.         $file_name = $_FILES['image']['name'];
  20.         $file_size =$_FILES['image']['size'];
  21.         $file_tmp =$_FILES['image']['tmp_name'];
  22.         $file_type=$_FILES['image']['type'];  
  23.         $file_ext=strtolower(end(explode('.',$_FILES['image']['name'])));
  24.        
  25.         $expensions= array("jpeg","jpg","png");        
  26.         if(in_array($file_ext,$expensions)=== false){
  27.             $errors[]="extension not allowed, please choose a JPEG or PNG file.";
  28.         }
  29.         if($file_size > 2097152){
  30.         $errors[]='File size must be excately 2 MB';
  31.         }              
  32.         if(empty($errors)==true){
  33.             move_uploaded_file($file_tmp,"images/".$file_name);
  34.             echo "Success";
  35.         }else{
  36.             print_r($errors);
  37.         }
  38.     }
  39. ?>
  40.  
  41. <form action="" method="POST" enctype="multipart/form-data">
  42. <input type="file" name="image" />
  43. <input type="submit"/>
  44. </form>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement