Home  >  Article  >  Backend Development  >  PHP code to delete records and delete image files at the same time

PHP code to delete records and delete image files at the same time

WBOY
WBOYOriginal
2016-07-25 08:55:381345browse
  1. /**
  2. * When deleting records, picture files will also be deleted
  3. * by bbs.it-home.org
  4. */
  5. $cn = mysql_connect('127.0.0.1','root','root') or die('database connect fail');
  6. mysql_select_db ('test',$cn);
  7. mysql_query("set names 'gbk'");
  8. /*
  9. Create database tutorial
  10. CREATE DATABASE `test`;
  11. Create data table test1
  12. CREATE TABLE `test`.`test1` (
  13. `id` INT( 4 ) NOT NULL AUTO_INCREMENT ,
  14. `title` VARCHAR( 40 ) NULL DEFAULT '0',
  15. `litpic` VARCHAR( 100 ) NULL DEFAULT '0',
  16. PRIMARY KEY ( `id` )
  17. ) ENGINE = MYISAM
  18. */
  19. $sql = "Select * from test1 ";
  20. $query = mysql_query( $sql ) or die($sql);
  21. if( mysql_num_rows( $query ) )
  22. {
  23. while( $rs = mysql_fetch_array( $query ) )
  24. {
  25. $sql = "Delete from test1 where id=".$rs['id'];
  26. if( mysql_query( $sql ) )
  27. {
  28. if( defile( $rs[' litpic'] ))
  29. {
  30. echo 'Failed to delete picture file';
  31. }
  32. else
  33. {
  34. echo 'The record was deleted successfully, and the record and the corresponding picture or file were deleted';
  35. }
  36. }
  37. else
  38. {
  39. echo 'Record not saved, failed to delete data and pictures';
  40. }
  41. }
  42. }
  43. else
  44. {
  45. echo 'No record';
  46. }
  47. /*
  48. @pare return
  49. #$url string
  50. Delete file
  51. */
  52. function delfile($url)
  53. {
  54. if( file_exists($url) && is_file( $url ) )
  55. {
  56. if( unlink( $url ) )
  57. {
  58. return true;
  59. }
  60. else
  61. {
  62. return false ;
  63. }
  64. }
  65. else
  66. {
  67. return false;
  68. }
  69. }
Copy code

>>>> Articles you may be interested in: php delete uploaded pictures and folders (example sharing) Simple sample code for uploading and deleting images in PHP Small example of PHP deleting all files created N minutes ago Example of how to delete a directory and all files in php An example of php code to delete all files in a directory N days ago php implementation code for deleting records and refreshing the current page Delete the php code of all files in the specified folder Simple example of uploading and deleting images in php A function written in php to delete a directory php code for recursively creating and deleting folders php custom function rrmdir to recursively delete directories and files Example of php recursively deleting directories



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