Im using the following to list the files in a directory in a intranet site I am making. The problem is that is is also listing the path to the file too, does anyone know what im doing wrong ?.
thanks :-)
<?php
foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator('./customer-files/28734f6d045f8a5a93.18936710')) as $filename)
{
echo '<p>';
echo "$filename\n";
echo '</p>';
}
?>
You know the path you're passing, just use either:
str_replace($path,'',$filename);
or
substr($filename,strlen($path));
If you don't want ANY PATH in there, you can just get the filename with $filename->getFilename();
however, that will lead to confusion, as subdirectories won't be visible.
Surely you can just use basename()
echo basename($filename) . "\n";
Related
I want to scan a directory which contains many files but I'm in need of files ending with .xml.gz . Below is the code what I've tried but it fails to meet the requirements.
$num_files = count(array_intersect(scandir(getcwd()), array('.xml.gz')));
I know that I should make use of wildcards but I'm unsuccessful in getting the right output
foreach (glob("*.xml.gz") as $filename) {
echo "$filename" . "\n";
}
I'm making a Wordpress page template and I'm trying to add all the images from a directory to an array and then use a foreach to echo them all to the HTML. But when I try to load the page I get this error Invalid argument supplied for foreach(), so this means it is not an array, but it worked before. I also tried it with the scandir function but that would give me the error [function.scandir]: failed to open dir: not implemented in. The PHP code is used is included below.
$folder = get_bloginfo('template_directory') . '/img/dir/';
$images = glob($folder . "*.{jpg,png,gif}", GLOB_BRACE);
foreach ($images as $image)
{
echo '<img src="' . $image . '" />';
}
The path to the folder is correct because with the code below I get one picture.
echo '<img src="' . get_bloginfo('template_directory') . '/img/dir/image.jpg" />';
Thanks in advance!
I think template_directory returns a URL in wordpress now, if you use get_template_directory() that will return the absolute path. (it doesn't return a trailing slash so you need to remember to add that)
That could be the issue you are having, worth a shot.
If you use a child theme use get_stylesheet_directory()
php's glob() function will return false on error (or when no matched files are found, depending on your os).
I suspect in your specific case the return value is indeed false (as foreach can work with empty array's, it'll basically just skip the block). Try to find out: var_dump($images);.
You should add a check to make sure the return value is indeed something you can work with:
if(is_array($images)) {
foreach($images as $image) { /* ... */ }
}
The problem is path. You try '/img/directory/'; and this isn't working. And later you say that '/img/dir/image.jpg' is working. Do you see directory and dir?
You have mispelled path, that's all.
I have seen a few example on the web but they seem pretty messy. I am looking for a nice and clean way to say, get me only the files/folders that have .zip on them. What I have so far is:
foreach(scandir(__DIR__) as $files) {
var_dump($files);
}
What I wonder is is if I need pre match or if the ZipArchive class has any functions that state "return only files with .zip
This should work for you, you can use glob():
<?php
foreach (glob("*.zip") as $filename) {
echo $filename . "<br />";
}
?>
possible Output:
test - Kopie.zip
test.zip
test2.zip
For more information about glob() see the manual: http://php.net/manual/en/function.glob.php
How can I search a specific directory and its subdirectories for a specific file?
What I've tried are the following two functions:
function getImageDirectory($ipaPath) {
$oDirectory = new RecursiveDirectoryIterator($ipaPath);
$oIterator = new RecursiveIteratorIterator($oDirectory);
foreach($oIterator as $oFile) {
if ($oFile->getFilename() == 'info.plist') {
return $oFile->getPath();
}
}
}
EDIT: This one works, just as well as the answer bellow! My example above return the directory of the file you are looking for, in this case "info.plist".
EDIT2: Thanks for the down votes! If someone would have asked a similar question it would have shown when I wrote the title. But nothing for PHP. So if I search for the wrong thing, not using developer language it shouldn't be down voted! And now someone with my low php skills might find an answer to this question! Is stackoverflow only for PRO's or for anyone that needs help?
As #diEcho suggested, use glob.
Get file list like this:
<?php
foreach( glob( "*.*" ) as $filename ) {
echo "$filename size " . filesize( $filename ) . "\n";
}
?>
After that, just go through the results. You might need a recursive function, to access subfolders though.
I've got the code
$directory = "C:/My file path";
$phpfiles = glob($directory . "*.html");
foreach($phpfiles as $phpfiles)
{
echo $phpfiles;
}
But how would I change it so that it doesn't just list the files, but actually links to them?
First of all, don't use same variable names at foreach(). You can link to files, like this.
foreach($phpfiles as $phpfile)
{
echo "<a href=$phpfile>".basename($phpfile)."</a>";
}
$phpfile containing full path of file (for example : /home/eray/Desktop/test.html)
basename() is returning just file name from path . basename($phpfile)'s output is test.html . If you want to print just test (without .html extension) , you can use this : basename($phpfile, ".html") Thanks, #aSeptik.
Assuming that the links are accessible via a web server you'll need a different root path for web access than you have on your computer. Also, your foreach is wrong. The second variable needs to be singular (well, at least different than the first). So assuming your web server sees the file path as a valid site path:
$rootPath = "/MyFilePath";
foreach ($phpfiles as $phpfile)
{
echo "$phpfile";
}
$files = glob("*.html");
echo '<ul>'.implode('', array_map('sprintf', array_fill(0, count($files), '<li>%s</li>'), $files, $files)).'</ul>';
This is ok "eray"
$phpfile containing full path of file (for example :
/home/eray/Desktop/test.html) basename() is returning just file name
from path . basename($phpfile)'s output is test.html . If you want to
print just test (without .html extension) , you can use this :
basename($phpfile, ".html") Thanks, #aSeptik.
how to remove. php extension and in the link.
exaple:
http//example.com/dir1/file.php **
(with out .php on end).
Thanks