Posted By

iTony on 05/02/08


Tagged

php file files upload


Versions (?)

Who likes this?

9 people have marked this snippet as a favorite

vickyvamp
mrjthethird
haozi
mmccrack
wizard04
lisandro
nb109
joethermal
catrinho


php uploading files


 / Published in: PHP
 

  1. //if they DID upload a file...
  2. if($_FILES['photo']['name'])
  3. {
  4. //if no errors...
  5. if(!$_FILES['photo']['error'])
  6. {
  7. //now is the time to modify the future file name and validate the file
  8. $new_file_name = strtolower($_FILES['photo']['tmp_name']); //rename file
  9. if($_FILES['photo']['size'] > (1024000)) //can't be larger than 1 MB
  10. {
  11. $valid_file = false;
  12. $message = 'Oops! Your file\'s size is to large.';
  13. }
  14.  
  15. //if the file has passed the test
  16. if($valid_file)
  17. {
  18. //move it to where we want it to be
  19. move_uploaded_file($_FILES['photo']['tmp_name'], 'uploads/'.$new_file_name);
  20. $message = 'Congratulations! Your file was accepted.';
  21. }
  22. }
  23. //if there is an error...
  24. else
  25. {
  26. //set that to be the returned message
  27. $message = 'Ooops! Your upload triggered the following error: '.$_FILES['photo']['error'];
  28. }
  29. }
  30.  
  31. //you get the following information for each file:
  32. $_FILES['field_name']['name']
  33. $_FILES['field_name']['size']
  34. $_FILES['field_name']['type']
  35. $_FILES['field_name']['tmp_name']

Report this snippet  

You need to login to post a comment.