Find files with certain name but variable file extension - php

I'd like to be able to select a file by just giving it's name (without extension). For example, I might have a variable $id holding 12. I want to be able to select a file called the-id-in-the-variable, say, 12.png from a directory, but it may have any one of a number of file extensions, listed below:
.swf
.png
.gif
.jpg
There is only one occurrence of each ID. I could use a loop and file_exists(), but is there a better way?
Thanks,
James

$matches = glob("12.*");
would return an array with all the matching filenames in the current directory. glob() works much the same as wildcard matching at the shell prompt.

Take a look at glob. Unfortunately, the exact semantics of the $pattern parameter is not described in the manual. But it seems your problem can be solved using this function.

Quick question to OP here:
What is the file extension of this file: somefile.tar.gz? Is it .gz or .tar.gz? :) I ask because most would answer this question as .tar.gz...

Related

PHP PHAR having problems with filenames with multiple dots

I'm trying to extract some files out of a tar.gz file.
But the filename seems to cause problems:
xxx.some-random-number.tar.gz
When I use \PharData::isValidPharFilename('xxx.some-random-number.tar.gz', false) the function returns false. When I omit the first part (i.e. \PharData::isValidPharFilename('some-random-number.tar.gz', false) it returns true.
I can't use different filenames as they are provided from a third-party service (and I don't wanna rename them on the fly, either (tedious).
Any ideas how to solve this?
I believe the extension needs to be phar, tar or zip. I just answered a similar question here where I provided a bit more detail.

PHP - Is it possible to open and read shuffle wildcard file in specific path?

Example: I need to open shuffle file name one file between site-1.txt, site-2.txt, site-3.txt, site-more.txt in folder sites.
How can I write in PHP?
I try
$shuffle = file("../sites/site-*.txt");
But it’s not a solution to select wildcard file from a specific path as well.
Thank
file() just reads one file, and returns its contents as an array of lines. The function to find all the files that match a wildcard is glob().
$files = glob("../sites/site-*.txt");
This returns an array of filenames, and you can then use array_rand() to pick one of them.
$random_file = $files[array_rand($files)];

Php script to replace specific string in filenames from database

I need a simple php script which needs to find files on server after reading Number column from database, copy files to another directory and then replace specific strings in saved files. For example we have files
20160107-151620_03216488727-all.mp3
20160418-105509_03225545395-all.mp3
We need to replace (03216488727, 03225545395) with the strings from database. Here is my database info:
Number Policy Number Month
03216488727 123456788 2016-06
03225545395 123433339 2016-06
so after the replacement files will be
20160107-151620_123456788-all.mp3
20160418-105509_123433339-all.mp3
Please help.
Have a look on the str_replace() function.

Finding file without knowing the extension in PHP

I have a bunch of uniquely named images with different extensions, if I have one of the unique names, but I don't know the extension (it's an image extension), how can I find the image extension as fast as possible? I've seen other people doing this by searching all possible file extensions on that file name, but it seems too slow to try and load 6 different possible combinations before bringing up the original image.
Does anyone know an easier way?
You could use glob for this. Might not be the best solution but it is simple;
The glob() function searches for all the pathnames matching pattern
according to the rules used by the libc glob() function, which is
similar to the rules used by common shells.
$files = glob('filenamewithoutextension.*');
if (sizeof($files) > 0) {
$file = $files[0]; // Might be more than one hit however we are only interested in the first one?
}
After getting the filename you can use pathinfo to get the specific extension.
$extension = pathinfo($file, PATHINFO_EXTENSION);

Don't get the glob function in PHP

http://php.net/glob
The documentation page on glob() has this example:
<?php
foreach (glob("*.txt") as $filename) {
echo "$filename size " . filesize($filename) . "\n";
}
?>
But to be honest, I don't understand how this can work.
The array produced by glob("*.txt") will be traversed, but where does this array come from? Is glob() reading a directory? I don't see that anywhere in the code. Glob() looks for all matches to *.txt
But where do you set where the glob() function should look for these strings?
Without any directory specified glob() would act on the current working directory (often the same directory as the script, but not always).
To make it more useful, use a full path such as glob("/var/log/*.log"). Admittedly the PHP documentation doesn't make the behaviour clear, but glob() is a C library function, which is where it originates from.
Something useful I've discovered with glob(), if you want to traverse a directory, for example, for images, but want to match more than one file extension, examine this code.
$images = glob($imagesDir . '*' . '.{jpg,jpeg,png,gif}', GLOB_BRACE);
The GLOB_BRACE flag makes the braces sort of work like the (a|b) regex.
One small caveat is that you need to list them out, so you can't use regex syntax such as jpe?g to match jpg or jpeg.
Yes, glob reads the directory. Therefore, if you are looking to match files in a specific directory, then the argument you supply to glob() should be specific enough to point out the directory (ie "/my/dir/*.png"). Otherwise, I believe that it will search for files in the 'current' directory.
Note that on some systems filenames can be case-sensitive so "*.png" may not find files ending in ".PNG".
A general overview of its purpose can be found here. Its functionality in PHP is based on that of the libc glob function whose rationale can be read at http://web.archive.org/web/20071219090708/http://www.isc.org/sources/devel/func/glob.txt .

Categories