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

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) {
//
}

Related

Display All .MP3 Files From A Folder In An Audio Tag

How can I get all audio files from a folder and display it in Audio Tags?
What I Have Tried
$sPath = 'music/favorite/*.mp3';
foreach (glob($sPath) AS $mp3) {
print $mp3 . PHP_EOL;
}
You can do it inside of the foreach. If you just want to echo the files, just do it like this:
foreach (glob($sPath) AS $mp3) {
echo '<audio>';
echo '<source src="'.$mp3.'" type="audio/mpeg">';
echo '</audio>';
}
Maybe you need to adjust the path of your $mp3 var.
It's just a sample. Please don't blame me because I didn't tested this.
Hope this helps.

Delete files in folder with foreach loop

In a file called docs.php I have made the following code to display all the files inside uploads folder
The foreach() loops all the files there stored in a view of the website and has an hyperlink that let it be open in the browser (in my model file it has restriction's to only pds, png or jpg files)
What i'm not able to do is inside the foreach() loop for every iteration is display some sort of hyperlink (example: Delete me!) that permits the user to click and delete only that particular file physical from the server, the others not clicked must remain visible.
My php code inside a view to the forloop() is:
<?php
$files=\yii\helpers\FileHelper::findFiles('uploads/', ['except'=>['*.DS_Store']]);
if (isset($files[0])) {
foreach ($files as $index => $file) {
$nameFile = substr($file, strrpos($file, '/') + 1);
echo Html::a($nameFile, Url::base().'/uploads/'.$nameFile) . "<br/>" . "<br/>" ; // render do ficheiro no browser
}
} else {
echo "There are no files available for download.";
}
?>
As i assume you have concept of FileHelper, but to delete file you have to use php native function unlink(filePath). unlink()

Mentioning directory together with glob

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.

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.

PHP Extract only the Filename from UNC Path

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);

Categories