Here is my code. Its working properly and now i want to compress the csv file before generate. Anybody please suggest me what to do!
$filename = 'Product_Export_' . date('Y-m-d') . '.csv';
header('Content-Encoding: UTF-8');
header('Content-type: text/csv; charset=UTF-8');
header('Content-Disposition: attachment; filename=' . $filename);
echo "\xEF\xBB\xBF"; // UTF-8 BOM
// clean the output buffer
ob_clean();
echo trim($_SESSION['cProductCSVdata']);
$_SESSION['cProductCSVdata'] = '';
exit;
Sending it to a zip file with ZipArchive is your solution.
Here is an example with .txt files.
<?php
$zip = new ZipArchive();
$filename = "./test112.zip";
if ($zip->open($filename, ZipArchive::CREATE)!==TRUE) {
exit("cannot open <$filename>\n");
}
$zip->addFromString("testfilephp.txt" . time(), "#1 This is a test string added as testfilephp.txt.\n");
$zip->addFromString("testfilephp2.txt" . time(), "#2 This is a test string added as testfilephp2.txt.\n");
$zip->addFile($thisdir . "/too.php","/testfromfile.php");
echo "numfiles: " . $zip->numFiles . "\n";
echo "status:" . $zip->status . "\n";
$zip->close();
?>
Related
I'm using PHPSpreadsheet to export an html table to .xlsx file.
Everything works fine except...
I want to give the filename the greek fullname of the employee that is stored in a SESSION but I'm getting something like this:
2021_ΑΘΑΝΑΣΙΟΣ ΛΑΜΠΡΙΔΗΣ.xlsx
Is there a way I can set the actual name as the file name???
Edit: the code is
if(isset($_POST["file_content"]))
{
$temporary_html_file = './tmp_html/' . time() . '.html';
file_put_contents($temporary_html_file, $_POST["file_content"]);
$reader = IOFactory::createReader('Html');
$spreadsheet = $reader->load($temporary_html_file);
$writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
$filename = $ecoYear . "_" . $_SESSION['fullname'] . '.xlsx';
$writer->save($filename);
header('Content-Type: application/x-www-form-urlencoded');
header('Content-Transfer-Encoding: Binary');
header("Content-disposition: attachment; filename=\"".$filename."\"");
readfile($filename);
unlink($temporary_html_file);
unlink($filename);
exit;
}
html_entity_decode — Convert HTML entities to their corresponding characters
<?php
$orig = "2021_ΑΘΑΝΑΣΙΟΣ ΛΑΜΠΡΙΔΗΣ.xlsx";
$text = html_entity_decode($orig);
echo $orig . PHP_EOL;
echo $text . PHP_EOL;
?>
Output: 72103715.php
2021_ΑΘΑΝΑΣΙΟΣ ΛΑΜΠΡΙΔΗΣ.xlsx
2021_ΑΘΑΝΑΣΙΟΣ ΛΑΜΠΡΙΔΗΣ.xlsx
I am trying to set up a PHP page to be able to download some log files.
I have tried searching (very hard) for a solution, but I don't really know PHP or HTML, so I have tried many snippets from many sources.
<?php
$dir = "/home/pi/fluidLogs";
$phpfiles = glob("$dir/log*.txt");
foreach ($phpfiles as $phpfile) {
echo '<a href=?file=' . $phpfile . '>' . basename($phpfile) . '</a>';
echo ' - Last mod: ' . date("Ymd-H:i:s", filemtime($phpfile));
echo ' - Size: ' . filesize($phpfile);
echo '</a><br>';
}
if(isset($_GET['file'])){
header('Content-Type: application/octet-stream');
header("Content-Transfer-Encoding: Binary");
header("Content-disposition: attachment; filename=\"" . basename($_GET['file']) . "\"");
readfile($_GET['file']);
}
?>
For example one file should contain,
This is a fake log file.
OK?
with a new line before OK?, but this (and all the downloaded files (via Chrome)) have the same extra data on the first line,
Sorry about the image. The contents were not being displayed correctly.
If user is requesting for a file, i.e. clicked on your anchor link, do not echo the list of files, which are then echoed on top of the file.
<?php
if(isset($_GET['file'])){
header('Content-Type: application/octet-stream');
header("Content-Transfer-Encoding: Binary");
header("Content-disposition: attachment; filename=\"" . basename($_GET['file']) . "\"");
readfile($_GET['file']);
}
else{
$dir = "/home/pi/fluidLogs";
$phpfiles = glob("$dir/log*.txt");
foreach ($phpfiles as $phpfile) {
echo '<a href=?file=' . $phpfile . '>' . basename($phpfile) . '</a>';
echo ' - Last mod: ' . date("Ymd-H:i:s", filemtime($phpfile));
echo ' - Size: ' . filesize($phpfile);
echo '<br>';
}
}
?>
I am trying to compress some video files on the fly using php and forcing compressed file to download. Below is the code I have written.
$files_todownload_string = ' movie1.mov movie2.mov '
header('Content-Type: application/x-gzip');
header('Content-Type: application/octet-stream');
header('Content-disposition: attachment; filename="file.zip"');
header( 'Content-Transfer-Encoding: binary' );
$fp = popen('zip -v - ' . $files_todownload_string, 'r');
$bufsize = 8192;
$buff = '';
while( !feof($fp) ) {
$buff = fread($fp, $bufsize);
echo $buff;
print "
";
}
pclose($fp);
The compression is done perfectly and the zip file is downloaded successfully. However, when I am trying to open the zip file in windows os, it won't open. It says that the compressed file is corrupted.
Any helps guys? Thanks in advance.
Remove print "
";
You're adding extra bytes.
I found this solution. The zipped files are working in all OS Linux, Windows and Mac.
Below is code:
$files_array = array();
$files_array[] = 'file1.mov';
$files_array[] = 'file2.mov';
$file = tempnam("tmp", "zip");
$zip = new ZipArchive();
$zip->open($file, ZipArchive::OVERWRITE);
foreach($files_array as $k => $v) {
$zip->addFile( $path.$files_available[$v] );
}
$zip->close();
header('Content-Type: application/zip');
header('Content-Length: ' . filesize($file));
header('Content-Disposition: attachment; filename="file.zip"');
header( 'Content-Transfer-Encoding: binary' );
ob_end_clean();
readfile($file);
Thanks everybody for giving the time.
My download.php is not working . After downloading is completed, the error on opening file is 'the archived file is corrupted or damaged '.
I dont know the reason of an error. so please help me. This code was a hard code. i tested this code by giving the name of the images. it worked right but when i retreived the images from database and tried to run this code , something went wrong.
<?php
echo "<br/>" , $product_id=$_REQUEST['product_id'];
echo "<br/>" , $query= ("SELECT image_name FROM " . $db_prefix ."product WHERE image_id = $product_id");
$test = class_exists('ZipArchive');
///$url='upload_images/';
$url = $_SERVER['DOCUMENT_ROOT'].'photohive/upload_images/';
$zip = new ZipArchive;
$zip->open($_SERVER['DOCUMENT_ROOT'].'photohive/downloads/file_' . time() . '.zip', ZipArchive::CREATE);
$result = mysql_query($query);
while( $row= #mysql_fetch_assoc($result))
{
//echo $url. $row['image_name'];
$file_name = $url.$row['image_name'];
if(file_exists($file_name)){
//$zip->addFile($file_name);
//$zip->addFromString(basename($file_name), file_get_contents($file_name));
$zip->addFile(realpath($file_name), $file_name);
}
}
$zip->close();
$encoding = 'binary';
$filename = $_SERVER['DOCUMENT_ROOT'].'photohive/downloads/file_' . time() . '.zip';
header('Pragma: public');
header('Expires: 0');
header('Content-Description: File Transfer');
header('Content-Disposition: filename="' . basename($filename). '"');
header('Content-Type: application/x-zip' );
header('Content-Transfer-Encoding: ' . $encoding);
header('Content-Length: ' . filesize($filename));
$file = readfile($filename);
//print($file);
exit;
?>
In your code you are using this string to get the filename twice: $_SERVER['DOCUMENT_ROOT'].'photohive/downloads/file_' . time() . '.zip';
In the second time, the time() probably will return the different timestamp, as some seconds will pass while you are compress the images.
Also using the time() method will bring the conflict on hi traffic sites, where you could have a possibility to get two or more requests in one second.
I suggest you to use the tempnam() function to generate the file. And remember to delete it after download is completed.
Here is a code:
$filename = tempnam($_SERVER['DOCUMENT_ROOT'].'photohive/downloads', "zip");
...
$zip->open($filename, ZipArchive::OVERWRITE);
And remove this line:
$filename = $_SERVER['DOCUMENT_ROOT'].'photohive/downloads/file_' . time() . '.zip';
after $encoding = 'binary';
i wrote code for downloading rar file it work's fine but
$name = 'file.rar';
$data = file_get_contents("file.rar");
$fh = fopen("$name", 'w') or die("can't open file");
fwrite($fh, $data);
fclose($fh);
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Length: ". filesize("$name").";");
header("Content-Disposition: attachment; filename=$name");
header("Content-Type: application/octet-stream; ");
header("Content-Transfer-Encoding: binary");
readfile($name);
exit;
after downloading , it shows an error unexpected end of archive while open that file,
it won't extract completely give me some suggestions thank you in advance
you can make ZIP file using following code on your web server
<?php
$za = new ZipArchive();
$za->open('test_with_comment.zip');
print_r($za);
var_dump($za);
$za->addFile('index.txt', 'newname.txt'); // original file , file to be added in zip
echo "numFiles: " . $za->numFiles . "\n";
echo "status: " . $za->status . "\n";
echo "statusSys: " . $za->statusSys . "\n";
echo "filename: " . $za->filename . "\n";
echo "comment: " . $za->comment . "\n";
for ($i=0; $i<$za->numFiles;$i++) {
echo "index: $i\n";
print_r($za->statIndex($i));
}
echo "numFile:" . $za->numFiles . "\n";
?>
After that you can give link for download..
This file.rar is generating via code?
I have tried your code without these code
$data = file_get_contents("file.rar");
$fh = fopen("$name", 'w') or die("can't open file");
fwrite($fh, $data);
fclose($fh);
Its working fine for me.