Is there a way to delete all files created by running the script. I have written a code were a lot of xml files are created. They have to be stored till the script run through all the xml files. These files aren't necessary any more when the script is finished. Is there a possibility to write a code that will delete these files in the same script?
I used unlike(filname) in my loop by saving the xml files locally. The problem is that it removed my file before I could used in the next part of my code.
foreach ($links as $lin){
unlike($filename);
}
Add on the end of script-file a code like this:
$files = glob('path/to/files/dir/*');
foreach($files as $file){
if(is_file($file)) {
unlink($file);
}
}
Add the following script
$files = glob('path/to/temp/*'); // get all file names
foreach($files as $file){ // iterate files
if(is_file($file))
unlink($file); // delete file
}
If you want to remove 'hidden' files like .htaccess, you have to use
$files = glob('path/to/temp/{,.}*', GLOB_BRACE);
Related
I want to delete a folder after deleting all the files inside it, in my codeigniter project. Lets say my folder name is upload, which is located near the application folder in my ci project.
The upload folder contains Peniyal as a sub-folder and it contains 4 images inside it. I need that 4 images to be deleted, next the sub-folder has to be deleted.upload folder should not be deleted. I am hanging my mind to do it.
So far I have tried the following:-
$files = glob('./upload/Peniyal');//to get all file names
//am not sure whether the path is correctly given..
foreach($files as $file){ // iterate files one by one
if(is_file($file))
unlink($file); // delete file
}
$path = './upload/Peniyal';
rmdir($path);
Any help will be appreciated. Thx!
The glob() function matches a pattern, but you are not providing one. So if you use the pattern *.* the glob will find all files in that folder.
// match any file
$files = glob('./upload/Peniyal/*.*');
foreach($files as $file){
if(is_file($file))
unlink($file);
}
$path = './upload/Peniyal';
rmdir($path);
With CodeIgnitor 4:
delete_files('./path/to/directory/', true);
rmdir('./path/to/directory/');
I create a zip file with php with this code:
<?php
$file1 = '/home/##ACCOUNT##/public_html/##SITE##/images/uploadfiles/backup/file1.csv';
$file2 = '/home/##ACCOUNT##/public_html/##SITE##/images/uploadfiles/backup/file2.csv';
// CREATE THE ZIP
$files = array($file1, $file2);
$zipname = '/home/##ACCOUNT##/public_html/##SITE##/images/uploadfiles/backup/backup.zip';
$zip = new ZipArchive;
$zip->open($zipname, ZipArchive::CREATE);
foreach ($files as $file) {
$zip->addFile($file);
}
$zip->close();
?>
In the created zip i get 2 files as i want it but it also contains the tree/path of those files so opening the zip you have to navigate inside those folder to get to the files is there a away to just have the 2 files in the zip file1.csv & file2.csv without the folders?
I found the anwser i wanted in another post
php creating zips without path to files inside the zip
As Workaround use $localname (second parameter) to define and control file/directory structure inside the zip eg.
<?php
$zip->addFile($file, $localname_relative_path);
?>
You can use ZipArchive::renameName method to change name of the file being added or use ZipArchive::addFromString. In the second case you need to remember that you first need to read that file into memory, so it should only be used in consideration with memory available.
Also you can use second parameter to ZipArchive::addFile as suggested in other answer.
foreach ($files as $file) {
$localName = basename($file);
$zip->addFile($file, $localname);
}
I have a folder with images.
As example:
z_1.jpg
z_2.jpg
z_3.jpg
//...
I want to delete every image with prefix z_*.jpg. How can I do that?
unlink('z_*.jpg'); ?
You need the exact filename to unlink() a file. So just use glob() to get all files which you want to grab. Loop through the returned array and delete the files, e.g.
<?php
$files = glob("z_*.jpg");
foreach($files as $file)
unlink($file);
?>
You may have heard of the glob method but that only manages to retrieve files from the directory that the file containing the method is located on - only one directory.
This is an example of the code that I am using:
<?php
foreach (glob("*.txt") as $filename)
{
$time = filemtime($filename);
$files[$time] = $filename;
}
krsort($files);
foreach ($files as $file) {
echo $file;
}
?>
What happens here is that all of the text files in the current directory are retrieved, they are then sorted by order of date modified and are then echoed out onto the page.
The problem with this is that I don't just want to retrieve text files from the one directory.
How would I change this so that I can retrieve files from multiple directories of my choice all from one page - so I can echo out all of the text files from the multiple directories onto one page rather than only echoing out the text files from one directory?
I believe I would need to store all the directories I want to glob into an array but I am not sure how to retrieve it.
Here is an example stolen from the page in my comment above
<?php
$Directory = new RecursiveDirectoryIterator('path/to/project/');
$Iterator = new RecursiveIteratorIterator($Directory);
$Regex = new RegexIterator($Iterator, '/^.+\.txt$/i', RecursiveRegexIterator::GET_MATCH);
?>
So, I understand it's pretty easy to zip a folder and its contents given that php.net and stackoverflow are full of sample codes. I am trying to zip up a folder as well. This is my code:
$zip = new ZipArchive;
$zip->open('myzip.zip', ZipArchive::CREATE);
foreach (glob("/Volumes/Data/Users/username/Desktop/Archive/some/thisFolder/*") as $file) {
$zip->addFile($file);
}
$zip->close();
My script is running in a folder, say on Desktop, and I want to zip thisFolder and its contents. The above code WORKS. But, the problem is that when I unzip the myzip.zip created, I get a structure like
Volumes>Data>Users>username>Desktop>Archive>some>thisFolder
and then I find the contents inside the 8th folder (thisFolder) in this case. How can I change this code so that when I unzip myzip.zip,I would straightaway see the folder thisFolder with the contents inside it instead of having to navigate through folders 7 times before getting my content?
I tried to change the path and silly things like that. But, it doesn't work.
Thanks
If you want everything in the file to be a name relative to your starting folder, use chdir() to start from there:
chdir("/Volumes/Data/Users/username/Desktop/Archive/some/thisFolder");
foreach (glob("*") as $file) {
$zip->addFile($file);
}
Actually, I don't think this will work when $file is a subdirectory -- addFile doesn't recurse automatically. There's a comment in the documentation that shows how to write a recursive zip function:
http://www.php.net/manual/en/ziparchive.addemptydir.php
bool ZipArchive::addFile ( string $filename [, string $localname ] )
filename
The path to the file to add.
localname
local name inside ZIP archive.
You can use the pathinfo function to each $file in your loop, in order to retrieve the filename without the path, and then, use it as second parameter to the addFile method.
pathinfo doc here
Here is an example that could solve your problem:
// Create your zip archive
$zip = new ZipArchive;
// open it in creation mode
$zip->open('myzip.zip', ZipArchive::CREATE);
// Loop on all your file
$mask = "/Volumes/Data/Users/username/Desktop/Archive/some/thisFolder";
$allAvailableFiles = glob($mask . "/*")
foreach ($allAvailableFiles as $file)
{
// Retrieve pathinfo
$info = pathinfo($file);
// Generate the internal directory
$internalDir = str_replace($mask, "", $info['dirname']);
// Add the file with internal directory
$zip->addFile($file, $internalDir . "/" . $info['basename']);
}
$zip->close();