So here i'm trying to send to the client some files received in a array from javascript. My problem is that i cannot redefine the filename (since its a header). So for example, i wanna send 3 files to the client, the first one is sent, then i got a error for the 2 last. Does anyone have idea how to fix it? Here is my php code:
PHP
<?php
ignore_user_abort(true);
set_time_limit(0); // disable the time limit for this script
$path = "../temp/"; // change the path to fit your websites document structure
$data = escapeshellarg($_GET['File']);
$data = str_replace("\\","",$data); // remove useless characters inserted
$data = str_replace("\"","",$data); //
$dl_file = explode(",", $data);
header("Content-type: application/octet-stream");
header("Cache-control: private"); //use this to open files directly
header('Pragma: private');
for($i = 0; $i < count($dl_file); $i++)
{
$fullPath = $path.$dl_file[$i];
$path_parts = pathinfo($fullPath);
header("Content-Disposition: attachment; filename=\"".$path_parts["basename"]."\"",true);
if ($fd = fopen ($fullPath, "r"))
{
$fsize = filesize($fullPath);
$path_parts = pathinfo($fullPath);
$ext = strtolower($path_parts["extension"]);
ob_clean();
flush();
readfile($fullPath);
}
fclose ($fd);
}
exit;
?>
Here is the error i got in logs file of the server :
Warning: Cannot modify header information - headers already sent in (php file path)
Related
On my website i allow a user to download a biography someone else posted while filling out a form. The problem I am recieving is that when I open a downloaded "txt" file, it opens fine but, when its like "docx or accdb" file, I get error messages like
We're sorry. We can't open xxx because we found a problem with its contents.
Details>>
The file is corrupt and cannot be opened
and I believe the problem happens when I get the file from the user. Here is my code
Upload file
$bio_name = $_FILES['biography']['name'];
$bio_tmp = $_FILES['biography']['tmp_name'];
$bio_size = $_FILES['biography']['size'];
$bio_type = $_FILES['biography']['type'];
$ext = pathinfo($bio_name, PATHINFO_EXTENSION);
if($ext=="pdf"||$ext=="PDF"||$ext=="doc"||$ext=="DOC"||$ext=="docx"||$ext=="DOCX"
||$ext=="XLS"||$ext=="xls"||$ext=="XLSX"||$ext=="xlsx"||$ext=="xlsm"||$ext=="XLSM"){
$fp = fopen($bio_tmp, 'r');
$content = fread($fp, filesize($bio_tmp));
$content = addslashes($content);
fclose($fp);
}else{
echo "<script>
alert('ERROR: This file format is not supported');
window.location.href='form.php';
</script>";
}
Download file
$size = $formData['bio_size'];
$type = $formData['bio_type'];
$file = $formData['bio_filename'];
$content = $formData['bio_tmp'];
header("Content-length: $size");
header("Content-type: $type");
header("Content-Disposition: attachment; filename=$file");
ob_clean();
flush();
echo $content;
I'm having an issue when trying to make a download script on my website. What ends up happening is that it only downloads between 160B to 310B instead of the whole 170MB that the file is. I'm not sure why it's happening, and if anyone can help that would be greatly appreciated. Also, I'm not sure if it's an issue with the code itself or that my host doesn't allow big files to be downloaded, as I get this message on net2ftp: "Files which are too big can't be downloaded, uploaded, copied, moved, searched, zipped, unzipped, viewed or edited; they can only be renamed, chmodded or deleted."
Here is my code:
ignore_user_abort(true);
set_time_limit(0);
$path = "/versions/";
$dl_file = $_GET['file'];
$fullPath = $path.$dl_file;
if ($fd = fopen($fullPath, "r")) {
$fsize = filesize($fullPath);
$path_parts = pathinfo($fullPath);
header("Content-type: application/octet-stream");
header("Content-Disposition: filename=\"".$path_parts["basename"]."\"");
header("Content-length: $fsize");
header("Cache-control: private");
while(!feof($fd)) {
$buffer = fread($fd, 2048);
echo $buffer;
}
}
fclose($fd);
exit;
I need help because I got an error message like "call to a member function mysql query on resource"
Here is my code:
<?php
include "koneksi.php";
$id_data = $_GET['id_data'];
ignore_user_abort(true);
set_time_limit(0); // disable the time limit for this script
$query = "SELECT * FROM kinerja WHERE id_data = $id_data";
$hasil_data = $koneksi->query($query);
if ($hasil_data->num_rows > 0) {
// output data of each row
while($row = $hasil_data->mysql_fetch_assoc()) {
$filename = $row['nama_file'];
}
}
$path = "../admin/files/"; // change the path to fit your websites document structure
//$dl_file = preg_replace("([^\w\s\d\-_~,;:\[\]\(\).]|[\.]{2,})", '', $_GET['download_file']); // simple file name validation
//$dl_file = filter_var($dl_file, FILTER_SANITIZE_URL); // Remove (more) invalid characters
$fullPath = $path.$filename;
if ($fd = fopen ($fullPath, "r")) {
$fsize = filesize($fullPath);
$path_parts = pathinfo($fullPath);
$ext = strtolower($path_parts["extension"]);
switch ($ext) {
case "pdf":
header("Content-type: application/pdf");
header("Content-Disposition: attachment; filename=\"".$path_parts["basename"]."\""); // use 'attachment' to force a file download
break;
// add more headers for other content types here
default;
header("Content-type: application/octet-stream");
header("Content-Disposition: filename=\"".$path_parts["basename"]."\"");
break;
}
header("Content-length: $fsize");
header("Cache-control: private"); //use this to open files directly
while(!feof($fd)) {
$buffer = fread($fd, 2048);
echo $buffer;
}
}
fclose ($fd);
exit;
?>
I need help for this code, I really apreciate any help. Sorry for my bad English.
It looks like the error is somewhere in koneksi.php. I would guess it's near the function query() in that class. If you include that class it would be easier to spot the error.
I'm trying to download a simple text file from my server to the client in php. Unfortunately, when i try to download, a new white page appear and the download isn't ask to the user. But, if i refresh the page, the download is now ask and the user can accept to download the file like usual. Do anyone know why?
P.S. If anyone knows about how not opening another blank page to show the download and just show it on the page i clicked the link, any information would be appreciated.
Here is the code :
HTML
Download file
PHP
<?php
ignore_user_abort(true);
set_time_limit(0); // disable the time limit for this script
$path = "toDownload/"; // change the path to fit your websites document structure
$dl_file = $_GET['download_file']; // simple file name validation
$fullPath = $path.$dl_file;
if ($fd = fopen ($fullPath, "r"))
{
$fsize = filesize($fullPath);
$path_parts = pathinfo($fullPath);
$ext = strtolower($path_parts["extension"]);
switch ($ext)
{
case "txt":
header("Content-type: application/txt");
header("Content-Disposition: attachment; filename=\"".$path_parts["basename"]."\""); // use 'attachment' to force a file download
break;
// add more headers for other content types here
default;
header("Content-type: application/octet-stream");
header("Content-Disposition: filename=\"".$path_parts["basename"]."\"");
break;
}
header("Content-length: $fsize");
header("Cache-control: public"); //use this to open files directly
while(!feof($fd))
{
$buffer = fread($fd, 2097152); // 2097152 Octet = 2Mo
echo $buffer;
}
}
fclose ($fd);
exit;
?>
I'm using PHP (PHPExcel) to handle all the excel files.
Currently I have saved a "New Template.xlsx" in a folder. And now I want the users to be able to download the "New Template.xlsx" at a click of a button.
How do i do that with php or phpexcel (i'm using Yii PHP framework as well)?
I'm not sure how to start, all I have now is
$filePath = Yii::app()->basePath . '\\vendors\\Excel Template\\New Template.xlsx';
Yii::import('application.vendors.PHPExcel', true);
$objPHPExcel = PHPExcel_IOFactory::load($filePath);
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
Edit
Thanks to Miqi180's comments, I'm now able to download the excel file, however I am unable to open it because of invalid file extension/file format.
Here are my codes:
<?php
ignore_user_abort(true);
set_time_limit(0); // disable the time limit for this script
//$path = "/absolute_path_to_your_files/"; // change the path to fit your websites document structure
$path = Yii::app()->basePath . '\\vendors\\Excel Template\\';
//$dl_file = preg_replace("([^\w\s\d\-_~,;:\[\]\(\].]|[\.]{2,})", '', $_GET['download_file']); // simple file name validation
$dl_file = preg_replace("([^\w\s\d\-_~,;:\[\]\(\].]|[\.]{2,})", '', 'NewTemplate.xlsx'); // simple file name validation
$dl_file = filter_var($dl_file, FILTER_SANITIZE_URL); // Remove (more) invalid characters
$fullPath = $path.$dl_file;
if ($fd = fopen ($fullPath, "r")) {
$fsize = filesize($fullPath);
$path_parts = pathinfo($fullPath);
$ext = strtolower($path_parts["extension"]);
switch ($ext) {
case "xlsx":
//header("Content-type: application/vnd.ms-excel");
header("Content-type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
header("Content-Disposition: attachment; filename=\"".$path_parts["basename"]."\""); // use 'attachment' to force a file download
break;
// add more headers for other content types here
default;
header("Content-type: application/octet-stream");
header("Content-Disposition: filename=\"".$path_parts["basename"]."\"");
break;
}
header("Content-length: $fsize");
header("Cache-control: private"); //use this to open files directly
while(!feof($fd)) {
$buffer = fread($fd, 2048);
echo $buffer;
}
}
fclose ($fd);
exit;
Stop Yii from sending its own headers and output
Send the appropriate headers for an xlsx file
Save the file to php://output
Look at some of the examples in the PHPExcel Examples folder like 01simple-download-xlsx.php
But unless you're changing the template between loading it and sending it to the user, there's no need to use PHPExcel for this
Basically, what you need to do is execute a php script which fires when another page is loaded, e.g. "download.php" and then send the file through the header commands in that script.
Complete working code example taken from here:
<?php
ignore_user_abort(true);
set_time_limit(0); // disable the time limit for this script
$path = "/absolute_path_to_your_files/"; // change the path to fit your websites document structure
$dl_file = preg_replace("([^\w\s\d\-_~,;:\[\]\(\].]|[\.]{2,})", '', $_GET['download_file']); // simple file name validation
$dl_file = filter_var($dl_file, FILTER_SANITIZE_URL); // Remove (more) invalid characters
$fullPath = $path.$dl_file;
if ($fd = fopen ($fullPath, "r")) {
$fsize = filesize($fullPath);
$path_parts = pathinfo($fullPath);
$ext = strtolower($path_parts["extension"]);
switch ($ext) {
case "pdf":
header("Content-type: application/pdf");
header("Content-Disposition: attachment; filename=\"".$path_parts["basename"]."\""); // use 'attachment' to force a file download
break;
// add more headers for other content types here
default;
header("Content-type: application/octet-stream");
header("Content-Disposition: filename=\"".$path_parts["basename"]."\"");
break;
}
header("Content-length: $fsize");
header("Cache-control: private"); //use this to open files directly
while(!feof($fd)) {
$buffer = fread($fd, 2048);
echo $buffer;
}
}
fclose ($fd);
exit;
However, you need to set the content-type correctly first (see this post).
from
header("Content-type: application/pdf");
to
header("Content-type: application/vnd.ms-excel");
or for For Excel2007 and above .xlsx files
header("Content-type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
Edit:
According to this MSDN blog article, the correct Extension MIME type for Excel .xlsx files actually is:
application/vnd.openxmlformats-officedocument.spreadsheetml.sheet