I've got a small php script that will gather all files in a directory. Futhermore, I'm cleaning through this array of names to skip over the ones I don't want:
$dirname = "./_images/border/";
$border_images = scandir($dirname);
$ignore = Array(".", "..");
foreach($border_images as $border){
if(!in_array($border, $ignore)) echo "TEST".$border;
}
This directory would contain images that I want to find. Amongst these images, there will be a thumbnail version and a full-size version of each image. I'm planning to have each image either labeled *-thumbnail or *-full to more easily sort through.
What I'm trying to find is a way to, preferably with the $ignore array, add a wildcard string that will be recognized by a check condition. For example, adding *-full in my $ignore array would make that files with this tag, anywhere in their filenames, would be ignored. I'm pretty sure the in_array wouldn't accept this. If this isn't possible, would using regular expressions be possible? If so, what would my expression be?
Thanks.
You're probably looking for php's function glob()
$files_full = glob('*-full.*');
There is a better way to do this known as glob().
Take a look at glob function.
glob — Find pathnames matching a pattern
Related
I have a folder that stores some thousands of pictures files.
I want to change the name of each file that matches the condition.
The idea is if the file name has _10 change to _ten, if has _5 change to _five.
So, xxdf23_10hy.jpg should be xxdf23_tenhy.jpg, 16_5_gt5.jpg should change to 16_five_gt5.jpg. But if file mane is gdjd_7.jpg, do nothing.
The code is working good, but it is matching the string ".." that should not be matched.
This is the part of the code:
$photoPath="../pic/";
$dir = new DirectoryIterator($photoPath);
foreach ($dir as $fileinfo) {
$filename = $fileinfo->getFilename();
if(preg_match($filename, "/_10/")){
//change te name to "_ten"
}
elseif(preg_match($filename, "/_5/")){
//change te name to "_five"
}
}
Something is not good with the way I am using the preg_match function.
But if I try it inside regex tester it works good.
What am I missing?
You've got your subject and pattern switched in the preg_match() commands. Try this:
if (preg_match("/_10/", $filename)) {
// more code
}
http://php.net/manual/en/function.preg-match.php
No need for the overhead of regex here at all. Perhaps simple glob() and str_replace() would meet your needs.
$photoPath="../pic/";
$replacements = array(
'_5' => '_five',
'_10' => '_ten'
);
foreach ($replacements as $pattern => $replace) {
$files = glob($photoPath . '*' . $pattern . '*');
foreach($files as $file) {
$old_name = $file;
$new_name = str_replace($pattern, $replace, $old_name);
rename($old_name, $new_name);
}
}
Here we don't even use regex or PHP string searching functionality to find the files we want to change. We use glob() which is basically a direct call to underlying libc glob() function and should perform significantly better and with less memory usage than the DirectoryIterator with post-filter functionality you are currently using. DirectoryIterator is probably overkill here anyway unless you are doing more complex file operations. glob() would filter your file names for you, meaning you are not doing useless regex searches against every file contained in DirectoryIterator object like you are currently doing.
The actual filepath name change is executed using basic str_replace(). You don't currently show how you are doing this, but I would imagine you would implement something similar or possibly just use preg_replace() rather than preg_match() if you desire to stick with regex approach.
I am using a PHP GET method to grab a file name that then is placed in a get_file_contents command. If it is possible, I would like to ignore letter case so that my URL's are cleaner.
For instance, example.com/file.php?n=File-Name will work but example.com/file.php?n=file-name will not work using the code below. I feel like this should be easy but I'm coming up dry. Any thoughts?
$file = $_GET['n'];
$file_content = file_get_contents($file);
Lowercase all your filenames and use:
file_get_contents(strtolower($file));
(I hope you're aware of some of the risks involved in using this.)
The Linux filesystem is case sensitive. If you want to do case insensitive matching against files that already exist on the user's machine, your only option is to obtain a directory listing and do case-insensitive comparison.
But you don't explain where the download URLs come from. If you already know the correct filenames and you want to generate prettier URLs, you can keep a list of the true pathnames and look them up when you receive a case-normalized one in a URL (you could even rename them completely, obfuscate, etc.)
So i am using this wordpress function to get the users image
the_author_meta('author_image', the_author_ID()
and it will either return something.jpg or something.png or something.gif if it finds an image otherwise it will return an integer like 2330. How would i do a preg_match or some conditional to let me know if an image is present. I was thinking of doing a preg_match to see if there is a period in the name but if someone has a better idea that would be great..
Simpler:
if (is_numeric($author_image)){
// this is presumably not a file
}
If all you want to do is check the extension of the file to see if it ends with something (ex. '.jpg', '.png', etc.) you can use the solution presented here:
startsWith() and endsWith() functions in PHP
I do not have familiarity with the library that you are using, but there really should be a better way to detect if the file is actually an image (some sort of meta data). Maybe reading the documentation will help?
EDIT: I misread the part about the function returning integers if an image is not found. The is_numeric() solution is probably enough, but I'll leave my answer up to give you options (for example, if you want to distinguish between image types).
I have a directory with sitemaps, which all end with a number.
I have an automatic sitemap generator script which needs to count all sitemaps in a folder using glob.
Currently I am stuck here.
What I need is to count all sitemap files which has a number in them, so I don't count the ones without any numbers.
For instance, in my root I have a sitemap.xml file, then I also have sitemap1.xml, sitemap2.xml, sitemap3.xml etc...
I need to use glob to only return true when the filename contains a number like "sitemap1.xml".
Is this possible?
$nr_of_sitemaps = count(glob(Sitemaps which contains numbers in their filenames));
Thanks
To glob for files ending in <digit>.xml you can use a pattern like:
*[0-9].xml
So to count the matches, the PHP might look like:
$count = count(glob('/path/to/files/*[0-9].xml'));
If you want super-fine control (moreso than glob can give) over the matching files, you could use a general pattern then use preg_grep to filter the resulting array to precisely what you want.
$count = count(
preg_grep(
'#(?:^|/)sitemap\d{1,3}\.xml$#',
glob('/path/to/files/sitemap*.xml')
)
);
See also: http://cowburn.info/2010/04/30/glob-patterns/
PHP beginner's question.
I need to keep image paths as following in the database for the admin backend.
../../../../assets/images/subfolder/myimage.jpg
However I need image paths as follows for the front-end.
assets/images/subfolder/myimage.jpg
What is the best way to change this by PHP?
I thought about substr(), but I am wondering if there is better ways.
Thanks in advance.
you should save your image path in an application variable and can access from both admin and frontend
If ../../../../ is fixed, then substr will work. If not, try something like this:
newpath=substr(strpos(path, "assets"));
It might seem like an odd choice at first but you could use ltrim. In the following example, all ../'s will be removed from the beginning of $path.
The dots in the second argument have to be escaped because PHP would treat them as a range otherwise.
$path = ltrim('../../../../assets/images/subfolder/myimage.jpg', '\\.\\./');
$path will then be:
assets/images/subfolder/myimage.jpg
I suggest this
$path = "../../../../assets/images/subfolder/myimage.jpg";
$root = "../../../../";
$root_len = strlen($root);
if(substr($path, 0, $root_len) == $root){
echo substr($path, $root_len);
} else {
//not comparable
}
In this way you have a sort of control on which directory to consider as root for your images