Home >Backend Development >PHP Tutorial >Simple example of uploading and deleting images in php

Simple example of uploading and deleting images in php

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-07-25 09:04:05843browse
  1. /**
  2. php upload pictures
  3. link: http://bbs.it-home.org
  4. */
  5. if (!empty($_FILES["img"]["name"])) { //Extract the file domain content name and judge
  6. $path="uppic/"; //Upload path
  7. if(!file_exists($path))
  8. {
  9. //Check whether the folder exists, if not, create it and give it the highest permissions
  10. mkdir("$path" , 0700);
  11. }//END IF
  12. //File formats allowed to be uploaded
  13. $tp = array("image/gif","image/pjpeg","image/jpeg");
  14. //Check whether the uploaded file is In the types that are allowed to be uploaded
  15. if(!in_array($_FILES["img"]["type"],$tp))
  16. {
  17. echo “<script>alert('Incorrect format');history.go(- 1);</script>”;
  18. exit;
  19. }//END IF
  20. $filetype = $_FILES['img']['type'];
  21. if($filetype == 'image/jpeg'){
  22. $type = '.jpg';
  23. }
  24. if ($filetype == 'image/jpg') {
  25. $type = '.jpg';
  26. }
  27. if ($filetype == 'image/pjpeg') {
  28. $type = '.jpg';
  29. }
  30. if($filetype == 'image/gif'){
  31. $type = '.gif';
  32. }
  33. if($_FILES["img"]["name" ])
  34. {
  35. $today=date(“YmdHis”); //Get the time and assign it to the variable
  36. $file2 = $path.$today.$type; //The full path of the image
  37. $img = $today.$ type; //Picture name
  38. $flag=1;
  39. }//END IF
  40. if($flag) $result=move_uploaded_file($_FILES["img"]["tmp_name"],$file2);
  41. //Special Note that the first parameter passed to move_uploaded_file here is the temporary file uploaded to the server
  42. }//END IF
  43. //Then write the value of $img to the corresponding field in the database
  44. ?>
Copy Code

2. Delete pictures:

  1. $img = //Query the database to find the corresponding data value
  2. unlink("uppic/".$img); //Delete the file
  3. ?>
Copy code


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn