php download not working for & - php

I have this php function to download files from my public_html.
function download($file){
$dir = "RepList/";
$path = $dir.$file;
if(!file_exists($path))
{
die("Error : The file does not exists !");
}else{
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename=$file");
header("Content-Typr: application/w3g");
header("Content-Transfer-Encoding: binary");
readfile($path);
}
}
if (isset($_GET['download']))
{
if(!empty($_GET['download']))
{
$file = $_GET['download'];
download($file);
}
}
For download I use something like:
<a class="download" id="download'.$i.'_" href="../download.php?download='.$cc->EList[$i]->getEName().'" target="_blank">Download</a>
If I have "&" symbol in my file name, I will receive "Error : The file does not exists !".
I worked on this like 1 year ago and now I want to fix the error.
Any advice? Something easy & fast.

You should probably use urlencode() function and change creating HTML link to:
<a class="download" id="download'.$i.'_" href="../download.php?download='.urlencode($cc->EList[$i]->getEName()).'" target="_blank">Download</a>
otherwise & character is treated as variables separator in url

Related

PHP says file does not exists when it does

Very lost right now. The filePath to this document is correct and is in the directory that is being printed by the echo but it keeps saying "file not found".
$fileName = 'driver.txt';
$filePath = $_SERVER['DOCUMENT_ROOT']."/driver.txt";
echo $filePath;
if(!file_exists($filePath)){ // file does not exist
die('file not found');
} else {
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename=$fileName");
header("Content-Type: application/zip");
header("Content-Transfer-Encoding: binary");
// read the file from disk
readfile($filePath);
}
Very silly mistake, tried downloading a different file and it worked. I then realized the file's name was driver.txt. So PHP was looking for driver.txt.txt. I appreciate all the help.

PHP: Download file from Server

I have tried all sorts of codes I've found but nothing has worked for me...
When I use readfile() or fopen() or something like this:
function makeDownload($file, $dir, $type) {
header("Content-Type: $type");
header("Content-Disposition: attachment; filename=\"$file\"");
readfile($dir.$file);
}
... A download is started but the file is always empty...
here's the last code I've tried:
$filename = "gandalf.jpg";
// define error message
$err = '<p style="color:#990000">Sorry, the file you are requesting is unavailable.</p>';
if (!$filename) {
// if variable $filename is NULL or false display the message
echo " filename NULL";
echo $err;
} else {
// define the path to your download folder plus assign the file name
$path = 'upload/'.$filename;
// check that file exists and is readable
if (file_exists($path) && is_readable($path)) {
echo "file exists";
// get the file size and send the http headers
$size = filesize($path);
header('Content-Type: image/png');
header('Content-Length: '.$size);
header('Content-Disposition: attachment; filename='.$filename);
header('Content-Transfer-Encoding: binary');
// open the file in binary read-only mode
// display the error message if file can't be opened
//readfile($path.$filename);
$file = # fopen($path, 'rb');
if ($file) {
// stream the file and exit the script when complete
fpassthru($file);
exit;
} else {
echo $err;
}
} else {
echo $err;
}
}
This is the source of my code: Source code
I hope you can help me :)
I use google chrome
I need your help for this little project: Link to Project
When you click on a file in the drop down appears "herunterladen" (download in german) here is where I want to start the php download code
I just need the most basic download function for PHP... why is there an upload example on w3schools but NOT a download example? oO
$fileSource= $_GET['fileSource']; //If you are passing the filename as a URL parameter
$fileSource= "sample.pdf" //If the filename is fixed in the current folder
if($fileSource) {
$filesize = filesize($fileSource);
$path_parts = pathinfo($fileSource);
$ext = strtolower($path_parts["extension"]);
switch ($ext) {
case "pdf":
header("Content-Disposition: attachment;
filename=\"".$path_parts["basename"]."\""); // use 'attachment' to
force a download
header("Content-type: application/pdf"); // add here more headers
for diff. extensions
break;
default:
header("Content-type: application/octet-stream");
header("Content-Disposition: filename=\"".$path_parts["basename"]."\"");
}
if($filesize) {
header("Content-length: $filesize");
}
readfile($fileSource);
exit;
}

