PHP readfile error - php

I am getting an error. I am trying to read an attachment. It does work perfectly on most files but on few I get this error. The files have the same format and the location it is trying to read from is correct. I have tested it on windows explorer. This is way i am reading it:
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="' . $filename .'"');
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($attachment_location));
ob_clean();
flush();
readfile($attachment_location);
exit();
This is the error I get
Warning: readfile(C:\Users\Public\asdgasd\4sf3\Suppliers\saf342\Files\Revit\2016\Seinätikas.rfa): failed to open stream: No such file or directory in C:\xampp\htdocs\web\downloadattachment.php on line 58

as Johndodo kind of references, you need to ensure that PHP is operating with the correct internal character set and encoding, so that it recognises the way the file is stored in your (windows) directory structure. See what character set your windows system is using and then use that same character set for PHP internal encoding.
Edit:
Logic process would be to:
Open file reference from email and convert the filename $var into the correct encoding.
Do the file_exists check
Proceed to pass the $var to the readfile function to open.

could you please provide the content of $filename and $attachment_location.
Could you please extend your code with this:
if (file_exists($file)) {
Your code goes here
....
exit();
}
Further things to check: Is the file readable by the webserver user (if you're using a websever).
Does the problem have an effect on files in which there are special chars in the filename ?

You should put a checker for see if the file exists in your filesystem.
if (!file_exists($filePath)) {
// Throw an exception or do something for alert the wrong path.
throw new Exception('File with this path is not available.');
} else {
// Do your amazing stuff here
}

Related

PHP - Forcing an MP3 file download

So, I need a little help here. I have a site which hosts some mp3s. When users click on the download url, it links directly to a file called downloadmp3.php, which goes 2 parameters in the url...the php file is included below, and it's basically supposed to FORCE the user to save the mp3. (not play it in the browser or anything).
That doesnt happen. Instead, it seems like the file is WRITTEN out in ascii to the browser. It seems like it's the actual mp3 file written out.
Here is my downloadmp3.php file...please, what's wrong in this code.
It works on my local LAMP (Bitnami Wampstack on windows)....that is, on my local testing environment, it sends the file to my broswer, and I can save it. When I upload it to the real server, it basically writes out the mp3 file.
Here is the culprit file, downloadmp3.php...please help
<?php
include 'ngp.php';
$file = $_GET['songurl'];
$songid = $_GET['songid'];
increasedownloadcount($songid);
if (file_exists($file)) {
header('Content-Description: File Transfer');
header('Content-Type: audio/mpeg');
header('Content-Disposition: attachment; filename=' . basename($file));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
ob_clean();
flush();
readfile($file);
exit;
}
?>
By the way, this site only hosts mp3s - no other audio or file format. So, this downloadmp3.php script should ideally ask the user where they want to save this file.
Thanks for your help in advance.
I think the filename should be in quotes:
header('Content-Disposition: attachment; filename="' . basename($file) . '"');
Change the content-type value to text/plain. With this browser wont recognize it and wont play the file. Instead it will download the file at clients machine.
Seems there is too many headers. I am sure they do SOMETHING... but this code works.
This code works with MP3 files.... downloads to a file. Plays without a problem.
if(isset($_GET['file'])){
$file = $_GET['file'];
header('Content-type: audio/mpeg');
header('Content-Disposition: attachment; filename=".$file.'"');
readfile('path/to/your/'.$file);
exit();
}
You can access it with ajax call, or this:
<a id="dl_link" href="download.php?file=<>file-you-wish-to-download<>" target="_blank">Download this file</a>
Hopefully this is of some use

Unable to download my files from my folder using PHP

I'm currently using localhost to run my pages and currently I am trying to download the files my users have uploaded and stored in a folder called uploaded_files
this is the code for my download page which isn't working and I'm not quite sure what's wrong.
<?php
$file = $_GET['file'];
if (file_exists($file)) {
header('Content-Description: File Transfer');
header('Content-Type: application/force-download');
header("Content-Disposition: attachment; filename=\"" . basename($file) . "\";");
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($name));
ob_clean();
flush();
readfile("C:\xampp\htdocs\FYPproject\uploaded_files/".$file); //showing the path to the server where the file is to be download
exit;
} else {
echo "Download failed";
echo $file;
}
?>
file_exists() need to receive the path of the file, if the file name is "test" and it's in "uploaded_files/" so you need to check file_exists("uploaded_files/test"). You don't need to pass the whole path if your php is already in "FYPproject" folder. It's the same for downloading files, you don't need to pass the whole path for readfile().
You are passing a file path with backward slashes and forward slashes to readfile. Even if this is not the cause of your problem you should change it. Change it to this:
readfile("C:\xampp\htdocs\FYPproject\uploaded_files\".$file);
Not all browsers support application/force-download so in this case try replacing the above code with application/octet-stream and see if it works

Starting a MP4 download from a php script

So I have this small PHP condition for a PHP download, it's been working fine for weeks then all of a sudden I noticed it's stopped working.
Rather than getting a download of the file, the users are getting a 0kb file instead.
I have checked the URL and it's still working as expected.
Here is the code
if ($refer == 'stackoverflow.com' || $refer == 'dev.stackoverflow.com') {
$fichero = $downloadUrl;
if ($fichero) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($fichero));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($fichero));
ob_clean();
flush();
readfile($fichero);
exit;
} else {
die("The File $fichero does not exist");
}
} else {
die("Sorry you must start the download from the website");
}
Now I know it's hitting the conditionals, because I am not getting any of the die statements and the file is still being offered as a download. This seems to have broken in Chrome and also IE(Edge). I have been doing some reading regarding headers and the only documentation on this proble I can find that appears to be relevant is the expires setting which i set to 0.
Am I missing an easy trick here?
Thanks a lot!
header('Content-Length: ' . filesize($fichero));
Was causing the issue, I should have debugged this a little more before posting.
This file needed write in order to suppress this warning
PHP Warning: filesize(): stat failed for
When removing
header('Content-Length: ' . filesize($fichero));
The code worked as expected, I tweaked with chmod on the file and re-enabled the above code and it fixed my issue.
Thanks

Remove include file in php script

I have a php file to handle file downloads in a website. It is working as it should.
if($_GET['type'] == "pdf") {
$dir = __DIR__ . "/../src/files/pdf/" . $_GET['name'];
if(file_exists($dir)) {
header('Content-Description: File Transfer');
header('Content-Type: application/pdf');
header('Content-Disposition: attachment; filename='.basename($dir));
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($dir));
readfile($dir);
exit;
} else {
$_SESSION['err'] = "No file found";
header("location: index.php");
exit;
}
}
Now I want to count the times that a file is being downloaded. So I have a method inside a class in a different php file. I usually include this php file and create an instance of this class when I need to use some of its methods. The problem is that if I include the file in my file-handler.php the header will be different and the file would be corrupted when downloaded.
So, how can I use the method of this file in the file-handler without affecting the header?
If you include your counting file before at the top of file-hander.php file before you output your download headers then the download headers will replace whatever headers were output before.
Additionally. your counting file must not output any content at all as the client is expecting the file you are downloading and nothing else.
I agree 100% with #rjdown, your code creates a massive potential security issue. I would not put it on a server connected to an untrusted network (Like The Internet). If you would appreciate help making it secure then please ask that question.

The code is for downloading excel file(.xls)

Problem:
After download, the file doesn't contain the data.
i.e it become blank.
So please help me for this.
<?php
session_start();
include_once 'oesdb.php';
$id=$_REQUEST['id'];
if(isset($_REQUEST['id']))
{
$sql=executeQuery("SELECT * FROM file where id=$id");
$rows = mysql_fetch_array($sql);
$file =$rows['file'];
header('Content-Description: File Transfer');
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment; filename='.basename($file));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
readfile('uploads/'.$file);
exit;
}
?>
Why not create a HTACCESS file in uploads folder then states
Allow From 127.0.0.1
Deny From All
Then just create a URL, use HTML5's new download feature, do something like this:
click to download
It saves time trying to use PHP to make a download script.
try replacing this:
$file =$rows['file'];
by this:
$file = "uploads/".$rows['file'];
and this:
readfile('uploads/'.$file);
by this
readfile($file);
if still not working put the value returned by the readfile function
IMPORTANT
Please take in consideration the sql injection issues (see comment of Ondřej Mirtes)
The problem is here:
header('Content-Length: ' . filesize($file));
Content-Length receives zero value and browser downloads zero-length file, as you told him. If $file is path relative to upload/, you should do this:
header('Content-Length: ' . filesize('upload/'.$file));
Be sure that filezise() returns correct size and readfile() realy outputs it.
But the other problem is that you mentioned UPLOAD folder and using uploads. They are not same and case is important. Also, may be using relative paths in 'uploads/'.$file is not a good idea, it is better to use absolute path. For example, '/var/www/upload/'.$file.

Categories