Mentioning directory together with glob - php

I have this script to access all the xml files in a folder.
But how would I specify a directory inside the glob ?
Here's my code :-
foreach (glob("*.xml*") as $filename) {
echo $filename."<br />";
}

You can add the directory in this way :-
$dir="dirname/";
foreach (glob("$dir*.xml*") as $filename) {
echo $filename."<br />";
}
Notice :- When you echo the filename, the directory would come attached to it. So for instance the filename is example.txt, then the output will be dirname/output.txt. You can then use explode to remoe the dir name.

Related

Displaying files from resources folder and making them downloadable

I am trying to create a sort of download list where I grab all the files from the resources folder.. my php file is in a different folder to that..
and then I want to make them like links so people can download those files
I tried this:
<?php
foreach(glob('*.*') as $filename) {
echo $filename."<br />";
}
?>
However I don't know how to grab files from my resources folder or make them downloadable :(
Cheers!
You can scan the directory and grab the the files with scandir
$dir = '/tmp';
$files1 = scandir($dir);
//Check if file is not . OR ..
$ignore = array(".", "..");
foreach($files1 as $key => $value){
if (!in_array($value, $ignore)) {
echo $value;
}
}
For more information read :
http://php.net/manual/en/function.scandir.php
In order to make a downloadable link I would like to know what kind of files you want to download.

PHP glob function from another directory

I have some PDF files in public_html/site.com/pdf but in my index files is located in public_html/site.com/
I would like to use the glob() function to iterate through all the pdf files located In the pdf/ folder. The problem is that I am not getting any result.
Here is that I tried.
# public_html/index.php
<?php
foreach (glob("pdf/*.pdf") as $filename) {
echo "$filename <br/>";
}
?>
Alternatively, if you want to search your public folder recursively, you can use RecursiveDirectoryIterator to achieve this: Consider this example:
Lets say you have this directory structure:
/var/
/www/
/test/
/images/
image.png
/files/
/files2/
pdf.pdf <-- searching for some pdf files, happens to be here
<?php
$filetype_to_search = 'pdf';
$root_path = '/var/www/test'; // your doc root (or maybe C:/xampp/htdocs/) :p
foreach ($iterator = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($root_path,
RecursiveDirectoryIterator::SKIP_DOTS),
RecursiveIteratorIterator::SELF_FIRST) as $value) {
if($value->getExtension() == $filetype_to_search) {
echo $value->getPathname() . "<br/>";
}
}
// ouput should be /var/www/test/images/files/files2/pdf.pdf
?>
My advice? Just set your full path as a variable & use it in cases like this.
In general you really cannot trust automatic methods used to get paths to be reliable for various reasons. This is why I have decided it’s best to set a base path explicitly as I explain here. I will assume you are on a standard Linux setup with /var/www/ as the root. So in your case you would set:
$BASE_PATH = '/var/www/public_html/site.com/';
And then your final code would be something like this:
$BASE_PATH = '/var/www/public_html/site.com/'
foreach (glob($BASE_PATH . "pdf/*.pdf") as $filename) {
echo "$filename <br/>";
}

Find path to file on server using php -

I would like to know how to get the absolute file path of the file i have found using glob() function. I am able to find a desired file using
foreach (glob("access.php") as $filename) {
echo "$filename absolutepath is: ";
}
not sure what function gets the full path of the file searched. Tried to google but can't find anything sensible.
Thanks
Slight update :
I have noticed that glob() function only searches the directory that the script is run from - and that is not good to me. I need a function that is equivalent to unix find / -name "somename"
Any alternative ? or am i missing something with the glob() ??
If you have to look also for files in subdirectories, you could use something like the following:
foreach (glob("{access.php,{*/,*/*/,*/*/*/}access.php}", GLOB_BRACE) as $filename) {
echo "$filename absolutepath is: ".realpath($filename);
}
You can use realpath to get file absolute path. More info: http://www.php.net/manual/en/function.realpath.php
I thinkt you need realpath(), as described here: http://www.php.net/manual/en/function.realpath.php
foreach (glob("access.php") as $filename) {
echo "$filename absolutepath is: " . realpath($filename);
}
The directory in which the glob function searches is available through the getcwd function.
To search any directory, given its path, one may use the following code snippet:
$dirToList = '/home/username/documents';
$patternToSearch = '*.odt'; // e.g. search for LibreOffice OpenDocument files
$foundFiles = FALSE;
$olddir = getcwd();
if (chdir($dirToList)) {
$foundFiles = glob($patternToSearch);
chdir($olddir); // switch back to the dir the code was running in before
if ($foundFiles) {
foreach ($foundFiles as $filename) {
echo nl2br(htmlentities(
'found file: '.$dirToList.DIRECTORY_SEPARATOR.$filename."\n"
, ENT_COMPAT, 'UTF-8'));
}
}
// else echo 'no found files';
}
// else echo 'chdir error';
To finally satiesfy your wish to do a search like
find / -name "somename"
you may put that code snippet in a function and call it while iterating through the directory tree of interest using PHP's RecursiveDirectoryIterator class.

How to open up all .text files with the same name?

Okay so let's say I have a folder and in a folder I have a ton of text files.
TextLog11564.txt
TextLog21564.txt
TextLog3456.txt
and so on, how would I open all of them, get the contents, and display them on the page? I know how to open a text file and read stuff from it but is there a way to open up all text files that start with "TextLog"? Instead of having to call them each one by one in the code.
You can use glob() for this:
<?php
foreach (glob("TextLog*.txt") as $filename) {
echo "$filename size " . filesize($filename) . "\n";
}
?>
You can use glob():
foreach (glob("TextLog*") as $filename) {
//
}

Foreach glob to include files in a subdirectory

I'm trying to learn how to include all the files in a directory using glob(), however I can't seem to get it to work. This is the code I have now:
foreach (glob("addons/*.php") as $filename) {
include $filename;
}
However a single file include seems to work just fine:
include "addons/hello.php";
This is what my file structure looks like:
Theme
-addons
--hello.php
-index.php
-options.php
So I'm not sure where the problem is. The code is inside a (theme) subdirectory itself, if that makes a difference at all. Thanks.
Use this for testing:
foreach (glob("addons/*.php", GLOB_NOCHECK) as $filename) {
PRINT $filename . "\n";
}
Should the directory not exist relatively to the current, then it will show addons/*.php as output.
This recursive function should do the trick:
function recursiveGlob($dir, $ext) {
$globFiles = glob("$dir/*.$ext");
$globDirs = glob("$dir/*", GLOB_ONLYDIR);
foreach ($globDirs as $dir) {
recursiveGlob($dir, $ext);
}
foreach ($globFiles as $file) {
include $file;
}
}
Usage: recursiveGlob('C:\Some\Dir', 'php');
If you want it to do other things to the individual file, just replace the include $file part.
Include is going to be using the search path which (while it typically includes the current working directory) isn't limited to that... using glob() with a relative directory path will always be relative to the current working directory. Before you enter your loop... ensure that your current working directory is where you think it is using echo getcwd()... you may find you're not in the Theme subdirectory after all; but that the Theme subdirectory is in the search path.
Make sure that path to file is absolute (from root of your server).
In my case this example works without problems:
$dir = getcwd();//can be replaced with your local path
foreach (glob("{$dir}/addons/*.php") as $filename) {
if(file_exists($filename))
{
//file exists, we can include it
include $filename;
}
else
{
echo 'File ' . $filename . ' not found<br />';
}
};

Categories