Php File Upload Script


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

General file upload script in php. Error set using SESSIONS.


Copy this code and paste it in your HTML
  1. if (!is_dir("directory")) {
  2. mkdir("directory");
  3. }
  4.  
  5. $max_size = 10000; // maximum file size, in KiloBytes
  6. $allowtype = array('pdf'); // allowed extensions
  7.  
  8. if (isset($_FILES['fileToUpload']) && strlen($_FILES['fileToUpload']['name']) > 1) {
  9.  
  10. $file = $ref_id; //Name of file when uploaded
  11.  
  12. $sepext = explode('.', strtolower($_FILES['fileToUpload']['name']));
  13. $type = end($sepext); // gets extension
  14. // Checks if the file has allowed type and size
  15. if (!in_array($type, $allowtype)) {
  16. $_SESSION['status'] = 1;
  17. echo '<script> window.location.href = "redirect.php"; </script>';
  18. }
  19. if ($_FILES['fileToUpload']['size'] > $max_size * 1000) {
  20. $_SESSION['status'] = 2;
  21. echo '<script> window.location.href="redirect.php"; </script>';
  22. }
  23. $uploadpath = 'directory/' . $file; // gets the file name
  24. if (move_uploaded_file($_FILES['fileToUpload']['tmp_name'], $uploadpath)) {
  25.  
  26. } else {
  27. $_SESSION['status'] = 3;
  28. echo '<script> window.location.href = "redirect.php"; </script>';
  29. }
  30. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.