I have many files in a directory that look similar to:
personalchat.spud.3353.1789
personalchat.guest.3355.1789
personalchat.ken.3355.1789
each file essentially has a few lines of html inside, and wanting to open all files, pull the content to the screen. I can do it by hardcoding a single filename but not all at once or by using a * for filename
Code:
$names=file('/var/www/html/web/www/chat.support/status/personalchat*');
foreach($names as $name)
{
echo $name.'<br>';
}
##UPATE 2
I tried changing to an array but it only displays the name of the files not the content of each:
I am able to list the files as an array:
personalchat.mick.3350.1733
personalchat.guest.3352.1739
personalchat.test.3351.1736
But not able to display the name of each file but not the content:
$mydir = '/var/www/html/web/www/chat.support/status/';
$myfiles = array_diff(scandir($mydir), array('.', '..'));
foreach($myfiles as $name)
{
echo $name.'<br>';
}
Thanks
file() function reads 1 file. So you need an array of files to read with file().
May be you can try to create such array using scandir() first, and then read the file 1 by one.
Related
i have search values like "search value" (defined as $q) and .txt files called "search value.txt" in /mydir/ directory. How can i scandir /mydir/ for the search value ($q) and put the found .txt file with php include command into page? Or is it a better way to put $q value into php file_get_contents php code (i mean put them together like a txt filename like (q$.txt - searchvalue.txt somehow) and pull the content of the .txt file into page? If yes, how? Thanks in advance.
Suppose you have this directory structure :
mydir
--file1.tx
--file2.tx
index.php
index.php
$filepath = glob("mydir/*.txt");//read all txt files in data directory
//print_r($filepath);//debug purpose
$search = 'file1';
// Browse all files and search the $search string in filenames
foreach ($filepath as $filename) {
// Give us file name only without the extension
$fileNameOnly = basename($filename, '.txt');
// Check if a file name only contains exactly our keyword $search
$isExactMatch = preg_match("/\b".$search."\b/iu", $fileNameOnly);
if ( $isExactMatch ) {
// Include the .txt file as result
require $filename;
}
}
Tested, it does the trick.
I have a script that places a CSV file into a temporary folder where it will remain until another script picks it up for import into my DB. This is a separated script and for various reasons cannot do both the placement into the temp folder AND the consecutive database import.
Since I now have a separated import script, I first need to scan the temp folder and look for the import-file, which is the ONLY file in the folder anyway, but has a constantly changing filename that I cannot pre-define. My question now is, how can I get the filename of said file, assign it to a variable and use this later for the database import?
When using a foreach() loop I end up with an array, but rather would like a string for further usage.
PHP
if(file_exists('./'.$temp)) {
$files = scandir('./'.$temp.'/');
foreach ($files as $attachment) {
if (in_array($attachment, array(".",".."))) continue;
$import_file = $attachment;
}
} else {
die("Temp Folder for $temp could not be found, hence no files exist for import. Operation cancelled.");
}
Since scandir is ordered alphabetically (http://php.net/manual/en/function.scandir.php), you file will always be at the end of the scandir array (on non-unix systems it is the only file, on unix systems it comes after . and ..).
Thus, you just need to get the last item of the array, like so:
$s = scandir("./".$temp."/");
$import_file = $s[count($s)-1];
This code automatically retrieves the name of the third file in the temp folder, so as long as there are no other files in there it should work perfectly.
Here is my directory structure,
C:\xampp\htdocs\..
C:\download\20150923abc.xls //abc is a random value
how can I attach the file 20150923abc.xls in php?
Also, how to change the filename after I got it?
Thanks.
Use the glob ability to find references all files of type .xls and then you can use the file name references as you wish. This sidesteps the issue of you not knowing the specific file name.
$files = glob("c:/download/*.xls");
This will produce an array of all .xls files with their full filepath. If you wish to rename or attach these files then you can do this using the glob reference:
rename($files[0], "c:/download/somenewname.xls");
etc. Read more at:
PHP Glob Function
EDIT:
From Comment below:
foreach (glob( $old_folder."*.xls") as $filename)
{
$names = explode('/', $filename);
$just_file_name = end($names);
echo $just_file_name . "----\n";
$new_folder = dirname(FILE)."\\prm\\att\\";
//rename_win($old_folder, $new_folder);
rename($filename, $new_folder.$just_file_name); <== this line changed.
}
unset($filename);
To fix the above code in your comment, you need to change the incorrect variables referenced (there was no array $files[0]) to the ones used in the foreach loop.
You may have heard of the glob method but that only manages to retrieve files from the directory that the file containing the method is located on - only one directory.
This is an example of the code that I am using:
<?php
foreach (glob("*.txt") as $filename)
{
$time = filemtime($filename);
$files[$time] = $filename;
}
krsort($files);
foreach ($files as $file) {
echo $file;
}
?>
What happens here is that all of the text files in the current directory are retrieved, they are then sorted by order of date modified and are then echoed out onto the page.
The problem with this is that I don't just want to retrieve text files from the one directory.
How would I change this so that I can retrieve files from multiple directories of my choice all from one page - so I can echo out all of the text files from the multiple directories onto one page rather than only echoing out the text files from one directory?
I believe I would need to store all the directories I want to glob into an array but I am not sure how to retrieve it.
Here is an example stolen from the page in my comment above
<?php
$Directory = new RecursiveDirectoryIterator('path/to/project/');
$Iterator = new RecursiveIteratorIterator($Directory);
$Regex = new RegexIterator($Iterator, '/^.+\.txt$/i', RecursiveRegexIterator::GET_MATCH);
?>
I am storing user uploaded files like pdf images and txt files in separate folders using my php script i want to retrieve the file names from the folder upload and give the pdf and txt in a group and also way to search for specific file.
I also need to rename the file before to $ja variable
$ja
$da = date("dmY");
$ja = $uid.$da;
move_uploaded_file($mi, $uploadpath)
also used this code which i found in stack
Example 01:
<?php
// read all files inside the given directory
// limited to a specific file extension
$files = glob("./ABC/*.txt");
?>
Example 02:
<?php
// perform actions for each file found
foreach (glob("./ABC/*.txt") as $filename) {
echo "$filename size " . filesize($filename) . "\n";
}
?>
the scandir(); function will help you
<?php
$dir = '/tmp';
$files1 = scandir($dir);
print_r($files1);
?>
http://www.php.net/manual/en/function.scandir.php
now you have array of all files in a location you specified you can use array functions to get your work done
you can try listing the directory with scandir, and then filter as you want in the php array of filenames you will get