I would like to write all files from specific folder into .txt file. The output working just fine, but I'm getting only one file saved into my txt file. Any idea? Thanks!
$fileList = glob('C:\users\John\Documents\*pdf');
foreach($fileList as $filename){
if(is_file($filename)){
echo $filename, '<br>';
$save_files = fopen("list.txt", "wb");
fwrite($save_files,$filename);
fclose($save_files);
}
}
Here is one way to do it...
foreach($fileList as $filename){
if(is_file($filename)){
echo $filename, '<br>';
$list[] = $filename;
}
}
$capture = implode("\r\n",$list);
file_put_contents("list.txt",$capture);
Related
I want to find a file with a wildcard in the same directory as my index.php is.
When I assign the $file_name manually with the name string, it works fine.
<?php
$file_name = glob("*.csv");
$handle = fopen($file_name, "r");
$file = fread($handle, filesize($file_name));
fclose($handle);
echo $file;
?>
The browser should output the content of the .csv file, like when I assign the $file_name manually.
you need a loop because glob() returns an array:
foreach (glob("*.csv") as $filename) {
$handle = fopen($filename, "r");
$file = fread($handle, filesize($filename));
fclose($handle);
echo $filename;
}
This script will find some images in working folder.
<?php
$workdir = getcwd(); // my working dir
$patternTofind = ".{jpg,gif,png}"; // Images example
$files = glob("$workdir*$patternTofind", GLOB_BRACE);
// Print result found
print_r($files);
?>
I am trying to copy a bunch of xlsx files to a different directory, I want to know what is that I am doing wrong
// Move all xlsx files
$src = './';
// echo "<pre>";print_r($src);die;
$dst = 'files/';
//echo "<pre>"; print_r($dst);die;
$files = glob("*.xlsx");
//echo "<pre>"; print_r($files);die;
foreach($files as $file){
$file_to_go = ($dst,$file);
echo "<pre>";print_r($file_to_go);die;
copy($file, $file_to_go);
}
How to write all the txt files contents in a folder to an html page, besides i don't know the files names, i tried this but it didn't work:
<?php
$files=scandir("D:\\new\places");
foreach (files as value)
echo (files);
?>
you can use following php code to get content of the files.
$path='../innovation';
$files=scandir($path);
//print_r($files);
foreach ($files as $key => $value) {
if($value!="." && $value!="..")
{
print_r(file_get_contents($path."/".$value));
}
}
readfile is ideal for this, since it streams the files directly to the output buffer, which will avoid memory issues for large files.
<?php
$path = 'myDirectory';
$files = glob("{$path}/*.txt");
foreach ($files as $file)
{
readfile($file);
}
If you want to list all the text files (.txt) from a directory to HTML using PHP you can use this code to do it:
<?php
echo '<ul>';
// set the path to the directory...
$dirname = "directory/";
$files = glob($dirname."*.txt");
foreach($files as $file) {
echo '<li>'.$file.'</li>';
}
echo '</ul>';
?>
You can try this:
$directory = "./foldername";
$files = glob($directory . "*.txt");
foreach($files as $file){
$file = fopen($directory . '/' . $file,'r');
while ($line = fgets($file)) {
echo($line);
}
fclose($fh);
}
Use glob() to get all *.txt files in a directory. After loop through every file with a foreach loop and get the content of each file using file_get_contents() and put the content into the target file with file_put_contents()
<?php
$files = glob("path/*.txt");
$output = "output.txt";
foreach($files as $file) {
$content = file_get_contents($file);
print_r($content);
}
?>
I try to read all *.txt files from a folder and write all content from each file into another txt file. But somehow it only writes one line into the txt file.
I tried with fwrite() and file_put_contents(), neither worked.
Here is my code:
<?php
$dh = opendir('/Applications/XAMPP/xamppfiles/htdocs/test/');
while($file = readdir($dh)) {
$contents = file_get_contents('/Applications/XAMPP/xamppfiles/htdocs/test/' . $file);
$dc = array($contents);
}
file_put_contents('content.txt', $dc);
?>
This should work for you:
(Here I get all *.txt files in a directory with glob(). After this I loop through every file with a foreach loop and get the content of each single file with file_get_contents() and I put the content into the target file with file_put_contents())
<?php
$files = glob("path/*.txt");
$output = "result.txt";
foreach($files as $file) {
$content = file_get_contents($file);
file_put_contents($output, $content, FILE_APPEND);
}
?>
try this
$contents = array();
$line = file(/*next file in dir*/);
foreach($lines as line){
array_push($line, $contents);
}
//File path of final result
$filepath = "mergedfiles.txt";
$out = fopen($filepath, "w");
//Then cycle through the files reading and writing.
foreach($filepathsArray as $file){
$in = fopen($file, "r");
while ($line = fgets($in)){
print $file;
fwrite($out, $line);
}
fclose($in);
}
//Then clean up
fclose($out);
return $filepath;
I need to read only pdf files in a directory and then read the filename of every files then I will use the filename to rename some txt files. I have tried using only eregi function. but it seems cannot read all I need. how to read them well?
here's my code :
$savePath ='D:/dir/';
$dir = opendir($savePath);
$filename = array();
while ($filename = readdir($dir)) {
if (eregi("\.pdf",$filename)){
$read = strtok ($filename,"."); //get the filenames
//to rename some txt files using the filenames that I get before
//$testfile is text files that I've read before
$testfile = "$read.txt";
$file = fopen($testfile,"r") or die ('cannot open file');
if (filesize($testfile)==0){}
else{
$text = fread($file,55024);
fclose($file);
echo "</br>"; echo "</br>";
}
}
More elegant:
foreach (glob("D:/dir/*.pdf") as $filename) {
// do something with $filename
}
To get the filename only:
foreach (glob("D:/dir/*.pdf") as $filename) {
$filename = basename($filename);
// do something with $filename
}
You can do this by filter file type.. following is sample code.
<?php
// directory path can be either absolute or relative
$dirPath = '.';
// open the specified directory and check if it's opened successfully
if ($handle = opendir($dirPath)) {
// keep reading the directory entries 'til the end
$i=0;
while (false !== ($file = readdir($handle))) {
$i++;
// just skip the reference to current and parent directory
if (eregi("\.jpg",$file) || eregi("\.gif",$file) || eregi("\.png",$file)){
if (is_dir("$dirPath/$file")) {
// found a directory, do something with it?
echo " [$file]<br>";
} else {
// found an ordinary file
echo $i."- $file<br>";
}
}
}
// ALWAYS remember to close what you opened
closedir($handle);
}
?>
Above is demonstrating for file type related to images you can do the same for .PDF files.
Better explained here