php glob() does not show files from another domain? - php

I am trying to read all files from a directory and store it in an array.But the file which contains this code is on www.xyz.com domain and i want to check files exist on https://www.test.com/prod_images/.But glob is returning empty array.Why is it returning empty array ?
foreach (glob('https://www.test.com/prod_images/*', GLOB_ONLYDIR) as $dir) {
$dirname = basename($dir);
$items[] = $dirname;
}
print_r($items);

Related

how to check for certain file extension in a folder with php [duplicate]

This question already has answers here:
Getting the names of all files in a directory with PHP
(15 answers)
Closed 4 years ago.
I have a folder named uploads with a lot of files in it. I want to find out if there is a .zip file inside it. How can i check if there is a .zip file inside it with php?
Use the glob() function.
$result = glob("my/folder/uploads/*.zip");
It will return an array with the *.zip-files.
Answer is already given by #Bemhard, i am adding more information for future use:
If you want to run script inside your uploads folder than you just need to call glob('*.zip').
<?php
foreach(glob('*.zip') as $file){
echo $file."<br/>";
}
?>
If you have multiple folders and containing multiple zip files inside the folders, than you just need to run script from root.
<?php
$dir = __DIR__; // will get the exact path
$dirs = array_filter(glob('*'), 'is_dir'); // filter is directory or not.
$i = 1;
foreach ($dirs as $key => $value) {
foreach(glob($value.'/*.zip') as $file){
echo $file."<br/>"; // this will print all files inside the folders.
}
$i++;
}
?>
One Extra point, if you want to remove all zip files with this activity, than you just need to unlink file by:
<?php
$dir = __DIR__; // will get the exact path
$dirs = array_filter(glob('*'), 'is_dir'); // filter is directory or not.
$i = 1;
foreach ($dirs as $key => $value) {
foreach(glob($value.'/*.zip') as $file){
echo $file."<br/>"; // this will print all files inside the folders.
unlink($file); // this will remove all files.
}
$i++;
}
?>
References:
Unlink
Glob
This also could help, using scandir and pathinfo
/**
*
* #param string $directoryPath the directory to scan
* #param string $extension the extintion e.g zip
* #return []
*/
function getFilesByExtension($directoryPath, $extension)
{
$filesRet = [];
$files = scandir($directoryPath);
if(!$files) return $filesRet;
foreach ($files as $file) {
if(pathinfo($file)['extension'] === $extension)
$filesRet[]= $file;
}
return $filesRet;
}
it can be used like
var_dump(getFilesByExtension("uploads/","zip"));

PHP echo all subfolder images

I have a directory with subfolders containing images. I need to display all these on one page, and also their folder name, so something like this:
echo Subfolder name
echo image, image, image
echo Subfolder2 name
echo image2, image2 , image2
etc
I've tried using
$images = glob($directory . "*.jpg");
but the problem is I have to exactly define the subfolder name in $directory, like "path/folder/subfolder/";
Is there any option like some "wildcard" that would check all subfolders and echo foreach subfolder name and its content?
Also, opendir and scandir can't be applied here due to server restrictions I can't control.
Glob normally have a recursive wildcard, written like /**/, but PHP Glob function doesn't support it. So the only way is to write your own function. Here's a simple one that support recursive wildcard:
<?php
function recursiveGlob($pattern)
{
$subPatterns = explode('/**/', $pattern);
// Get sub dirs
$dirs = glob(array_shift($subPatterns) . '/*', GLOB_ONLYDIR);
// Get files in the current dir
$files = glob($pattern);
foreach ($dirs as $dir) {
$subDirList = recursiveGlob($dir . '/**/' . implode('/**/', $subPatterns));
$files = array_merge($files, $subDirList);
}
return $files;
}
Use it like that $files = recursiveGlob("mainDir/**/*.jpg");

How to get directory names in php

how can I get all folders in a directory and make
if name of folder == $variable {
code...
}
Can you help-me please?
use glob package
elementsIncurrendDir = glob("regExpFiler*.[tT][xX][tT]")
for s in elementsIncurrendDir:
if s== 'whatever':
pass
If you want to verify that it is a folder you can use:
if os.path.isdir(s):
You can get files and folders by using scandir() function which returns array of filenames.
Function array_diff() computes difference between two arrays and effectively removes filenames . and .. from the array of filenames.
$directory = '/path/to/my/directory';
$scanned_directory = array_diff(scandir($directory), array('..', '.'));
You can then check if file is a directory using is_dir() function and also check for name of the folder.
foreach ($scanned_directory as $filename) {
if (is_dir($filename) && $filename == "folder_name") {
// filename is directory with name folder_name
}
}

Read Meta Tags from Files in a Directory

I'm trying to create a script that will read all of the other files in the directory and list them by grabbing the title meta tag for each one using the code below, but it's not working. If I'm reading the documentation correctly, get_meta_tags expects a URL by default, and if you want to point to a local file, you need to set the use_include_path parameter. But I don't think I'm doing that correctly.
$dir = '.';
$files = scandir($dir);
set_include_path($dir);
foreach ($files as &$value) {
$tags = get_meta_tags($value, true);
echo $tags['title'] . "<br/>";
}
According to the documentation it takes a URL or a filename-string. The only thing the second parameter is good for is if the files you want to parse are not on the current path, but are on the include path instead. That is not the case here, as you are already iterating over a path to get the filenames. You should do:
$dir = '.';
$files = scandir($dir);
foreach ($files as &$value) {
$tags = get_meta_tags($value);
echo $tags['title'] . "<br/>";
}

php - scandir and return matched files

I am trying to get a matched array of files using scandir() and foreach().
when I run scandir() then it returns all file list. Its okey here.
now in second step when I do foreach scandir()s array then I get only one matched file. but there are two files called (please note before doing foreach my scandir() returns all files including this two files);
widget_lc_todo.php
widget_lc_notes.php
something is missing in my code, I dont know what :-(
here is my code:
$path = get_template_directory().'/templates';
$files = scandir($path);
print_r($files);
$template = array();
foreach ($files as $file){
if(preg_match('/widget_lc?/', $file)):
$template[] = $file;
return $template;
endif;
}
print_r($template);
Your code above is calling return as soon as it finds the first matching file, which means that the foreach loop exits as soon as preg_match returns true. You should not return until after the foreach loop exits:
// ...
foreach ($files as $file){
if(preg_match('/widget_lc?/', $file)) {
$template[] = $file;
}
}
return $template;
// ...

Categories