PHP ReadFile Issues with Spaces

Got a bit of a PHP problem I am stuck on at the moment. I have a file called download.php which gives secure access to file downloads which are stored in a private folder on the server. i.e. 1 level above httpd.
The code looks like this.
$Document = new Documents($DID);
$file = $Document->getfilewithdir();
header("Content-Description: File Transfer");
header("Content-length: " . $Document->getfilesize());
header("Content-type: " . $Document->getfiletype());
header("Pragma: public");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-disposition: attachment; filename=\"" . basename($Document->getfilename()) . "\"");
readfile($file);
exit;
Where
$Document->getfilewithdir() returns something like /Applications/MAMP/htdocs/Portal_QCNW/private/portal_files/filename.docx
and
$Document->getfilename() returns something like filename.docx.
Extra code included:
function setfilename($Val)
{
$this->c_Filename = $Val;
}
function getfilename()
{
return $this->c_Filename;
}
function getfilewithdir()
{
$path_parts = pathinfo($this->geturl());
$file_name = $path_parts['basename'];
return FILELOC . $file_name;
}
This is all fine if there file name has no spaces in, but if there are spaces in the file name i.e. file name.docx or filename (1).docx then the readfile statement returns "failed to open stream". What can I do to get round this and deal with spaces in filenames?
I use https://blueimp.github.io/jQuery-File-Upload/ to upload the file, and there could be something there were I remove spaces at the upload stage.
Any thoughts and advice would be great.
Kind Regards
James
Thanks for your help. I reviewed the code and simplified it. Because I was passing a URL file name it was been formatted as a url rather than a string.
All sorted now.
James

Cannot download pdf using ipad

I am using the following function to download pdf file it is working fine when i download it from PC or laptop but when i click on download link using ipad it opens a page with lots of special chracters and I am unable to download the file.
My download function is
public function download() {
$download_path = $_SERVER['DOCUMENT_ROOT'] . "/import";
$filename = $_GET['file'];
$file = str_replace("..", "", $filename);
$file = "$download_path/$file";
if (!file_exists($file))
die("Sorry, the file doesn't seem to exist.");
$type = filetype($file);
header("Content-type: $type");
header("Content-Disposition: attachment;filename=$filename");
header('Pragma: no-cache');
header('Expires: 0');
readfile($file);
}
Any idea about this error ?
This is probably the issue:
$type = filetype($file);
header("Content-type: $type");
From the manual
Possible values are fifo, char, dir, block, link, file, socket and
unknown.
Which are not things you want to see in the header. You are probably looking for:
header('Content-type: application/pdf');
You probably want finfo_file() and not filetype().

Joomla component + forcedownload

i have coded a component (Gallery). And i want to intergrate forcedownload for all items.
But will not work :(, just getting corrupted files. I have tried to use exact same code and run it outside of joomla and its works fine. I have created a template thats totaly empty just
have but its not help.
My (com_component/views/forcedownload/tmpl/default.php)
<?php
if(!isset($_GET["id"]) || empty($this->item)) {
echo "Invalid request";
die();
}
$path = "images/randomtest/catid".$this->item->cat_id."/";
$filename = $path.$this->item->filename;
$file = $this->item->filename;
if(!file_exists($filename)) {
echo "Error: File not found";
die();
}
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename=$file");
header("Content-Type: Content-type: application/octet-stream");
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($filename));
readfile($filename);
?>
The first thing is: Are you using $_GET['format'] = raw? If not in your controller do
if(!isset($_GET["format"]) || $_GET["format"]!='raw') {
$this->setRedirect( JURI::current(). '?format=raw' );
return false;
}
also in your header you are declared content type as: application/octet-stream, are you sure you want this?
also here:
$path = "images/randomtest/catid".$this->item->cat_id."/";
you need, whole SYSTEM PATH not only local one, try using:
$path = JPATH_SITE."/images/randomtest/catid".$this->item->cat_id."/";

Categories