how to exclude a certain folder when using scandir in php - php

I am using scandir to list all the files in a directory. But there should be an exception for ./, ../ and tmp folder.
I already have this to exclude the dot and double dot:
$files = preg_grep('/^([^.])/', scandir($dir));
How can i add tmp folder to it? (name of the folder is tmp)

I would choose for this solution, because of already mentioned by #duskwuff, your current code excludes all the files which start with a .
$files = array_diff( scandir($dir), array(".", "..", "tmp") );

Try :
$toRemove = array('.','..','tmp');
$cdir = scandir($dir);
$result = array_diff($cdir, $toRemove);
It's easier than preg_grep

Since it's a regex you can try to take a look at the negative lookahead:
$files = preg_grep('/^(?!tmp|\.{1,2})$/', scandir($dir));

I would have done something like that if you want to stick with regex
$files = preg_grep('/^(?!tmp|(?!([^.]))).*/', scandir($dir));

Related

PHP: How to explode string

I have a variable that stores the location of a temp file:
$file = 'C:\xampp\htdocs\temp\filename.tmp';
How can I explode all this to get filename (without the path and extension)?
Thanks.
Is not the best code but if you confident that this path will be similar and just file name will be different you can use this code:
$str = 'C:\xampp\htdocs\temp\filename.tmp';
$arrayExplode = explode("\\", $str);
$file = $arrayExplode[count($arrayExplode)-1];
$filename = explode('.', $file);
$filename = $filename[0];
echo $filename;
Advice: Watch out on the path contain "n" like the first letter after the backslash. It could destroy your array.
You should use the basename function, it's meant specifically for that.

how to eliminate the first 2 entries from a path in php

i have a variable that shows me the path to a directory like below:
$dir = uploads/sha256/folder1/subfolder1/subsubfolder1
How can i "cut off" the first 2 directories from $dir so that it becomes:
$dir = folder1/subfolder1/subsubfolder1
sample code:
$dir = "uploads/sha256/folder1/subfolder1/subsubfolder1";
$pieces = explode("/", $dir);
echo $pieces[2]; // piece2
This gives me only folder1
And i need the complete path after the sha256
so what i actually try to achieve is something like this:
echo $pieces[>2];
You can capture () everything after the first two directories and replace with that:
$dir = preg_replace('#[^/]+/[^/]+/(.*)#', '$1', $dir);
Or you can explode it, slice all elements after the first two and implode it again:
$dir = implode('/', array_slice(explode('/', $dir), 2));

Find File in directory Using php

I want Get Matched Files in php I have try Many More Time
for example ...
my directory files
1.254450_abcd.mp3
2.101215_apple.mp4
3.102545_efgf.php
i find only number like this 254450
$mypath = "/files/" ;
$find = "254450" ;
//i want get matched full name
echo "$filename" ;// get 254450_abcd.mp3
else
"file not found " ;
You can use scandir and preg_grep.
$mypath = "/files/" ;
$find = "254450" ;
$files = scandir($mypath);
$matches = preg_grep("/" . $find . "/", $files);
$matches is now an array with files matching $find
Here is a semi working example. I replaced scandir with your files in an array, just like scandir returns them.
https://3v4l.org/QullZ

PHP regular expression for directory

I need a regular expression that would take the string after the last forward slash.
For example, considering I have the following string:
C:/dir/file.txt
I need to take only the file.txt part (string).
Thank you :)
You don't need a regex.
$string = "C:/dir/file.txt";
$filetemp = explode("/",$string);
$file = end($filetemp);
Edited because I remember the latest PHP spitting errors out about chaining these types of functions.
If your strings are always paths you should consider the basename() function.
Example:
$string = 'C:/dir/file.txt';
$file = basename($string);
Otherwise, the other answers are great!
The strrpos() function finds the last occurrence of a string. You can use it to figure out where the file name starts.
$path = 'C:/dir/file.txt';
$pos = strrpos($path, '/');
$file = substr($path, $pos + 1);
echo $file;

Does glob() have negation?

I know I can do this...
glob('/dir/somewhere/*.zip');
...to get all files ending in .zip, but is there a way to return all files that are not ZIPs?
Or should I just iterate through and filter off ones with that extension?
You could always try something like this:
$all = glob('/dir/somewhere/*.*');
$zip = glob('/dir/somewhere/*.zip');
$remaining = array_diff($all, $zip);
Although, using one of the other methods Pascal mentioned might be more efficient.
A quick way would be to glob() for everything and use preg_grep() to filter out the files that you do not want.
preg_grep('#\.zip$#', glob('/dir/somewhere/*'), PREG_GREP_INVERT)
Also see Glob Patterns for File Matching in PHP
This pattern will work:
glob('/dir/somewhere/*.{?,??,[!z][!i][!p]*}', GLOB_BRACE);
which finds everything in /dir/somewhere/ ending in a dot followed by either
one character (?)
or two characters (??)
or anything not starting with the consecutive letter z,i,p ([!z][!i][!p]*)
I don't think glob can do a "not-wildcard"...
I see at least two other solutions :
use a combinaison of opendir / readdir / closedir
Or use some SPL Iterator ; To be more specific, I'm thinking about DirectoryIterator ; and maybe you can combine it with some FilterIterator ?
$dir = "/path";
if (is_dir($dir)) {
if ($d = opendir($dir)) {
while (($file = readdir($d)) !== false) {
if ( substr($file, -3, 3) != "zip" ){
echo "filename: $file \n";
}
}
closedir($d);
}
}
NB: "." and ".." not taken care of. Left for OP to complete

Categories