I recently reworked the naming convention of some images on our website. When I uploaded the images with altered names, I ended up getting the images duplicated, one copy with the old naming convention and one with the new naming convention. The images numbered in the thousands and so I didn't want to manually delete them all.
So I decided that I needed to figure out a php script that would be capable of deleting the old images from the site. Luckily the old images were consistently named with either an ending of f.jpg or s.jpg. So all I had to do is find all the files with those endings and delete them. I thought it was a fairly straightforward thing, but for whatever reason the several different solutions I found listed online didn't work right. I ended up going back to some old code I had posted on Stackoverflow for a different purpose and reworked it for this. I'm posting that code as the answer to my problem in case it might be useful to anyone else.
Below is my solution to finding files matching a certain naming convention in a selected folder and its sub-folders and deleting them. To make it work for your situation. You'll want to place it above the directory that you want to delete, and you'll specify the specific folder by replacing the part where I have ./media/catalog/. You'll also want to replace the criteria I have selected, namely (substr($path, -5)=='f.jpg' || substr($path, -5)=='s.jpg'). Note that the 5 in the preceding code refers to how many letters are being matched in the criteria. If you wanted to simply match ".jpg" you would replace the 5 with a 4.
As always, when working with code that can effect a lot of files, be sure to make a backup in case the code doesn't work the way you expect it will.
<?php #stick ClearOldJpg.php above the folder you want to delete
function ClearOldJpg(){
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator("./media/catalog/"));
$files = iterator_to_array($iterator, true);
// iterate over the directory
foreach ($files as $path) {
if( is_file( $path ) && (substr($path, -5)=='f.jpg' || substr($path, -5)=='s.jpg')){
unlink($path);
echo "$path deleted<br/>";
}
}
}
$start = (float) array_sum(explode(' ',microtime()));
echo "*************** Deleting Selected Files ***************<br/>";
ClearOldJpg( );
$end = (float) array_sum(explode(' ',microtime()));
echo "<br/>------------------- Deleting selected files COMPLETED in:". sprintf("%.4f", ($end-$start))." seconds ------------------<br/>";
?>
One fun bonus of this code is that it will list the files being deleted and tell how long it took to run.
Related
If there is any mistake in my writing, please excuse me. I'm not very good at translating from Spanish to English.
I am using pluploadQueue, reading the example script (upload.php). I have tested it and it works fine.
In addition to all that, I need to record information about the files that are attached in a database.
I have tried to add the statements that save the information in the database, but it only works halfway. It generates just one entry in my database table.
I notice that in the "upload" directory it generates a 1024kb .part for the 1st file but it doesn't do anything else there. Although visually the Widget tells me that it has uploaded all the files.
I need to save as many entries as files are selected (many can be uploaded, so it has been thought).
Could someone point me to a reference? I have tried to search for something about my problem but I have not been able to find anything.
I use version 2.3.9 of plupload
Correct me if I'm wrong, but I'm understanding that the upload.php script is executed (or should be) for each file to be uploaded, right?
My logic is such that:
Insert into db basic "header" information for the first time. In the following operations it is recovered.
plupload upload routine
Insert in db file information
thus forming a master-detail relationship bettween 2 tables.
For some reason, it stays at point 2. And this part would not be running:
while (($file = readdir($dir)) !== false) {
$tmpfilePath = $targetDir . DIRECTORY_SEPARATOR . $file;
// If temp file is current file proceed to the next
if ($tmpfilePath == "{$filePath}.part") {
continue;
}
// Remove temp file if it is older than the max age and is not the current file
if (preg_match('/\.part$/', $file) && (filemtime($tmpfilePath) < time() - $maxFileAge)) {
#unlink($tmpfilePath);
}
}
closedir($dir);
}
Help me?
EDIT:
Okay. I have found a typo error. I corrected it and now it saves me for each file. What I can't figure out right now is why it has generated hundreds of .part files in the directory instead of overwriting it. And this led to hundreds of records being stored, one for each generated .part.
I came up with this questioin. My background is from the Node.js. I am not usually quite used to be in PHP. That's why I'm aksing this question to solve the current issues.
The issues is that's to says I have certain Files and Folders that contains with a specific letters and number at the beginning. As you can see given by down below with a scrrenshot.
I just learn and writing some php code that I grabbed it from the internet resources. Take a look at what's my code:
I want this to detele this all folders which contains letters "exp_" and all the files names start with this numbers "xx-xx-xx" etc.
I created delete.php. When I'd called this file via the browser, I want to achieve to delete all the files and folder which for the No. 1 case.
All these folders and files are generated in everdays. That's why I do want to clean all those data.
<?php
$path = "test";
if(!unlink($path)){
echo "File has not deleted yet.";
} else {
echo "Successfully deleted!";
}
?>
Is there any how any solution to solve this issues? I will appreciate all in advanced who are giving me idea and suggestions from you guys.
You can do something like this:
$files = new DirectoryIterator(__DIR__);
foreach ($files as $file) {
if ( ($file->isDir() and strpos($file->getFilename(),'exp_')!==false) || ($file->isFile() and $file->getFilename() == date('d-m-Y') ) ) {
unlink($file->getPathname());
}
}
Thus, all folders with names like exp_ and files with today's date will be deleted.
I have a php/mysql website where users create listings and upload multiple images (3 image sizes for each). I made the mistake of storing the images in folders as below :
images/listings/listing_id/image-name.jpg
images/listings/listing_id/thumbs/image-name.jpg
images/listings/listing_id/large/image-name.jpg
Unfortuantely now I have come across the problem where the maximum number of sub directories is 30,000 and my code breaks.
I want to now change my folder structure to the one below :
images/listings/yyyy/mm/dd/listing_id/image-name.jpg
images/listings/yyyy/mm/dd/listing_id/thumbs/image-name.jpg
images/listings/yyyy/mm/dd/listing_id/large/image-name.jpg
I decided the best way forward would be to create a php script to loop around all directories in the 'images/listings/' folder, and copy every image in to a new directory as specified above. or each 'listing_id' folder, I would need to lookup using mysql the listing_id, and get the created_date and then split the date to get the yyyy mm dd.
I'm totally lost in creating the php script, would it be possible to rename the existing directory structure without copying and deleting the old images, or would I need to copy the old images, create the new directories, move them and then delete the old folders?
So long as you don't have any listing_ids that are the same as a 4-digit year you should be able to create the new folder structure alongside the existing one.
You seem to already have a plan for how to do it, you should try actually writing some code.
Hint: rename() is the same as 'move'. Don't do "copy and delete" unless you want it to take 10x longer.
Lastly, as a rule of thumb you should try to avoid having any folder with more than about 1000 entries if possible. If there are a lot of filesystem operations that have to use the directory index for a folder that large it can drastically reduce filesystem performance.
You can use
$origin = "images/listings";
$final = "images/final";
$start = strlen($origin) + 1;
$di = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($origin, RecursiveDirectoryIterator::SKIP_DOTS));
foreach ( $di as $file ) {
if ($file->isFile()) {
$mtime = $file->getMTime();
$date = sprintf("%d/%d/%d", date("Y", $mtime), date("m", $mtime), date("d", $mtime));
$new = sprintf("%s/%s/%s", $final, $date, substr($file, $start));
$dir = dirname($new);
is_dir($dir) or mkdir($dir, null, true);
rename($file, $new);
}
}
For the moving of image,you can use the rename() Function of php.
For example
rename("../app/temp/natural.jpeg","../public/public_photo/natural.jpeg");
Above given code natural.jpeg from the folder /aap/temp/ has moved to the folder /public/public_photo/
I have two folders, in one i have the videos and in the second one the configuration files for each video(3 files per video). Now if i want to delete a video i have to delete files by hand.
I found this :
<?php
$filename = 'name.of.the.video.xml';
$term = str_replace(".xml","", $filename);
$dirPath = ("D:/test/");
foreach (glob($dirPath.$term.".*") as $removeFile)
{
unlink ($removeFile);
}
?>
A echo will return:
D:/test/name.of.the.video.jpg
D:/test/name.of.the.video.srt
D:/test/name.of.the.video.xml
Is ok and it help me a lot, but i have a problem here.
Not all files are the same ex:
Name.of.The.video.jpg
Name.Of.The.Video.xml
If i echo the folder looking for that string and is not identic with the $filename will return empty.
So, my question is, how can i make that search Case insensitive?
Thank you.
You are making use of the glob function which is case sensitive. You are using the wrong function therefore to get the list of files.
You should therefore first normalize the filenames in the directory so they all share the same case (e.g. all lowercase). Or you need to use another method to get the directory listing case-insensitive. I suggest the first, however if that is not an option, why don't you glob for all files first and then filter the list of files using preg_grep which allows to specify patterns that are case-insensitive?
Which leads me to the point that it's more practicable to use DirectoryIterator with a RegexIterator:
$filename = 'name.of.the.video.xml';
$term = basename($filename, ".xml");
$files = new DirectoryIterator($dirPath);
$filesFiltered = new RegexIterator($files, sprintf('(^%s\\..*$)i', preg_quote($term)));
foreach($filesFiltered as $file)
{
printf("delete: %s\n", $file);
unlink($file->getPathname());
}
A good example of the flexibility of the Iterators code are your changed requirements: Do that for two directories at once. You just create two DirectoryIterators and append the one to the other with an AppendIterator. Job done. The rest of the code stays the same:
...
$files = new AppendIterator();
$files->append(new DirectoryIterator($dirPath1));
$files->append(new DirectoryIterator($dirPath2));
...
Voilá. Sounds good? glob is okay for some quick jobs that need just it. For everything else with directory operations start to consider the SPL. It has much more power.
Is strcasecmp() a valid function for this? Its a case insensitive str comparison function?
Surely if you know the file name and you can echo it out, you can pass this to unlink()?
OK, whats the best solution in php to search through a bunch of files contents for a certain string and replace it with something else.
Exactly like how notepad++ does it but obviously i dont need the interface to that.
foreach (glob("path/to/files/*.txt") as $filename)
{
$file = file_get_contents($filename);
file_put_contents($filename, preg_replace("/regexhere/","replacement",$file));
}
So I recently ran into an issue in which our web host converted from PHP 5.2 to 5.3 and in the process it broke our installation of Magento. I did some individual tweaks that were suggested, but found that there were still some broken areas. I realized that most of the problems were related to an issue with the "toString" function present in Magento and the now deprecated PHP split function. Seeing this, I decided that I would try to create some code that would find and replace all the various instances of the broken functions. I managed to succeed in creating the function, but unfortunately the shot-gun approach didn't work. I still had errors afterwards. That said, I feel like the code has a lot of potential and I wanted to post what I came up with.
Please use this with caution, though. I'd recommended zipping a copy of your files so that you can restore from a backup if you have any issues.
Also, you don't necessarily want to use this as is. I'm providing the code as an example. You'll probably want to change what is replaced.
The way the code works is that it can find and replace whatever is in the folder it is put in and in the sub folders. I have it tweaked so that it will only look for files with the extension PHP, but you could change that as needed. As it searches, it will list what files it changes. To use this code save it as "ChangePHPText.php" and upload that file to wherever you need the changes to happen. You can then run it by loading the page associated with that name. For example, mywebsite.com\ChangePHPText.php.
<?php
## Function toString to invoke and split to explode
function FixPHPText( $dir = "./" ){
$d = new RecursiveDirectoryIterator( $dir );
foreach( new RecursiveIteratorIterator( $d, 1 ) as $path ){
if( is_file( $path ) && substr($path, -3)=='php' && substr($path, -17) != 'ChangePHPText.php'){
$orig_file = file_get_contents($path);
$new_file = str_replace("toString(", "invoke(",$orig_file);
$new_file = str_replace(" split(", " preg_split(",$new_file);
$new_file = str_replace("(split(", "(preg_split(",$new_file);
if($orig_file != $new_file){
file_put_contents($path, $new_file);
echo "$path updated<br/>";
}
}
}
}
echo "----------------------- PHP Text Fix START -------------------------<br/>";
$start = (float) array_sum(explode(' ',microtime()));
echo "<br/>*************** Updating PHP Files ***************<br/>";
echo "Changing all PHP containing toString to invoke and split to explode<br/>";
FixPHPText( "." );
$end = (float) array_sum(explode(' ',microtime()));
echo "<br/>------------------- PHP Text Fix COMPLETED in:". sprintf("%.4f", ($end-$start))." seconds ------------------<br/>";
?>