hello
i have tryed a lot of source codes for this but i have the same error in all of them
include 'functii.php';
starts();
opendb();
$query = "SELECT content,`title_real`,`size`,`ext` FROM file WHERE file_id = '3'";
$result = mysql_query($query) or die('Error, query failed');
list($content,$filename,$size,$ext) = mysql_fetch_array($result);
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header("Content-Disposition: attachment; filename=$filename.$ext");
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".$size);
echo $content;
exit;
the problem is that in the downloaded file i have a lot of "\0" that came from nowhere.
the file is well stored in the database. i tested that.
thank you
It sounds like an encoding issue. It could be that the file is encoded in UTF-16 and you are displaying it as if it were ASCII.
Try to:
header("Content-Disposition: attachment; filename = $filename . '.' . $ext");
instead of:
header("Content-Disposition: attachment; filename=$filename.$ext");
3 things :
As Alexander.Plutov said, you got an error in the filename, it should be $filename.'.'.$ext You're missing the "." between the filename and its extension.
As Mark Byers said, it may seem like an encoding issue, you should check your PHP file encoding as well as your DB's one.
Hope you're only stocking text based file, because, any other file content definitely shouldn't be stored in a database! And even for text based file, it shouldn't be used for that, it's not its purpose at all.
In any other case than file generation, files should be stored in your server and may be called from your DB thanks to file path and name.
Related
$file_name = $_GET['name'];
$file_url = $_GET['file-url'] . $file_name;
header('Content-Type: application/octet-stream');
header("Content-Transfer-Encoding: Binary");
header("Content-disposition: attachment; filename=\"".$file_name."\"");
readfile($file_url);
exit;
I'm using this code to download files in my site fetching from another web servers.
It works if my url looks like this:-
https://www.example.com/file_download.php?name=video_song.mp4&file-url=http://www.googlevideo.com/video/play/221589
So, it starts downloading by fetching the file from http://www.googlevideo.com/video/play/221589 in my site. Now, though it downloads the file correctly, it does allow the downloader to see the actual size of the file. So, downloaders having problems with it (e.g. Time Remaining, Download Percentage etc.).
So what header should I use to solve this thing? Please explain it by coding.
You may try this:
header("Content-Length: " . $filesize);
The end goal is for the user to download a .csv file. Right now I'm just testing trying to download a simple text file: test.txt. The only thing in this file is the word "test".
Here is the HTML code for files_to_download.php
Test file: <a href='test.php?file=test.txt'>Test.txt</a>
Code for test.php:
if(!(empty($_GET["file"])))
{
$file_name = $_GET["file"];
$path = "path/to/file";
$fullPath = $path . $file_name;
if(ini_get('zlib.output_compression'))
ini_set('zlib.output_compression', 'Off');
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Disposition: inline; attachment; filename=\"$file_name\"");
header("Content-Type: text/plain");
header("Content-Transfer-Encoding: binary");
readfile($fullPath);
}
I've tried variations of the headers above, adding more and removing others. The above seem to be the most common recommended on this site.
I've also tried changing
header("Content-Type: text/plain");
to
header("Content-Type: text/csv");
and get same results: empty .txt or .csv file.
The files are not empty when I open them directly (file browser) from the server. I've checked the permissions of the files and they're both 644, so the entire world can at least read the files. The directory is 777.
Is there a configuration on the Apache server I need to specify that may not be or am I missing something above.
Thanks for looking!
In most cases the path is wrong
Read the text file, then echo the text out after your header() calls.
Here's how I have my csv download set up:
//downloads an export of the user DB
$csv = User::exportUsers();
header('Content-disposition: attachment; filename=userdb.csv');
header('Content-type: text/csv');
echo $csv;
Where exportUsers() creates the csv data. You can easily just replace $csv with the contents of your text file, then echo it out.
And as far as your text file, you can use file_get_contents() to get the contents of your file into a string. Then echo that string.
Try setting the content length of the file:
header('Content-Length: ' . filesize($file));
Also, please have this in mind: file inclusion
In my case, the path was correct. And the download-forcing was working on windows, but not mac.
I figured out after few tests that the header Content-Length was failing. I was using the function filesize on a full url, like :
$url_my_file = "http://my-website.com/folders/file.ext";
header('Content-Length: '.(filesize($url_my_file)));
I replace it by
$url_my_file = "http://my-website.com/folders/file.ext";
$headers = get_headers($url_my_file, 1);
header('Content-Length: '.($headers['Content-Length']));
And ... It's working now :)
I've learned how to create CSV files from MySQL data from another StackOverflow question. My problem is, for some reason when I call this code, it tries to save a file called index.php (which is the current page). Inside the index.php file my data from the table is there, separated by commas. I'm guessing I have a small typo somewhere, but after playing with the code I cannot find it. Thanks to anyone who can help.
$result=mysql_query("SELECT * from tbl_email");
if(mysql_num_rows($result)) {
header ("Content-type: application/csv Content-Disposition:\"inline; filename=messages.csv\"");
echo "REF #,Company,Name,Email,Message,Date\n";
while($row = mysql_fetch_row($result)) {
$companyname = mysql_query("SELECT company FROM tbl_users WHERE user_id ='$row[1]'");
$datname = mysql_fetch_array($companyname);
echo"$row[7],$datname[company],$row[2],$row[4],$row[5],$row[6]\n";
}
die();
}
You need multiple header() calls rather than one call which supplies multiple headers on a single line, and I believe the most appropriate mime type for a CSV is text/csv.
header("Content-type: text/csv");
header("Content-Disposition: inline; filename=messages.csv");
And more commonly, we would use Content-Disposition: attachment to force a download.
header("Content-type: text/csv");
header("Content-Disposition: attachment; filename=messages.csv");
It should be:
header('Content-type: application/csv');
header('Content-Disposition: attachment; filename="messages.csv"');
Notice that the value for filename is not encased correctly with double quotes. Try to use single quotes in php, this will save you alot of trouble. ;)
Have a look at http://www.techcoil.com/blog/php-codes-to-tell-browsers-to-open-the-download-dialog-box-for-users-to-download-a-file/ to learn more about telling browser to download your file.
This has been posted, but I've tried lot of solutions found on SO and more (like this: http://davidwalsh.name/php-force-download)
I basically have this:
{
$filePath = '../public/myfile.png';
$fileName = basename($filePath);
$fileSize = filesize($filePath);
if (!is_file) {
die("File not found");
} else {
header("Content-Description: File Transfer");
header('Content-Type: application/octet-stream');
header("Content-Disposition: attachment; filename= " . $fileName);
header("Content-Transfer-Encoding: binary");
readfile($filePath);
}
}
Files are recognized and downloaded, but .PNGs are empty and .DOCs are corrupted (and Word asks me to fix the file, then it's ok). I have tried also PDFs, and no problem with that.
I trues to put all sort of options (Pragma, Cache-Control, Expires, Content-Length, etc.), still downloaded files but corrupted in some way...
Did you ever had my problem? Please consider I'm on IIS 7.5
Thanks in advance
Open the downloaded files with a plain text editor like Notepad++. At the top you will find a PHP Error notice, it will tell you what's going wrong.
The error is probably "session already send". Then add ob_start(); at the beginning of your script.
I want to allow a user to download a pdf file, the download code is below....for some odd reason even though the file is being downloaded I get an error saying that the file has been damaged on the server...Could someone help me and point out where I am making my mistake.
<php
$name = $_POST["name_first"];
$mail = $_POST['email'];
$number = $_POST['phone_number'];
$email_message = "first name: {$name} email is {$mail} number is {$number} ";
mail('fanaa#gmail.com', 'Form Response', $email_message);
if ($mail == "" OR $name == "" OR $number == "")
{
echo "Enter valid details ";
}
else
{
header('Content-type: application/pdf');
header('Content-Disposition: attachment; filename="tokina.pdf"');
readfile('docs/tokina.pdf');
}
?>
I used this code to download pdfs:
header ("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header('Content-Type: application/octetstream');
header("Content-Transfer-Encoding: Binary");
header("Content-length: ".filesize($file));
header("Content-disposition: attachment; filename=\"".basename($filename)."\"");
readfile("$file");
}
This should be fine, and make sure there are no spaces or return characters (don't escape php at all is the best solution).
If you find your still having problems, open the corrupted file with notepad (there may be a php error warning inside).
Hope this helps!
Remove the headers and look at the page, do you see any error messages? If PHP outputs anything else than the actual PDF source, the file will appear to be corrupted.
header('Content-type: application/pdf');
enable PHP extension php_gettext and you are done.
try taking out the double quotes in
header('Content-type: "application/octet-stream"');
so it becomes
header('Content-type: application/octet-stream');
Maybe your content-type is not correct. try this one:
header('Content-type: application/pdf');
header('Content-Disposition: attachment; filename="downloaded.pdf"');
readfile('original.pdf');
Your PDF file tokina.pdf is either not uploaded or not in the same directory as the PHP file. That's why it's saving as "tokina.pdf.htm" - it's loading the HTML for a 404 page instead. That is why your browser/PDF viewer thinks the file is "corrupted" - because its extension is PDF but its contents are not.
Make sure the file is uploaded, and if it is, make sure readfile is pointing to the correct path. If it's not in the same folder, use a relative/absolute path, for example:
readfile('docs/tokina.pdf');
And yes, the content type should be application/pdf
Using this script
header('Content-Type: application/force-download');
header('Content-Disposition: attachment; filename='.$filename);
header('Content-Transfer-Encoding: binary');
header('Content-Length: '.filesize($filenamepath));
readfile($filenamepath);
I had the same problem. Comparing the original file and the downloaded file with a hexadecimal editor like UltraEdit, I found some characters at the beginning of the corrupted file.
The problem was that after ?> marking end of PHP code there were line terminators several times in my code.
Remove all the line terminators after ?> and read also the forum article Downloaded Files are corrupt - Common Problem. That worked for me.
I hope that can help you.
I use
$download_path = your path (where to look for the files)
set_time_limit(0);
$file_url = $download_path . $data['link'];
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="' . basename($file_url). '"');
//then to read the file
readfile($file_url);
this usually works for me