简体   繁体   中英

how can i Delete a file from the server using PHP?

My web host gave me a very low space. I want to delete the image files from an entry listed on mysql database row whenever its deleted.The database structure has a post title, a category id, post id and five images directory details I use an older version of XAMPP on my windows 10 PC so that i can use both mysql and msqli queries. :) Sorry for my English . I was in a bit of haste.thank you.

In php there is the unlink()-function to delete files. So you have to fetch the filedata from the database, delete the file and then delete the row.

the function you are looking for is unlink()

now regarding the space issue.you need to scan the files then match them with database,then remove the unnecessary ones.

Scan your filenames like this:

$filenames = [];
foreach (glob(__DIR__."/Models/*") as $filename){
array_push($filenames, $filename);
}

you can now see if the files are needed by retrieving filenames from your database. I'm on mobile, couldn't give the full code but this should solve your problems.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM