Advertisement
Mr_hEx

Untitled

May 19th, 2023 (edited)
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.67 KB | None | 0 0
  1. <html>
  2. <head>
  3. <title>Simple File Upload Script in PHP</title>
  4. <link href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet">
  5. </head>
  6. <body>
  7. <div class="container">
  8. <?php
  9.  
  10. // check the file is uploaded or not
  11. if (is_uploaded_file($_FILES['attachment']['tmp_name'])) {
  12.  
  13. // Determine the file location
  14. $newname = dirname(__FILE__) . '/' .basename($_FILES['attachment']['name']);
  15.  
  16. if($_FILES['attachment']['size'] > 2097152) {
  17. $errors[]='File size must be excately 2 MB';
  18. }
  19.  
  20. // Check Allowed File Types
  21. $file_ext=strtolower(end(explode('.',$_FILES['attachment']['name'])));
  22. $extensions= array("pdf","doc","mp4","jpg");
  23. if(in_array($file_ext,$extensions)=== false){
  24. $errors[]="File extension not allowed, please choose a PDF, DOC, DOCX file.";
  25. }
  26.  
  27. if(empty($errors)==true){
  28. // Move the file from temporary location to determined location
  29. if (!(move_uploaded_file($_FILES['attachment']['tmp_name'], $newname))) {
  30. echo "<p>ERROR: A problem occurred during file upload!</p>\n";
  31. } else {
  32. echo "<p>The file has been saved as: {$newname}</p>\n";
  33. }
  34. }
  35. else{
  36. print_r($errors);
  37. }
  38. }
  39. ?>
  40. <form action="uploader.php" method="post" enctype="multipart/form-data">
  41. <div class="form-group">
  42. <label class="col-md-3 control-label">Upload a file (PDF, DOC, DOCX, MP4, JPG)</label>
  43. <div class="col-md-6">
  44. <input type="file" name="attachment" class="form-control-file" />
  45. </div>
  46. </div>
  47. <div class="form-group">
  48. <div class="col-md-9 col-md-offset-3">
  49. <button type="submit" class="btn btn-primary">Submit</button>
  50. </div>
  51. </div>
  52. </form>
  53. </div>
  54. </body>
  55. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement