I am using the following code to display the files in descending order of date. But When I upload any file without extension its not visible because of glob, is there any way to show the hidden files?
Code:
<?php
$dir = "/opt/lampp/htdocs/jquery";
chdir($dir);
array_multisort(array_map('filemtime', ($files = glob("*.*"))), SORT_DESC, $files);
foreach($files as $filename)
{
echo "<li>".$filename."</li>";
}
?>
#bodi0 gave you the code for ONLY items with no dots, you might be looking for
...glob("*")
to get all files.
Then, you will need to remove "." and ".."
This is impossible with inclusive only glob (python), the answerers (is that a word), misunderstood your question.
/* gets all files/folders and returns folders with no "/" at the end, /*/ gets only folders and adds the "/" at the end, but for files with NO extension ie path/foo (NO DOT) it is not straightforward to separate the files from the folders with glob.
It is possible, of course, just pass this regex pattern to the glob():
glob("([^\.])");
The pattern ([^\.]) means every file name, which does not have a dot in it.
Related
I want to delete the files which are not appearing in this array. I know the name of file partially but don't know the size parameter suffixed after file name like filename-50x75.jpg, filename-100x77.jpg , filename-500x377.jpg.
I want to delete above images from a directory and don't want to delete below images.
$list_of_allowed_images=array("filename-50x50.jpg","filename-50x70.jpg","filename-90x50.jpg","filename-100x100.jpg","filename-150x150.jpg","filename-250x200.jpg","filename-300x250.jpg","filename-360x270.jpg","filename-390x250.jpg","filename-500x345.jpg","filename-768x576.jpg","filename-820x400.jpg","filename-1024x768.jpg");
I have the following snippet:
foreach(glob($base_path_del.$only_obs_img."[0-9][0-9]*x*.{jpg,gif,png}", GLOB_BRACE) as $file_to_del_now)
{
if(!in_array($file_to_del_now,$list_of_allowed_images))
{
unlink($file_to_del_now);
}
}
but I think it can be more efficient. Is another more efficient way to do this?
Here's what I recommend:
(untested code)
chdir($base_path_del);
$files = glob($only_obs_img."[0-9][0-9]*x*.{jpg,gif,png}", GLOB_BRACE);
$whitelist_regex = "/-(?:50x[57]0|90x50|100x100|150x150|250x200|300x250|360x270|390x250|500x345|768x576|820x400|1024x768)\.jpg$/i";
$removables = preg_grep($whitelist_regex, $files, PREG_GREP_INVERT);
foreach ($removables as $filename) {
unlink($filename);
}
So...
Change the current working directory so that glob() doesn't include the paths in the collection of qualifying files.
Invert preg_grep() so that files that don't match the whitelist regex requirements are retained.
Then just loop the naughty list and delete the lot.
The regex pattern boils down your whitelist array logic. The check starts at the last - in the filename, checks the dimensions, checks .jpg case-insensitively, then ensures that the filename has ended.
p.s. or array_map() if you don't want to break the functional style.
array_map('unlink', $removables);
I have 2500 images in a Folder, which has NAME word in all the images. For examples
Peter Wang B5357550.jpg
Sander Mackiney B5355624.jpg
what i need to do is read all the filenames and rename it to the following
B5357550.jpg
B5355624.jpg
So remove NAME and SURNAME from filename, is it possible in PHP to do bulk renaming ?
(All student IDs are in format of Bxxxxxxx)
Quick, simple solution:
$dir = $_SERVER['DOCUMENT_ROOT'].'/your-folder-to-files';
$files = scandir($dir);
unset($files[0],$files[1]);
foreach ($files as $oldname){
$newname = substr($oldname, -12);
rename ($dir.'/'.$oldname, $dir.'/'.$newname);
}
N.B.: You may need to change the server path to something similar to:
$dir = "/home/users/you/folder_files/";
or
$dir = "folder_files/";
If $_SERVER['DOCUMENT_ROOT'] does not work for you.
If they're all in that format, it would be simple to fix, yes. Run glob to get all the .jpg files into an array, then simply explode the filename on spaces, use a foreach loop on that array, use end to get the last section, and rename the file to that string.
I need help about file manipulation in PHP.
I have 4 file with known names and UNKNOWN extensions.
Like that:
Y923BBBB.E120506
Y924BBBB.E120606
Y925BBBB.E120706
Y926BBBB.E120806
and the file extensions changes everyday.
How i can cut or strip for every file the file extension, so that will stay only the names like that:
Y923BBBB
Y924BBBB
Y925BBBB
Y926BBBB
Anybody an idea?
Think about it the other way around: you want to extract the filename, not "delete the extension":
echo pathinfo($file, PATHINFO_FILENAME);
http://php.net/pathinfo
Use strrpos to find the last . and substr to get only the substring up to that point. To find the files and rename them, use glob and rename:
foreach(glob('*') as $f) {
if ($f == '.' || $f == '..') continue;
$stripped = substr($f, 0, strrpos($f, '.'));
rename($f, $stripped);
}
Take care that glob('*') works differently on windows and linux (compare with answer). Use DirectoryIterator instead if you want a more stable code. Also that one provides the needed functions already to process the file-extension and won't break - as in this example - when a file does not have a dot inside. And take real care with rename, using glob returns the file-name only, rename handles this as full path, you will move files to locations you might not want to move them.
foreach(new DirectoryIterator('.') as $f) {
/* #var $f splFileInfo*/
if (!$f->isFile()) continue;
($ext = strlen($f->getExtension())) && $ext++;
if (!$ext) continue;
$path = $f->getRealPath();
rename($path, substr($path, 0, -$ext));
}
Take care. You should always takes care with rename operations. Every operation related to the file-system and changing it needs more care as let's say read-only proceedings.
In short
We have a a file called clients.(unique parameter). And now we want to unlink() it, but as we don't know the file extension, how do we succeed?
Longer story
I have a cache system, where the DB query in md5() is the filename and the cache expiration date is the extension.
Example: 896794414217d16423c6904d13e3b16d.3600
But sometimes the expiration dates change. So for the ultimate solution, the file extension should be ignored.
The only way I could think of is to search the directory and match the filenames, then get the file extension.
Use a glob():
$files = glob("/path/to/clients.*");
foreach ($files as $file) {
unlink($file);
}
If you need to, you can check the filemtime() of each file returned by the glob() to sort them so that you only delete the oldest, for example.
// Example: Delete those older than 2 days:
$files = glob("./clients.*");
foreach ($files as $file) {
if (filemtime($file) < time() - (86400 * 2)) {
unlink($file);
}
}
You are correct in your guess to search the directory for a matching file name. There are multiple approaches you could take:
readdir the folder in question
glob as suggested by Micheal
You could also get the output of ls {$target_dir} | grep {$file_first_part} and then unlink the resulting string (assuming a match is found).
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 .