I have to start a download with php of a file inside a password protected folder.
I have the following code:
<?php
$File="https://testuser:password#www.testdomain.com/test/2/file.zip";
if(file_exists($File))
{
header("Content-Disposition: attachment; filename=\"" . basename($File) . "\"");
header("Content-Type: application/octet-stream");
header("Content-Length: " . filesize($File));
header("Connection: close");
readfile($File);
}
?>
file_exists and filesize seems that don't work with this kind of url.
How can I solve the problem?
Related
How can I read/download a large file in PHP without running into allowed memory bytes exhausted?
I am currently trying:
$content = null;
while (!feof($file['data'])) {
$content .= fread($file['data'], 8192);
}
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename=\"" . $file['real_filename'] . "\"");
header("Content-Length: " . $file['length']);
echo $content;
Have you tried this ?
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename=\"" . $file['real_filename'] . "\"");
header("Content-Length: " . $file['length']);
while (!feof($file['data'])) {
print fread($file['data'], 8192);
}
I have this php code that helps user to download a file from database. But the downloadable file is downloaded directly into default computer download folder. What I want is to popup a box to ask the user where to save the file, so if their any help. Thank You.
<?php
$data = $_REQUEST['data'];
header('Content-Type: application/octet-stream');
header("Content-Transfer-Encoding: Binary");
//header("Content-disposition: attachment; filename=\"" . basename($data) . "\"");
header("Content-disposition: attachment; filename=\"" . basename($data) . "\"");
readfile($data);
?>
I have a link which shows the filename to download.When a user clicks it,it needs to get downloaded.The file gets downloaded but it contains only 0 KB.In console it shows
Resource interpreted as Document but transferred with MIME type application/force-download: "../download.php?file=filename"
My code is like this:
<a href="download.php?file=user_uploads/'.$_path['uploads'].
'logo_images/'.$row['FileName'].'" title="Click to download">'.$row['FileName'].'</a>
The download.php is like this:
<?php
$path = str_replace('/download.php?file=','',$_SERVER['REQUEST_URI']);
header("Content-Description: File Transfer");
header("Content-Type: application/force-download");
header("Content-Disposition: attachment; filename=\"" . basename($path . $uri[1]) . "\"" );
#readfile($path);
?>
Thanks in advance.I have checked the path of the file also.
try
Click
download.php
<?php
$path = 'yourpath'.$_GET['file'];
header("Content-Description: File Transfer");
header("Content-Type: application/force-download");
header("Content-Disposition: attachment; filename=".$_GET['file'] );
#readfile($path);
?>
file.txt - change with your file name
I had similar issue while downloading my file. I used this code for download.php:
<?php
$path = $_REQUEST['path'];
#setting headers
header('Content-Description: File Transfer');
header('Cache-Control: public');
header('Content-Type: application/zip');
header("Content-Transfer-Encoding: binary");
header('Content-Disposition: attachment; filename='. basename($path));
header('Content-Length: '.filesize($path));
ob_clean(); #THIS!
flush();
readfile($path);
exit;
?>
and my link was:
Download Pack
Hope it helps.
Following is the code:
define(ADMIN_ROOT,'/var/www/abc/pqr/web/control/');
$filename = $test_data['test_name'];
$flname = ADMIN_ROOT.'modules/tests/test_pdfs/'.$filename;
header("Content-Type: application/pdf");
header("Cache-Control: no-cache");
header("Accept-Ranges: none");
header("Content-Disposition: attachment; filename=\"" .$flname. ".pdf\"");
The pdf file is already present over there at the directory
/var/www/abc/pqr/web/control/modules/tests/test_pdfs/
Also it has all the permissions assigned.But when I try to download a file, it's downloading some different file with same name but the file is not in a format which could be opened. In short the desired file is not getting downloaded. Can anyone please help me in correcting my issue?
You must flush the file contents to the browser, headers won't do that.
So: readfile( $flname ) http://php.net/manual/pt_BR/function.readfile.php
Please try this
<?php
$filename = $test_data['test_name'];
$file = dirname(__FILE__) . "/modules/tests/test_pdfs/" . $filename;
//$file = "/var/www/htdocs/audio/" . $filename;
if (file_exists($file)) {
header ("Content-type: octet/stream");
header ("Content-disposition: attachment; filename=" . str_replace(" ", "_", basename($file)) . ";");
header ("Content-Length: " . filesize($file));
readfile($file);
die();
}
else {
//ERROR MESSAGE HERE..........
}
?>
Please change this line
header("Content-Type: application/pdf");
to
header ("Content-type: octet/stream");
You can also use the following code for download any type of file extensions:
<?php
$filename = $test_data['test_name'];
$contenttype = "application/force-download";
header("Content-Type: " . $contenttype);
header("Content-Disposition: attachment; filename=\"" . basename($filename) . "\";");
readfile(ADMIN_ROOT.'modules/tests/test_pdfs/'.$filename);
exit();
?>
I download a file but it gives invalid file in return.
Here's my download_content.php
<?php
$filename = $_GET["filename"];
$buffer = file_get_contents($filename);
/* Force download dialog... */
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
/* Don't allow caching... */
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
/* Set data type, size and filename */
header("Content-Type: application/octet-stream");
header("Content-Transfer-Encoding: binary");
header("Content-Length: " . strlen($buffer));
header("Content-Disposition: attachment; filename=$filename");
/* Send our file... */
echo $buffer;
?>
download file link:
Download
$r['file'] contains the file name to be downloaded.
The complete path of the folder which contain the file is:
localhost/ja/gallery/downloads/poster/large/'.$r['file'].'
ja is the root folder in htdocs.
I don't know what the actual problem is, can anyone help me out please?
<?php
header( "Content-Type: application/vnd.ms-excel" );
header( "Content-disposition: attachment; filename=spreadsheet.xls" );
// print your data here. note the following:
// - cells/columns are separated by tabs ("\t")
// - rows are separated by newlines ("\n")
// for example:
echo 'First Name' . "\t" . 'Last Name' . "\t" . 'Phone' . "\n";
echo 'John' . "\t" . 'Doe' . "\t" . '555-5555' . "\n";
?>
As said in the other question, this way looks better:
$filename = $_GET["filename"];
// Validate the filename (You so don't want people to be able to download
// EVERYTHING from your site...)
// For example let's say that you hold all your files in a "download" directory
// in your website root, with an .htaccess to deny direct download of files.
// Then:
$filename = './download' . ($basename = basename($filename));
if (!file_exists($filename))
{
header('HTTP/1.0 404 Not Found');
die();
}
// A check of filemtime and IMS/304 management would be good here
// Google 'If-Modified-Since', 'If-None-Match', 'ETag' with 'PHP'
// Be sure to disable buffer management if needed
while (ob_get_level()) {
ob_end_clean();
}
Header('Content-Type: application/download');
Header("Content-Disposition: attachment; filename=\"{$basename}\"");
header('Content-Transfer-Encoding: binary'); // Not really needed
Header('Content-Length: ' . filesize($filename));
Header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
readfile($filename);
That said, what does "invalid file" mean? Bad length? Zero length? Bad file name? Wrong MIME type? Wrong file contents? The meaning may be clear to you with everything under your eyes, but from our end it's far from obvious.
UPDATE: apparently the file is not found, which means that the filename= parameter to the PHP script is wrong (refers a file that's not there). Modified the code above to allow a directory to contain all files, and downloading from there.
Your $filename variable contains whole path as below
header("Content-Disposition: attachment; filename=$filename");
Do like this
$newfilename = explode("/",$filename);
$newfilename = $newfilename[count($newfilename)-1];
$fsize = filesize($filename);
Then pass new variable into header
header("Content-Disposition: attachment; filename=".$newfilename);
header("Content-length: $fsize");
//newline added as below
ob_clean();
flush();
readfile($filename);