Advertisement
Eather

Upload photo by php

Jun 25th, 2012
336
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.11 KB | None | 0 0
  1. //form to upload a file
  2.  
  3. <form action="" method="post" ENCTYPE="multipart/form-data">
  4. <input type="file" name="photo"/>
  5. <input type="submit" name="submit" value="Upload"/>
  6. </form>
  7.  
  8.  
  9. //PHP code
  10.  
  11. <?php
  12. if(isset($_POST['submit'])) //if submit button is clicked
  13. {
  14.  if(isset($_FILES['photo'])) //if there is any file browsed
  15.   {
  16.          //check what is file extension
  17. $s=strtolower($_FILES["photo"]["name"]); //get the file name
  18.  
  19.             $ext=end(explode('.', $s)); //explode the name from '.'; if the file name is abc.jpg, $ext will get value 'jpg'; this is to check file extension
  20.        
  21.          if($ext!='jpg' && $ext!='jpeg' && $ext!='png') //check the image type, you can add more type here
  22.           {
  23.               echo "Please choose jpg/jpeg/png file";
  24.           }
  25.          else if($_FILES["photo"]["size"]>1000000) //file maximum size 1mb
  26. {
  27.       echo "Please choose a file smaller than 1mb";
  28. }
  29. else{
  30.  
  31. $pat=$_SERVER['HTTP_HOST'].'/images/'.$_FILES["photo"]["name"];
  32.      move_uploaded_file($_FILES["photo"]["tmp_name"],$pat); //upload the file
  33.  
  34. if($pat)echo "File uploaded";
  35. }
  36.  
  37.   }
  38. }
  39.  
  40. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement