php uploading files


/ Published in: PHP
Save to your folder(s)



Copy this code and paste it in your HTML
  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


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.