I have 25 files under 1 folder, which I have included using glob in PHP
foreach (glob(PATH."*.php") as $filename) {
require_once $filename;
}
I want to exclude 1 file from this list while testing, how do I achieve that ?
The preg_grep would be difficult to change again and again. What I am trying to achieve is not to mess with the current things and add new files in the folder. I want to exclude the newly added file until it is error free and doesn't harm the current setup.
Specifically exclude the file:
foreach (glob(PATH."*.php") as $filename) {
if ($filename!='excluded_file.php') {
require_once $filename;
}
}
If you want to get really sexy:
$exclude_files=array('exclude1.php','exclude2.php');
foreach (glob(PATH."*.php") as $filename) {
if (!in_array($filename,$exclude_files)) { require_once $filename; }
}
Or just temporarily remove the problem file from the directory.
Related
Below is my attempt to delete a folder and all its content. A folder may contain zip files and folders with files.
public function deleteFolder($dir){
if(file_exists($dir)){
$it = new \RecursiveDirectoryIterator($dir, \RecursiveDirectoryIterator::SKIP_DOTS);
$files = new \RecursiveIteratorIterator($it,
\RecursiveIteratorIterator::CHILD_FIRST);
foreach($files as $file) {
if ($file->isDir()){
rmdir($file->getRealPath());
} else {
unlink($file->getRealPath());
}
}
rmdir($dir);
}
}
but it returns the following error:
rmdir(C:\Juliver\UIUX\pd-loader\loader/temp/utso-pulgada-pd-loader-5066a7e0298a):
Directory not empty in C:\Juliver\UIUX\pd-loader\loader\Patcher.php on line 95
line 95 points to rmdir($dir); line
If I check the folder utso-pulgada-pd-loader-5066a7e0298a, I see that it's already empty but it throws me the above error.
$dirname = 'C:/Users/Admin/Desktop/test';
array_map('unlink', glob("$dirname/*.*"));
rmdir($dirname);
try this, this remove all the file present in the folder, and that folder too
Directory may contain other directories so you have to use a recursive function.
function removeDir($path) {
$files = glob("$path/*");
foreach ($files as $file) {
if (is_dir($file)) {
removeDir($file);
} else {
unlink($file);
}
}
rmdir($path);
}
Now is enough to call removeDir("/my/nice/path");
If you see the directory already empty, try to check for hidden files and be sure that you have the right permissions.
I suspect you have already checked its not a file permissions issue. As your code works for me but not you, it makes me wonder if it is a to do with PHP file stat or Real Path caching.
Unlinking a file should clear stat cache for individual file automatically. However PHP bugs have previously been known to cause this issue with rmdir.
Try doing a clearstatcache after the rmdir statement in your foreach block.
Previously I've used glob (mentioned in other answers) so I've no idea how RecursiveDirectoryIterator works re file handles; as a long shot try destroying these objects ( unset($files); unset($it) ) before your final rmdir.
Please answer this question and help me!!
I have found a dozen of code snippets about deleting a folder with its all files and sub folders in PHP. But I am facing difficulties to apply them.
This is an example code :
function rrmdir($path) {
// Open the source directory to read in files
$i = new DirectoryIterator($path);
foreach($i as $f) {
if($f->isFile()) {
unlink($f->getRealPath());
} else if(!$f->isDot() && $f->isDir()) {
rrmdir($f->getRealPath());
}
}
rmdir($path);
}
I can not understand how to write the source directory. I mean, how I set the $path variable? All the files of my website is inside 'public_html' folder. Will I include 'public_html' also?
When I want to delete a folder inside 'usercontent' folder, I am writing like this :
$path = "/usercontent/somefolder"
Please answer with an example directory path.
Update: What should be the permission of the folder which will be deleted.
Instead of listing all the files and directories
foreach(glob("/*") as $file) {
...
}
Is there a way to filter *.txt files and directories? Because with this example
foreach(glob("/*.txt") as $file) {
...
}
Will only get the *.txt files, but not the directories. But to list only directories, this will work:
foreach(glob("/*", GLOB_ONLYDIR) as $file) {
...
}
but will left out the *.txt files. I am trying to avoid using preg_match() or similar operations post reading the directory because it will waste server resources if there are around 5000 unnecessary files (image files with *.jpg, *.png).
Thank you.
Also tried GLOB_BRACE, but it didn't work, so had to use the following code:
$aDir1 = glob('/*.txt'); // current directory
$aDir2 = glob('/*/*.txt'); // subdirectory, one level below
$aDir = asort(array_merge($aDir1, $aDir2));
Thank you.
I'm doing approximately like how this Q&A says:
$filenames = glob("../webform/components/*.inc");
foreach ($filenames as $filename)
{
include $filename;
echo $filename;
}
But instead of a bunch of included files, I'm only geting:
Warning: Invalid argument supplied for foreach() in include_once() ...
It appears that $filenames is empty. Why would that be the case? (I already checked that the folder contains .inc files!)
Do a var_dump($filenames). Do you get an empty array, e.g.
array(0) {
}
If so, the glob worked, but didn't find any files. If you get a boolean false, e.g.
bool(false)
then the glob failed completely - incorrect path, unreadable directory, etc...
If you are doing this in Drupal from within a module, using a relative path might not work as the index.php in DRUPAL_ROOT is where your code is run from.
You may want to change it to either (1) or (2) below:
// Assuming your webform folder is at sites/all/modules/[MODULE_NAME]/webform
$pattern = drupal_get_path('module', 'MODULE_NAME') . '/webform/components/*.inc'; // (1)
// Assuming the context is MODULE_NAME.module.
// $pattern = dirname(__FILE__) . '/webform/components/*.inc'; // (2)
$filenames = glob($pattern);
foreach ($filenames as $filename) {
include $filename;
echo $filename;
}
If you are doing this from outside a module and not from a script in the root of your Drupal site, use DRUPAL_ROOT to set the correct pattern to search for.
(Drupal 7)
Simple question - How to list .htaccess files using glob()?
glob() does list "hidden" files (files starting with . including the directories . and ..), but only if you explicitly ask it for:
glob(".*");
Filtering the returned glob() array for .htaccess entries with preg_grep:
$files = glob(".*") AND $files = preg_grep('/\.htaccess$/', $files);
The alternative to glob of course would be just using scandir() and a filter (fnmatch or regex):
preg_grep('/^\.\w+/', scandir("."))
in case any body come to here,
since the SPL implemented in PHP, and offers some cool iterators, you may make use of the to list your hidden files such as .htaccess files or it's alternative hidden linux files.
using DirectoryIterator to list all of directory contents and excluding the . and .. as follows:
$path = 'path/to/dir';
$files = new DirectoryIterator($path);
foreach ($files as $file) {
// excluding the . and ..
if ($file->isDot() === false) {
// make some stuff
}
}