Advertisement
Guest User

Untitled

a guest
Sep 17th, 2019
262
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.92 KB | None | 0 0
  1.  
  2. PHP File Upload
  3. With PHP, it is easy to upload files to the server.
  4.  
  5. However, with ease comes danger, so always be careful when allowing file uploads!
  6.  
  7. Configure The "php.ini" File
  8. First, ensure that PHP is configured to allow file uploads.
  9.  
  10. In your "php.ini" file, search for the file_uploads directive, and set it to On:
  11.  
  12. file_uploads = On
  13. Create The HTML Form
  14. Next, create an HTML form that allow users to choose the image file they want to upload:
  15.  
  16. <!DOCTYPE html>
  17. <html>
  18. <body>
  19.  
  20. <form action="upload.php" method="post" enctype="multipart/form-data">
  21. Select image to upload:
  22. <input type="file" name="fileToUpload" id="fileToUpload">
  23. <input type="submit" value="Upload Image" name="submit">
  24. </form>
  25.  
  26. </body>
  27. </html>
  28. Some rules to follow for the HTML form above:
  29.  
  30. Make sure that the form uses method="post"
  31. The form also needs the following attribute: enctype="multipart/form-data". It specifies which content-type to use when submitting the form
  32. Without the requirements above, the file upload will not work.
  33.  
  34. Other things to notice:
  35.  
  36. The type="file" attribute of the <input> tag shows the input field as a file-select control, with a "Browse" button next to the input control
  37. The form above sends data to a file called "upload.php", which we will create next.
  38.  
  39.  
  40. Create The Upload File PHP Script
  41. The "upload.php" file contains the code for uploading a file:
  42.  
  43. <?php
  44. $target_dir = "uploads/";
  45. $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
  46. $uploadOk = 1;
  47. $imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
  48. // Check if image file is a actual image or fake image
  49. if(isset($_POST["submit"])) {
  50. $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
  51. if($check !== false) {
  52. echo "File is an image - " . $check["mime"] . ".";
  53. $uploadOk = 1;
  54. } else {
  55. echo "File is not an image.";
  56. $uploadOk = 0;
  57. }
  58. }
  59. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement