PHP simple sample code for uploading and deleting images-php Tutorial

Source: Internet
Author: User
PHP simple sample code for uploading and deleting images

  1. If (! Empty ($ _ FILES ["img"] ["name"]) {// extract the file domain content name and determine
  2. $ Path = "uppic/"; // Upload path
  3. If (! File_exists ($ path ))
  4. {
  5. // Check whether the folder exists. if not, create the folder and grant the highest permission.
  6. Mkdir ("$ path", 0700 );
  7. } // END IF
  8. // File format that can be uploaded
  9. $ Tp = array ("image/gif", "image/pjpeg", "image/jpeg ");
  10. // Check whether the uploaded file is of the allowed Upload type
  11. If (! In_array ($ _ FILES ["img"] ["type"], $ tp ))
  12. {
  13. Echo "script" alert ('incorrectly formatted '); history. go (-1); script ";
  14. Exit;
  15. } // END IF
  16. $ Filetype = $ _ FILES ['IMG '] ['type'];
  17. If ($ filetype = 'image/jpeg '){
  18. $ Type = '.jpg ';
  19. }
  20. If ($ filetype = 'image/jpg '){
  21. $ Type = '.jpg ';
  22. }
  23. If ($ filetype = 'image/pjpeg '){
  24. $ Type = '.jpg ';
  25. }
  26. If ($ filetype = 'image/GIF '){
  27. $ Type = '.gif ';
  28. }
  29. If ($ _ FILES ["img"] ["name"])
  30. {
  31. $ Today = date ("YmdHis"); // Obtain the time and assign it to the variable
  32. $ File2 = $ path. $ today. $ type; // the complete path of the image.
  33. $ Img = $ today. $ type; // image name
  34. $ Flag = 1;
  35. } // END IF
  36. If ($ flag) $ result = move_uploaded_file ($ _ FILES ["img"] ["tmp_name"], $ file2 );
  37. // Note: The first parameter passed to move_uploaded_file is the temporary file uploaded to the server.
  38. } // END IF
  39. // Write the $ img value to the corresponding field in the database.

2. delete images in php:

  1. Unlink ("uppic/". $ img); // unlink method to delete an object

>>> Articles you may be interested in: php deleting records and deleting image files code php deleting uploaded images and folders (instance sharing) small example of PHP deleting all files created N minutes ago php deleting directories and all files methods example of php deleting all files under the Directory code of N days ago php deleting records while refreshing the current page php code for deleting all files in a specified folder simple example for uploading and deleting images php writes a function for deleting directories php recursively creates and deletes folders php recursively deletes directories and file user-defined function rrmdirphp recursively delete a directory example

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.