I'm trying to create 2 text files which should only be placed in a downloadable zip file.
Twice use of
fopen("php://output","w")
with
header() things
works and helps avoiding file creation on server but I end up mixing the two strings to into one downloadable text file.
Here is a local version of creating 2 files in a local directory:
$dirname = "C:/texts";
if (!is_dir($dirname)) {
mkdir($dirname);
}
$myfile = fopen("C:/texts/one".$dateInputted.".txt", "w") or die("Unable to open file!");
fwrite($myfile, $one);
fclose($myfile);
$myfile = fopen("C:/texts/two".$dateInputted.".txt", "w") or die("Unable to open file!");
fwrite($myfile, $two);
fclose($myfile);
Use ZipArchive plugin of PHP
$zip = new ZipArchive();
$zip->open('path/to/zipfile.zip', ZipArchive::CREATE);
$zip->addFile('some-file.pdf', 'subdir/filename.pdf');
$zip->addFile('another-file.xlxs', 'filename.xlxs');
$zip->close();
You can find more help here :
http://akrabat.com/php/creating-a-zip-file-with-phps-ziparchive/
http://php.net/manual/en/zip.examples.php
Related
In a symfony Controller I have a file (UploadedFile object) as a variable.
I want to open this "variable" with php ZipArchive, and extract it.
But the open() methods is expecting a string, which is the filename in the filesystem. Is there any way to process the file with the ZipArchive and without writing the file variable to the FS?
You can use tmpfile() to create a temporary file, write to it and then use it in the zip. Example:
<?php
$zip = new ZipArchive();
$zip->open(__DIR__ . '/zipfile.zip', ZipArchive::CREATE);
$fp = tmpfile();
fwrite($fp, 'Test');
$filename = stream_get_meta_data($fp)['uri'];
$zip->addFile($filename, 'filename.txt');
$zip->close();
fclose($fp);
Little improve:
$zipContent; // in this variable could be ZIP, DOCX, XLSX etc.
$fp = tmpfile();
fwrite($fp, $zipContent);
$stream = stream_get_meta_data($fp);
$filename = $stream['uri'];
$zip = new ZipArchive();
$zip->open($filename);
// profit!
$zip->close();
fclose($fp);
Just make no sense to create "zipfile.zip" and add file inside, because we already have a variable.
Check out this answer at https://stackoverflow.com/a/53902626/859837
There is a way to create a file which resides in memory only.
i am trying to read contents from a text file in php. i am using wamp on windows. i m getting this error:
Warning: fopen(/input.txt): failed to open stream: No such file or directory in C:\wamp\www\cycle_gps_sender.php on line 3
this is my code:
$location = fopen("/input.txt", "r") or die("Unable to open file!");
echo $location;
fclose($location);
both the php file and input.txt are placed in www folder of wamp.
Hope this will help you:
$File = "log_post.txt";
$fh = fopen ($File, 't') or die("can't open file");
fclose($fh);
$location = fopen("input.txt", "r") or die("Unable to open file!");
echo $location;
fclose($location);
Use this code and keep the input.txt file in the same directory where this code is written.
First check if file exist or not?
$filename = '/path/to/foo.txt';
if (file_exists($filename)) {
chmod($filename, 0777);
echo "The file $filename exists";
} else {
echo "The file $filename does not exist";
}
$location = file_get_contents('./input.txt', FILE_USE_INCLUDE_PATH);
echo $location;
or
$location = file_get_contents('input.txt');
echo $location;
hope it will help
Add full path to file.
On the Windows platform, be careful to escape any backslashes used in the path to the file, or use forward slashes.
$location = fopen("C:\\folder\\input.txt", "r");
Remove '/'(Slash)
$location = fopen("input.txt", "r") or die("Unable to open file!");
$file = fopen("path/input.txt","a+") or die("Unable to open file!");
.....
fclose($file);
You have to create file before you READ or if you open file by 'r' , so if you open file by 'a+' , your file will be created automatically.
I am trying to use the putcsv function in php to write an array to a text file but the file is blank, I tried changing the extension to .csv and it works but I need to write it to .txt as per specifications of my assignment. I attempted to change the filename to .csv then write to it then back to .txt, my filesize changed but the file is still blank.
Here is my code
$fp = fopen('logfile.txt','w') or die ('No file!!!');
fputcsv($fp,$csv);
fclose($fp);
I got your problem, if its not allow to write CSV format to text file then write in to CSV file and rename(using rename() in PHP) to text file after writing in to CSV is done.
Here is my sample code:
$fp = fopen('logfile.csv','w') or die ('No file!!!');
fputcsv($fp,$csv);
fclose($fp);
rename('logfile.csv','logfile.txt');
Let me know, if your problem is solved.
Second Method:
str_putcsv()
function str_putcsv($fields, $delimiter = ',', $enclosure = '"', $escape_char = '\\' ) {
foreach ($fields as &$field) {
$field = str_replace($enclosure, $escape_char.$enclosure, $field);
$field = $enclosure . $field . $enclosure;
}
return implode($delimiter, $fields) . "\n";
}
Simply you can call
$file = fopen("newfile.txt", "w") or die("Unable to open file!");
$csvStr = str_putcsv($csv);
fwrite($file, $csvStr);
fclose($fp);
Use fwrite()
$myfile = fopen("newfile.txt", "w") or die("Unable to open file!");
$txt = "John Doe\n";
fwrite($myfile, $txt);
$txt = "Jane Doe\n";
fwrite($myfile, $txt);
fclose($myfile);
I am developing a website in wordpress i want to put a txt file in header for read at time of load
<?php
$myfile = fopen("keyword.txt", "r") or die("Unable to open file!");
// Output one character until end-of-file
while(!feof($myfile)) {
echo fgetc($myfile);
}
fclose($myfile);
?>
its giving no file of directory found please help me how to do it n wordpress
Assuming you have keyword.txt in your template directory.
$myfile = fopen( dirname(__FILE__) . "/keyword.txt", "r") or die("Unable to open file!");
Im trying to read and write to/from the same file, is this possible?
Here is what I am getting negative results with:
<?php
$file = fopen("filename.csv", "r") or exit("Unable to open file!");
while (!feof($file)) {
$line = fgets($file);
fwrite($file,$line);
}
fclose($file);
?>
You're opening the file in read-only mode. If you want to write to the file as well, do
fopen("filename.csv", "r+")
You'll need to open the file with more 'r+' instead of just 'r'. See the documentation for fopen: http://php.net/manual/en/function.fopen.php
You opened the file in "read only" mode. See the docs.
$file = fopen("filename.csv", "r+") or exit("Unable to open file!");