I wrote a plugin that allows a user to upload a .zip file and then if that file already is in the uploads folder it will erase the contents, upload the new zip file there, and then unzip it.
The plugin works and everything, but I'm noticing that every time I upload a file, it stays in the media library, even though it's deleted from the uploads folder. So the uploads folder could be empty, but it's still showing in the media library. Here's my code to replace the file:
//first, check if there's an existing directory
if (file_exists($upload_dir . "PDF-archive")){
//if the directory exists, delete all the files inside
$file_names = glob($upload_dir . "PDF-archive/*");
foreach ($file_names as $files){
if (is_file($files)){
unlink($files);
}
}
}
else {
//if directory doesn't exist, create a new one
$pdf_archive_dir = $temp['basedir'];
$pdf_archive_dir = $pdf_archive_dir . '/PDF-archive';
wp_mkdir_p($pdf_archive_dir);
}
//directory is created and empty, now add files and unzip them
rename($uploaded_file_path, $new_file_path); //move zip files into new directory
unzip_zips($uploaded_file_name); //unzips the uploaded file
}
I'm assuming the media library saves to the database...would there be a way to make sure it gets deleted from there as well? Like I said it works but the client is going to be adding a new zip file every single day so I want to avoid having 50+ entries in the media library, even though they don't exist in the uploads folder.
Sidenote: I'm pretty new to this so if you see issues with my code, I'd appreciate the feedback. Thanks!