Codeigniter : Delete directory after deleting all files - php

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/');

Related

Remove "Attachments" folder after PHPMailer email is sent

I've got a web form which has a unique upload folder for each user (using their PHP session_id() as the folder name) which works well. When the form is submitted (after error checking) PHPMailer is used to send the email and the attachments. This is also working well. However, after the email is sent, I would like to remove the uploads from the folder and then the folder itself (sort of a self-cleanup!) The files are removed as expected but the folder remains (albeit empty). I wonder if the folder is somehow "still in use" so doesn't get deleted or something similar? This is the code:
// Empty the contents of the upload folder
if (is_dir($dir)) { // Target directory ($dir) is set above in photos POST section
// Check for any files inside the directory
$files = glob($dir.'/*'); // Get all file names
foreach($files as $file) { // Iterate through the files
if(is_file($file)) { // Check its a file
unlink($file); // Delete the file
}
}
// Remove the upload folder
rmdir($dir); //NOT WORKING? NEEDS SOME TROUBLESHOOTING...
}
Any other ideas on why this folder is remaining?
Ben
I would guess that your folders might contain hidden files (starting with .) which the default glob pattern won't match, so try this:
$files = glob($dir . '/{,.}*'); // Get all file names including hidden ones
foreach($files as $file) { // Iterate through the files
if(is_file($file)) { // Check its a file
unlink($file); // Delete the file
}
}
Also check the return value on both unlink and rmdir so you can see exactly where it's failing.
Turns out after much testing that the problem was not actually with rmdir at all! My web form uses a Dropzone for photo uploads to a unique folder for each user using their php session_id() and this folder is supposed to be created when they add a photo to Dropzone (if it doesn’t already exist). Problem was I’d put the folder creation code outside of the actual upload script so the folder was in fact being deleted but them instantly created again when the form submitted and the page reloads! Sorry about that but thanks for all your help. :)

How can I delete all files in a folder, which matches a certain pattern?

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);
?>

How to correctly select a directory and delete it with its all sub folders and files in PHP

Please answer this question and help me!!
I have found a dozen of code snippets about deleting a folder with its all files and sub folders in PHP. But I am facing difficulties to apply them.
This is an example code :
function rrmdir($path) {
// Open the source directory to read in files
$i = new DirectoryIterator($path);
foreach($i as $f) {
if($f->isFile()) {
unlink($f->getRealPath());
} else if(!$f->isDot() && $f->isDir()) {
rrmdir($f->getRealPath());
}
}
rmdir($path);
}
I can not understand how to write the source directory. I mean, how I set the $path variable? All the files of my website is inside 'public_html' folder. Will I include 'public_html' also?
When I want to delete a folder inside 'usercontent' folder, I am writing like this :
$path = "/usercontent/somefolder"
Please answer with an example directory path.
Update: What should be the permission of the folder which will be deleted.

Delete images from a folder

I want to to destroy all images within a folder with PHP how can I do this?
foreach(glob('/www/images/*.*') as $file)
if(is_file($file))
#unlink($file);
glob() returns a list of file matching a wildcard pattern.
unlink() deletes the given file name (and returns if it was successful or not).
The # before PHP function names forces PHP to suppress function errors.
The wildcard depends on what you want to delete. *.* is for all files, while *.jpg is for jpg files. Note that glob also returns directories, so If you have a directory named images.jpg, it will return it as well, thus causing unlink to fail since it deletes files only.
is_file() ensures you only attempt to delete files.
The easiest (non-recursive) way is using glob():
$files = glob('folder/*.jpg');
foreach($files as $file) {
unlink($file);
}
$images = glob("images/*.jpg");
foreach($images as $image){
#unlink($image);
}
use unlink and glob function
for more see this link
http://php.net/manual/en/function.unlink.php
and
http://php.net/manual/en/function.glob.php

Select file(s) in a directory based upon complex filename

I have audio files in var/
This is the file name
2-3109999999-3246758493-1271129518-1271129505.6.wav
Format
2=campaign id
3109999999=caller id
3246758493=number called
1271129518=timestamp call ended
1271129505=timestamp call started
6=call id
If I were to pass just the number called which was 3246758493, how can I find all the files without defining all the other variables(such as timestamp, etc) and just the files that have that number in the filename?
You would need to loop though the folder: http://php.net/manual/en/function.readdir.php
Then for each of the files in the folder, try and match it to the file that was requested using regex I guess?
http://www.txt2re.com/index-php.php3?s=2-3109999999-3246758493-1271129518-1271129505.6.wav&8
You could also use a DirectoryIterator to scan the folder and a RegexIterator to filter the files based on a pattern.
$id = '3246758493';
$files = new RegexIterator(new DirectoryIterator('var/'),
"#^\d-\d{10}-$id-\d{10}-\d{10}\.\d\.wav$#D");
foreach ($files as $fileinfo) {
echo $fileinfo . PHP_EOL;
}

Categories