Delete files in PHP

Source: Internet
Author: User
Delete files in PHP Include ("conn/conn. php ");
$ Id = $ _ GET [id];
$ Tupian =$ _ GET [tupian];
$ User = new shanchu ();
$ User-> delFileUnderDir ();
$ SQL = mysql_query ("delete from image where id = $ id ");

Class shanchu {
// All objects in the loop Directory
Function delFileUnderDir ($ dirName = "../admin/upimages ")
{
If ($ handle = opendir ("$ dirName ")){
While (false! ==( $ Item = readdir ($ handle ))){
If ($ item! = "." & $ Item! = ".."){
If (is_dir ("$ dirName/$ item ")){
DelFileUnderDir ("$ dirName/$ item ");
} Else {
If (unlink ("$ dirName/$ item "))
Echo "script" alert ('the information has been deleted! '); Window. location. href = 'delete. php? Type = $ type '; script ";

}
}
}
Closedir ($ handle );
}
}
}
?>

Now it is to delete files in a folder. what I want is to delete a single file.
You can also delete the database fields.


Reply to discussion (solution)

Http://www.phpthinking.com/archives/238 can be referred


Can I change it based on the code I sent?

First, you need to find all the files and list them.
For example
Delete file1
File2 deletion
Delete file3
File4 deletion
When you click delete, the file name is taken over. For example, delete

After accepting filename, you can use unlink ($ filename); to delete a single file.

Example of deleting a single file
// Display page settings
If ($ _ SESSION ["tb_forum_name"] = "") {// record login name information
Echo "script alert ('illegal login prohibited! '); Window. location. href = 'hlogin. php'; script ";
Exit;
} Else {
?>
// Query database data table information
If ($ page ){
$ Page_size = 6; // Two records are displayed on each page.
$ Query = "select count (*) as total from tb_photo where up_id"; // read data from the database
$ Result = mysql_query ($ query );
$ Message_count = mysql_result ($ result, 0, "total"); // obtain the total number of records
$ Page_count = ceil ($ message_count/$ page_size); // obtain the total number of pages
$ Offset = ($ page-1) * $ page_size;
$ Query = mysql_query ("select * from tb_photo where up_id order by up_id desc limit $ offset, $ page_size ");
While ($ myrow = mysql_fetch_array ($ query )){
?>
// Other information about image settings (omitted)
"Width =" 40 "height =" 40 "/>
// Click the delete button to delete the data according to the deletion page sent by the ID number it clicked.
& Picpath = "Class =" STYLE1 "> delete image information
// Receive and select the ID to delete the data in the corresponding database
$ Delete_id = $ _ GET [delete_id]; // receives the ID number to be deleted.
$ Picpath = '../'. $ _ GET [picpath]; // delete the image saved in the directory and in the database
$ Former = substr (strrchr ($ picpath, '/'), 1); // you can specify the path of the saved image.
If (file_exists ($ picpath )){
Unlink ($ picpath); // query the path information stored in the processed image
} // CodeGo.net/
$ Query = mysql_query ("delete from tb_photo where up_id = '$ delete_id'"); // query the ID of the data to be deleted based on the database information
If ($ query = true) {// if data exists, delete it.
Echo "script" alert ('deleted successfully! '); History. back (); script ";
} Else {
Echo "script alert ('deletion failed! '); History. back (); script ";
} // Note: table information (omitted)
?>

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.