I have a link that I have to repeat 50 times, for each folder, and I have 15 folders.the link that I have to repeat looks like this:
now, the jpg files are named car 1- car 50. and I would really like to be able to generate this script so that I can input the path "update/images/Cars/" the picture title (car) and the input the number of times that I need this link, and then have it spit out something that looks like this:
and then it keeps repeating, I'm assuming this can be done with a counter, but I'm not sure. Thanks!
You can do it with a for loop:
$path = "update/images/Cars/";
$title = "car";
$times = 50;
for($i = 1; $i <= $times; $i++)
echo "\n";
I used $title for the lightbox argument since you didn't specify
Use a powerful text editor. ;-)
For example, in Vim, I can use the following sequence of keystrokes to create your required text:
i
Esc
qa (start recording macro into register a)
Y (yank (= copy) whole line)
p (paste into the following line)
/ ( Return (search for opening brace)
Space (advance cursor one character so it now sits on the number)
Ctrl+a (increment the number)
q (stop recording the macro)
49#a (invoke the macro 49 times)
If you're going to add or remove images from the folder, then you might get better results using the DirectoryIterator object from the Standard PHP Library. Using it would require PHP5, but there's an old-school way of handling it, too. This snippet assumes that all of the files in the directory are the images you want to list:
$link = '%s';
$dir = new DirectoryIterator("/path/to/update/images/Cars");
foreach($dir as $file) if(!$file->isDot()) echo sprintf($link, $file, $file);
Notice that I put the information about the anchor-element into the $link variable and then used sprintf to print those anchors to the screen. If you don't have PHP5 available to you, you'd want to do it this way:
$link = '%s';
$dir = opendir("/path/to/update/images/Cars");
while(($file = readdir($dir)!==false) if($file != "." && $file != "..") echo sprintf($link, $file, $file);
closedir($dir);
These would only be necessary if you're adding more car photos into the library and don't want to update the page that produces all the links. Both of these snippets should automatically search through the directory of car images and create the links you need.
You can also alter these snippets to search through sub-directories, so you could slam out the links to the images in all 15 folders all with a little bit more code. Let me know if you want to see that code, too.
Related
I'm trying to group a bunch of files together based on RecipeID and StepID. Instead of storing all of the filenames in a table I've decided to just use glob to get the images for the requested recipe. I feel like this will be more efficient and less data handling. Keeping in mind the directory will eventually contain many thousands of images. If I'm wrong about this then the below question is not necessary lol
So let's say I have RecipeID #5 (nachos, mmmm) and it has 3 preparation steps. The naming convention I've decided on would be as such:
5_1_getchips.jpg
5_2_laycheese.jpg
5_2_laytomatos.jpg
5_2_laysalsa.jpg
5_3_bake.jpg
5_finishednachos.jpg
5_morefinishedproduct.jpg
The files may be generated by a camera, so DSC###.jpg...or the person may have actually named each picture as I have above. Multiple images can exist per step. I'm not sure how I'll handle dupe filenames, but I feel that's out of scope.
I want to get all of the "5_" images...but filter them by all the ones that DON'T have any step # (grouped in one DIV), and then get all the ones that DO have a step (grouped in their respective DIVs).
I'm thinking of something like
foreach ( glob( $IMAGES_RECIPE . $RecipeID . "-*.*") as $image)
and then using a substr to filter out the step# but I'm concerned about getting the logic right because what if the original filename already has _#_ in it for some reason. Maybe I need to have a strict naming convention that always includes _0_ if it doesn't belong to a step.
Thoughts?
Globbing through 1000s of files will never being faster than having indexed those files in a database (of whatever type) and execute a database query for them. That's what databases are meant for.
I had a similar issue with 15,000 mp3 songs.
In the Win command line dir
dir *.mp3 /b /s > mp3.bat
Used a regex search and replace in NotePad++ that converted the the file names and prefixed and appended text creating a Rename statement and Ran the mp3.bat.
Something like this might work for you in PHP:
Use regex to extract the digits using preg_replace to
Create a logic table(s) to create the words for the new file names
create the new filename with rename()
Here is some simplified and UNTESTED Example code to show what I am suggesting.
Example Logic Table:
$translation[x][y][z] = "phrase";
$translation[x][y][z] = "phrase";
$translation[x][y][z] = "phrase";
$translation[x][y][z] = "phrase";
$folder = '/home/user/public_html/recipies/';
$dir=opendir($folder);
while (false !== ($found=readdir($dir))){
if pathinfo($file,PATHINFO_EXTENSION) == '.jpg')
{
$files[]= pathinfo($file,PATHINFO_FILENAME);
}
}
foreach($files as $key=> $filename){
$digit1 = 'DSC(\d)\d\d\.jpg/',"$1", $filename);
$digit2 = 'DSC\d(\d)\d\.jpg',"$1", $filename);
$digit3 = 'DSC\d\d(\d)\.jpg',"$1", $filename);
$newName = $translation[$digit1][$digit2][$digit3]
ren($filename,$newfilename);
}
I want to create a piece of code that will remove images based on set parameters and a wildcard.
The number of images and naming will vary, though the first two parameters will remain as a constant.
// Example of images to be deleted / removed.
/images/1-50-variablename-A.jpg
/images/1-50-variablename-B.jpg
/images/1-50-variablename-C.jpg
/images/1-50-variablename-D.jpg
/images/1-50-variablename-E.jpg
Essentially I am after a loop to make this happen though I am not too sure on the best logic to make this happen.
$menuid = "1";
$imageid = "50"
$fileName = "images/".$menuid."-".$imageid."-*.jpg";
if (file_exists ($fileName)) {
unlink ($fileName);
}
You can use the php glob function (http://php.net/manual/fr/function.glob.php). Feed it with your pattern (it supports wildcards) and then iterate over the result and unlink each file.
Hope it helped
Solution presented itself in Glob form.
$menuid ="9999";
$imageid="5";
array_map('unlink', glob("../images/".$menuid."-".$imageid."-*.jpg"));
I have a directory of images on a server that customers have uploaded. I need to be able to get all files that match a certain string or item code and put them inside an array. Filenames and extensions can always vary but each file will always have an 8 digit item code in the filename. So for instance say in my directory i have:
/images/
62115465.jpg
62115465-02.jpg
62115465-07.jpg
13452766.png
56773392.jpeg
56773392-avatar.jpg
I want to be able to pull out all the files that match the 8 digit item code so:
//all images that have 62115465 in the file name would give me
62115465.jpg
62115465-02.jpg
62115465-07.jpg
//or all images that have 56773392 in the file name would give me
56773392.jpeg
56773392-avatar.jpg
and then want them in an array like so:
$all_files = array(
'62115465.jpg',
'62115465-02.jpg',
'62115465-07.jpg'
);
I tried using the glob() function as below which can match some files like the 62115465.jpg but doesnt pick up the 2 other files with the -02 and -07 tags
$files = glob('62115465.'.*');
glob('62115465*');
note the removal of the .. glob() essentially replicates doing something like dir *.txt at a command prompt.
I know this question was answered already, but to the novice/intermediate coder, or anyone new to the glob() function (AHEM ME COUGH), it's pretty unclear what's going on here. So, here's a full script that I cobbled together to do pretty much the same thing the OP asked for- search through a directory for all files beginning with the target prefix and add them to an array.:
<?php
$imgs = array();
$dir = $_SERVER['DOCUMENT_ROOT'].'/path/to/your/images';
$prefix = 'your-prefix_string-_-';
chdir($dir);
$matches = glob("$prefix*");
if(is_array($matches) && !empty($matches)){
foreach($matches as $match){
$imgs[] = $match;
}
}
?>
Can you try this,
$files = glob('62115465.*');
Try like this-
$files = glob("[^62115465]*.*);
Or like this:
$files = glob("62115465*.*);
I host a bunch of Wordpress sites, and I want to make a script that will check their version every day and if they don't match with Wordpress' latest version, it would notify me.
Right now the easiest way I found to check their versions is looking into the wp-includes/version.php file, so I made this:
grep "$wp_version =" /home/*/public_html/wp-includes/version.php > /~/wordpress/versions.txt
The line above will be put on the cron to be executed once a day.
Now I want to make a php script that will organize each line of the above script into a table, like this:
Folder (in which the wordpress site is installed) ; Version
Then, I want the script to check if the above "Version" matches with the variable $wp_latest_version that I will set. If they match, the "Version" cell will turn green and if they don't, the "Version" will turn red so me or and peers can know which sites we need to update.
I have sort of an idea of how I will do the last part, but right now I'm stuck in the "translate .txt file to table" process.
Any suggestions?
Also, should I use MySQL or not?
This is part of the output text that I want to translate into a table:
/home/alton/public_html/wp-includes/version.php:$wp_version = '3.5.2';
/home/boutaces/public_html/wp-includes/version.php:$wp_version = '3.4';
/home/byseacom/public_html/wp-includes/version.php:$wp_version = '3.5.2';
/home/capricho/public_html/wp-includes/version.php:$wp_version = '3.5.1';
/home/carlosva/public_html/wp-includes/version.php:$wp_version = '3.6.1';
/home/cerimoni/public_html/wp-includes/version.php:$wp_version = '3.5.1';
/home/cotaksis/public_html/wp-includes/version.php:$wp_version = '3.6.1';
/home/crisblog/public_html/wp-includes/version.php:$wp_version = '3.5';
/home/customup/public_html/wp-includes/version.php:$wp_version = '3.5.2';
/home/dentist/public_html/wp-includes/version.php:$wp_version = '3.4.2';
foreach($lines as $line)
{
if(preg_match('|\/home\/(.+?)\/public_html\/wp-includes\/version.php:\$wp_version = '(.+?)';|', $line, $matches))
{
$folder = $matches[1];
$version = $matches[2];
}
}
Once you have the values separated from the string, write them either into a db of your choice or into a file.
I have a huge repository of files that are ordered by numbered folders. In each folder is a file which starts with a unique number then an unknown string of characters. Given the unique number how can i open or copy this file?
for example:
I have been given the number '7656875' and nothing more.
I need to interact with a file called '\server\7656800\7656875 foobar 2x4'.
how can i achieve this using PHP?
If you know the directory name, consider using glob()
$matches = glob('./server/dir/'.$num.'*');
Then if there is only one file that should start with the number, take the first (and only) match.
Like Yacoby suggested, glob should do the trick. You can have multiple placeholders in it as well, so if you know the depth, but not the correct naming, you can do:
$matchingFiles = glob('/server/*/7656875*');
which would match
"/server/12345/7656875 foo.txt"
"/server/56789/7656875 bar.jpg"
but not
"/server/12345/subdir/7656875 foo.txt"
If you do not know the depth glob() won't help, you can use a RecursiveDirectoryIterator passing in the top most folder path, e.g.
$iterator = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator('/server'));
foreach($iterator as $fileObject) {
// assuming the filename begins with the number
if(strpos($fileObject->getFilename(), '7656875') === 0) {
// do something with the $fileObject, e.g.
copy($fileObject->getPathname(), '/somewhere/else');
echo $fileObject->openFile()->fpassthru();
}
}
* Note: code is untested but should work
DirectoryIterator return SplFileInfo objects, so you can use them to directly access the files through a high-level API.
$result = system("ls \server\" . $specialNumber . '\');
$fh = fopen($result, 'r');
If it's hidden below in sub-sub-directories of variable length, use find
echo `find . -name "*$input*"`;
Explode and trim each result, then hope you found the correct one.