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.
Related
I'm looping unto directories and each directory consist of multiple files that yet to be rename. Below is the code
<?php
$path = __DIR__.'/';
$files = array_diff(scandir($path), array('.', '..'));
foreach($files as $f ){
$files2 = array_diff(scandir($path.'/'.$f), array('.', '..'));
foreach( $files2 as $f2 ){
rename($path.'/'.$f.'/'.$f2, $path.'/'.$f.'/'.strtolower(str_replace(' ','_',$f2)));
echo 'success<br>';
}
}
above codes return an error of
The system cannot find the file specified. (code: 2)
in each directory, some of the files has the name that consist of special character(s) e.g. Velāyat-e Nūrestān.json.
Any ideas ?
Your code,in the actual state can't work because there are a lot of issues in it.I suggest to use native Directory Iterator to achieve this properly .
You can use:
$root=__DIR__.'/';
$it = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($root));//create a recursive directory iterator
$it->rewind();
while($it->valid())
{
if (
!$it->isDot()//if file basename not in ['.','..']
&&$it->isFile()//and is really a file and not a directory
)
{
rename(str_replace('/','\\',$it->getPathname()),str_replace('/','\\',$it->getPath().'\\'.mb_strtolower(str_replace(' ','_',$it->getBasename()))));//try to rename it
echo "success";
}
$it->next();
}
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");
I'm trying to list files in a folder. I have done this before, so I am not sure why I am having a problem now.
I have a PDF files I am trying to display to my web page. The directory structure looks like this:
folder1/folder2/displayFiles.php
folder1/folder2/files.pdf
displayFiles.php is the process file where I am using the code below.
I am trying to display the file called files.pdf onto the page, which is in the same directory as the process file.
Here is my code so far:
<?php
$dir = "folder1/folder2/";
// $dir = "/"; <-- I also tried this
$ffs = scandir($dir);
foreach($ffs as $ff)
{
if($ff != '.' && $ff != '..')
{
$filesize = filesize($dir . '/' . $ff);
echo "<ul><li><a download href='$dir/$ff'>$ff</a></li></ul>";
}
}
?>
I know it's a simple fix. I just cannot find the code to fix it.
Your $dir is pointing at a non-existent folder
Change the dir to point to the folder correctly $dir = ".";.
Just use glob
http://php.net/manual/de/function.glob.php
$pdfs = glob("*.pdf"); // if needed loop through your directorys and glob files
print_r($pdfs);
Just an example. You should be able to use it with some edits.
I would like to delete all files matching a particular extension in a specified directory and all subtree. I suppose I should be using using unlink but some help would be highly appreciated... Thank you!
you need a combination of this
Recursive File Search (PHP)
And the unlink / delete
You should be able to edit the example instead of echoing the file, to delete it
To delete specific extension files from sub directories, you can use the following function. Example:
<?php
function delete_recursively_($path,$match){
static $deleted = 0,
$dsize = 0;
$dirs = glob($path."*");
$files = glob($path.$match);
foreach($files as $file){
if(is_file($file)){
$deleted_size += filesize($file);
unlink($file);
$deleted++;
}
}
foreach($dirs as $dir){
if(is_dir($dir)){
$dir = basename($dir) . "/";
delete_recursively_($path.$dir,$match);
}
}
return "$deleted files deleted with a total size of $deleted_size bytes";
}
?>
e.g. To remove all text files you can use it as follows:
<?php echo delete_recursively_('/home/username/directory/', '.txt'); ?>
I'm trying to create an Intranet page that looks up all pdf documents in a UNC path and the returns them in a list as hyperlinks that opens in a new window. I'm nearly there however the following code displays the FULL UNC path - My question how can I display only the Filename (preferably without the .pdf extension too). I've experimented with the basename function but can't seem to get the right result.
//path to Network Share
$uncpath = "//myserver/adirectory/personnel/";
//get all files with a .pdf extension.
$files = glob($uncpath . "*.pdf");
//print each file name
foreach ($files as $file)
{
echo "<a target=_blank href='File:///$file'>$file</a><br>";
}
The links work fine it just the display text shows //myserver/adirectory/personnel/document.pdf rather than just document. Note the above code was taken from another example I found whilst researching. If there's a whole new better way then I'm open to suggestions.
echo basename($file);
http://php.net/basename
Modify your code like this:
<?
$uncpath = "//myserver/adirectory/personnel/";
//get all files with a .pdf extension.
$files = glob($uncpath . "*.pdf");
//print each file name
foreach ($files as $file)
{
echo "<a target=_blank href='File:///$file'>".basename($file)."</a><br>";
}
?>
You may try this, if basename() does not work for some reason:
$file_a = explode('/',$file);
if (trim(end($file_a)) == '')
$filename = $file_a[count($file_a)-2];
else
$filename = end($file_a);