I have a script that I would like to edit.
It basically downloads files and counts it and then displays it as expected. The only problem is that when I am using a file that has characters in it, it throws invalid file error instead of downloading.
When I have a file named (GREGZYY-SUCK-iT-AN-SEE-2O16.zip) for example, it downloads the file. But, when I have a file named (RyanJones - F.N.T ( For GG, KyleMorris & Murdo.zip) 16) it throws me invalid file error.
Below are the files and the code I have:
index.php
download.php
files folder & a counters folder
index.php is as follows:
<?php
function get_download_count($file=null){
$counters = './counters/';
if($file == null) return 0;
$count = 0;
if(file_exists($counters.md5($file).'_counter.txt')){
$fp = fopen($counters.md5($file).'_counter.txt', "r");
$count = fread($fp, 1024);
fclose($fp);
}else{
$fp = fopen($counters.md5($file).'_counter.txt', "w+");
fwrite($fp, $count);
fclose($fp);
}
return $count;
}
?>
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title></title>
</head>
<body>
filenameziphere (Downloaded <?php echo get_download_count('filenameziphere.zip');?> times) <br>
filenamehere (Downloaded <?php echo get_download_count('filenameziphere2.zip');?> times) <br>
</body>
</html>
download.php is as follows:
<?php
$downloads_folder = './';
$counters_folder = './counters/';
if(!empty($_GET['file'])){
$file = basename($_GET['file']);
if(file_exists($downloads_folder.$file)){
if(file_exists($counters_folder.md5($file).'_counter.txt')){
$fp = fopen($counters_folder.md5($file).'_counter.txt', "r");
$count = fread($fp, 1024);
fclose($fp);
$fp = fopen($counters_folder.md5($file).'_counter.txt', "w");
fwrite($fp, $count + 1);
fclose($fp);
}else{
$fp = fopen($counters_folder.md5($file).'_counter.txt', "w+");
fwrite($fp, 1);
fclose($fp);
}
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.$file.'"');
header('Content-Transfer-Encoding: binary');
header('Connection: Keep-Alive');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre- check=0');
header('Pragma: public');
header('Content-Length: ' . sprintf("%u", filesize($downloads_folder.$file)));
// http headers for zip downloads
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"".$file."\"");
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".sprintf($downloads_folder.$file));
ob_end_flush();
$fh = fopen($downloads_folder.$file, "rb");
while (!feof($fh)) {
echo fgets($fh);
flush();
}
fclose($fh);
exit;
}else{
header("HTTP/1.0 404 Not Found");
exit('File not found!');
}
}
?>
I tried changing the utf charset to utf-8 at times and other pieces, but still nothing is working. Any help is appreciated and if possible a shorter code.
Related
when i send a csv file to the browser using header('Content-Disposition: attachment filename="xyz.cs" '); the file named as export.php (filename of the php-script) instead of xyz.csv
public function sendCSV($arrBody, $arrHeadings)
{
$fh = fopen('php://output', 'w');
ob_start();
fputcsv($fh, $arrHeadings);
if (! empty($arrBody)) {
foreach ($arrBody as $line) {
fputcsv($fh, $line);
}
}
$strBuffer = ob_get_clean();
$strFile = 'csv_' . date('Ymd') . '-' . uniqid() . '_export.csv';
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false);
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment filename=\"$strFile\";" );
header("Content-Transfer-Encoding: binary");
exit($strBuffer);
}
I want to send a file directly to browser:
<?php
#ini_set('error_reporting', 0);
#apache_setenv('no-gzip', 1);
#ini_set('zlib.output_compression', 'Off');
#ob_implicit_flush();
$file = 'ORG.iso';
$quoted = sprintf('"%s"', addcslashes(basename($file), '"\\'));
$file_size = filesize($file);
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename=' . $quoted);
header('Content-Transfer-Encoding: binary');
header('Connection: Keep-Alive');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . $file_size);
#ob_end_flush();
set_time_limit(0);
$f = fopen($file, 'r');
while (!feof($f))
{
$buff = fread($f, 204800);
echo $buff;
sleep(1);
}
fclose($f);
It shows correct file size in Firefox and Chrome right after visitor enters the link. However it does not work in jDownloader. It seems like jDownloader is waiting for the whole script to end.
When I try a normal, static file from the same webserver- jDownloader shows file size instantly.
So obviously something is wrong with my script.
I tried to write a php-script to create and download a zip file. When I tested the script on my localhost, the download works, but when it's uploaded to the server, things go wrong: instead of downloading the zip file, the content of the file is displayed in the browser.
Can somebody point me in the right direction?
The code
$zip = new ZipArchive();
$zip->open("$maand/zipfile.zip", ZipArchive::OVERWRITE);
$zip->addFile("$maand/ant/a_nb.txt", 'ant.txt');
$zip->addFile("$maand/lim/l_nb.txt", 'lim.txt');
$zip->addFile("$maand/oos/o_nb.txt", 'oos.txt');
$zip->addFile("$maand/vla/v_nb.txt", 'vla.txt');
$zip->addFile("$maand/wes/w_nb.txt", 'wes.txt');
$zip->close();
$filename = "zipfile.zip";
$filepath = "$maand/";
// headers for zip downloads
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public");
header("Content-type: application/zip");
header("Content-Disposition: attachment; filename=\"".$filename."\"");
header("Content-Length: ".filesize($filepath.$filename));
ob_end_flush();
#readfile($filepath.$filename);
you missing ob_start()
example:
header('Content-Type: application/csv');
header('Content-Disposition: attachement; filename="' . $download . '"');
ob_start();
$str = '';
if(file_exists($file) === true){
$str = file_get_contents($file);
}
ob_end_clean();
echo $str;
Im using a button to direct to a script which creates a text file:
<?php
$handle = fopen("file.txt", "w");
fwrite($handle, "text1.....");
fclose($handle);
?>
after that I want the web browser to propose to download or open this file
how should I do? Thanks
use readfile() and application/octet-stream headers
<?php
$handle = fopen("file.txt", "w");
fwrite($handle, "text1.....");
fclose($handle);
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename('file.txt'));
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize('file.txt'));
readfile('file.txt');
exit;
?>
$content = file_get_contents ($filename);
header ('Content-Type: application/octet-stream');
echo $content;
Should work
Or possible for users to download the file without saving it on the server?
I am getting data from the database, and I want to save them .doc (MS Word) file.
if(isset($_POST['save']))
{
$file = "new_file2.doc";
$stringData = "Text text text text...."; //This data put in new Word file
$fh = fopen($file, 'w');
fwrite($fh, $stringData);
fclose($fh);
header('Content-Description: File Transfer');
header('Content-type: application/msword');
header('Content-Disposition: attachment; filename="'.$file.'"');
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
readfile($file);
unlink($file);
exit;
}
How should the code look like without this:
" $fh = fopen($file, 'w');
fwrite($fh, $stringData);
fclose($fh);"
and this "unlink($file);"
I hope to understand what I need
enter code here
You just need to output headers and then the content:
header(...);
echo $stringData;
No need to play with any files.
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment;
filename=\"$_fileName\"");
ob_clean();
readfile($_filePath);