Simple sample code for uploading images and deleting pictures in PHP

Source: Internet
Author: User
Tags php write
  1. if (!empty ($_files["img" ["name"])) {//Extract the File field content name and determine
  2. $path = "uppic/"; Upload path
  3. if (!file_exists ($path))
  4. {
  5. Check if there is a folder, if not created, and give the highest permissions
  6. mkdir ("$path", 0700);
  7. }//end IF
  8. Allow upload of file formats
  9. $TP = Array ("Image/gif", "Image/pjpeg", "image/jpeg");
  10. Check if the upload file is in a type that allows uploading
  11. if (!in_array ($_files["img" ["type"], $TP))
  12. {
  13. echo "";
  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"); Get time and assign a value to a variable
  32. $file 2 = $path. $today. $type; The full path of the picture
  33. $img = $today. $type; Picture name
  34. $flag = 1;
  35. }//end IF
  36. if ($flag) $result =move_uploaded_file ($_files["img" ["Tmp_name"], $file 2);
  37. Note that the first parameter passed to Move_uploaded_file here is a temporary file uploaded to the server
  38. }//end IF
  39. The value of $img is then written to the corresponding field in the database
Copy Code

2,php Delete a picture:

    1. Unlink ("uppic/". $img); Unlink method to delete a file
Copy Code

>>> you may be interested in the article: PHP Delete record while deleting the image file code php delete uploaded pictures and folders (instance sharing) PHP Delete A small example of all files created n minutes ago php Delete directory and all Files Method Example php delete directory n days before all file code a case of PHP delete record simultaneously refresh the current page implementation code Delete all files in the specified folder PHP code PHP upload and delete a picture of a simple example PHP write a delete directory PHP recursive delete directory and file custom function rrmdirphp Recursive Delete directory example by recursively creating and deleting folder code

  • Related Article

    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.