if(unlink('./'.date('m-d-Y').'/'.$file))
{
echo "file named $file has been deleted successfully";
}
else
{
echo "file is not deleted";
}
I have the code above to delete my files.
However, is it possible to only let it delete files that contain ".5010."
Can I do something like "%.5010.%" or something, also if its more than one file do I need to put it in a while or a foreach loop? Because now it deletes all my files.
You can use glob() to find the pathnames matching a pattern.
So your code would look something like this,
$path="./".date('m-d-Y')."/*".$file."*";
$files = glob($path); // this will return multiple files.
foreach ($files as $file) {
unlink($file);
}
glob() returns an array containing the matched files/directories, an empty array if no file matched or FALSE on error.
You can loop on file with glob and after use preg_match to select files who match a pattern
<?php
foreach(glob('path/to/dir/*.*') as $file) {
if (preg_match('*.5010.*', $file)) {
unlink($file)
}
}
Related
I would like to delete all files matching a particular extension in a specified directory and all subtree. I suppose I should be using using unlink but some help would be highly appreciated... Thank you!
you need a combination of this
Recursive File Search (PHP)
And the unlink / delete
You should be able to edit the example instead of echoing the file, to delete it
To delete specific extension files from sub directories, you can use the following function. Example:
<?php
function delete_recursively_($path,$match){
static $deleted = 0,
$dsize = 0;
$dirs = glob($path."*");
$files = glob($path.$match);
foreach($files as $file){
if(is_file($file)){
$deleted_size += filesize($file);
unlink($file);
$deleted++;
}
}
foreach($dirs as $dir){
if(is_dir($dir)){
$dir = basename($dir) . "/";
delete_recursively_($path.$dir,$match);
}
}
return "$deleted files deleted with a total size of $deleted_size bytes";
}
?>
e.g. To remove all text files you can use it as follows:
<?php echo delete_recursively_('/home/username/directory/', '.txt'); ?>
I am trying to get all images from a folder with php. It works fine. Now i wanna check if a certain image exists, and if so don't display it.
This is my basic code with works fine:
foreach (glob("Bilder/Spectrum/*.png") as $filename) {
$filenameDienst = explode("_", $filename);
echo "<a href='Dienste?d=".$filenameDienst[1]."#tabs-2'> <img class='loopimage' src='".$filename."'> </a>";
}
Now i wanna check for the image name "MB_default_Spectrum.png" and if it exists don't display it.
I have tried this:
foreach (glob("Bilder/Spectrum/*.png") as $filename) {
$filenameDienst = explode("_", $filename);
if ($filename != "MB_default_Spectrum.png") {
echo "<a href='Dienste?d=".$filenameDienst[1]."#tabs-2'> <img class='loopimage' src='".$filename."'> </a>";
}
}
But it did not work.. it is still displaying. What is wrong here? Thanks
glob returns an array of the paths matching the given pattern, not just the filenames. Looking at your code, the condition should then be:
if ($filename != "Bilder/Spectrum/MB_default_Spectrum.png")
Also, I personally prefer the following code (I find it cleaner):
$results = glob('path/to/dir/*.png');
foreach ($results as $filename)
// Skip specific file
if ($filename === 'path/to/dir/secretNuclearLaunchCodesAsImage.png')
continue;
echo $filename
}
Could someone help me with how I can check the existance of files which starts with any name like sample1.pdf, sample12.pdf in a particular folder using PHP? Below is my code for checking a single file.
<html>
<?php
if(file_exists("properties/".$owner."/".$property."/sample1.pdf") )
?>
</html>
The PHP function glob (here) will provide you an array of files that match. So you can do the following:
$files = glob("properties/{$owner}/{$property}/sample*.pdf");
This will then return an array of files within the "properties/{$owner}/{$property}/" directory that start with "sample" and have an extension of .pdf
You can then loop through the files and do what you need
//Check if there are any files and there were not any FATAL errors
if (count($files) > 0 && $files !== FALSE) {
foreach ($files as $file) {
//Do something here
}
} else {
//There were no matching files
}
Hope this helps
I'm trying to find if a file exists. I know the name of it but I do not know the extension. What could I do PHP wise so that the file exists function checks for the file without knowing it's extension?
file_exists('image_storage/ses_' . $session_user_id . 'need to put something here for the
extension' );
You could use PHP's glob function to get a list of files that match a given pattern:
$files = glob('image_storage/ses_' . $session_user_id . '.*');
if (count($files) > 0) {
// check your files with a loop
foreach ($files as $file) {
// do whatever you want; this file exists =]
}
}
You won't need to check if the file exists with glob; if it returns it in the array, it should exist.
If you are in linux you can do this..
$ret = exec("ls image_storage/ses_" . $session_user_id."*");
if(!empty($ret))
{
//file exists..
}
Please note that I want the compartment number to change.
<?php
$compartment = "1";
/* HERE I NEED SOME SCRIPT TO FIND THE EXTENSION OF THE FILE NAME $compartment AND TO SAVE THAT AS A VARIABLE NAMED 'EXTENSION'.*/
if (file_exists($compartment.$extension)) {
echo "$compartment.$extension exists!
} else {
echo "No file name exists that is called $compartment. Regardless of extension."
}
?>
<?php
$compartment = "2";
/* HERE I NEED SOME SCRIPT TO FIND THE EXTENSION OF THE FILE NAME $compartment AND TO SAVE THAT AS A VARIABLE NAMED 'EXTENSION'.*/
if (file_exists($$compartment.$extension)) {
echo "$compartment.$extension exists!
} else {
echo "No file name exists that is called $compartment. Regardless of extension."
}
?>
Thank You!
You need glob().
$compartment = "2";
$files = glob("/path/to/files/$compartment.*"); // Will find 2.txt, 2.php, 2.gif
// Process through each file in the list
// and output its extension
if (count($files) > 0)
foreach ($files as $file)
{
$info = pathinfo($file);
echo "File found: extension ".$info["extension"]."<br>";
}
else
echo "No file name exists called $compartment. Regardless of extension."
by the way, what you are doing above is crying for a loop. Don' repeat your code blocks, but wrap one of them into this:
$compartments = array(1, 3, 6, 9); // or whichever compartments
// you wish to run through
foreach ($compartments as $compartment)
{
..... insert code here .......
}
Look up:
glob — Find pathnames matching a pattern
fnmatch — Match filename against a pattern
pathinfo — Returns information about a file path