Browser won't download file correcty - php

I've looked through the site and can't find an answer to my question. I'm trying to make a winrar file (which is 1GB) downloadable from my server and whenever I try, it gives me a winrar file with the same name that is only like 9kb. Here's what I have for the headers. I'm really new to downloadable content so, don't make fun of me. :D
header('Content-Disposition:attachment; filename="java.rar" ');
I'm assuming that I need more. hahaha!
I keep seeing people use header('Content-type: application/pdf'); above their disposition. Does this support rar format, or do I need to use it as zip?

Probably your script is timing out and only a small part from 1GB file is being sent. You may try increasing time limit but IMO for such a big file you'll have to link directly to make it downloadable.
If you want to count file downloads you may link to a php script which will increase the counter and redirect browser directly to the file afterwards.

Related

Does fopen downloads a file i open?

Im using fopen to let users download a audio in the code below as the code attribute doesn't always works in all situations and browsers.
Does this downloads the file to my server temporarily or lets the user download it from the external sourced
<?php
$file=fopen('link','r');
header("Content-Type:audio/mp4");
header("Content-Disposition: attachment; filename='example.m4a' ");
fpassthru($file);
?>
All the data will be read (from wherever the file handle points) by your PHP program on your server. That may involve copying data from a remote URL to your server. That data may exist entirely in RAM. It may hit swap space on the disk.
The PHP program then outputs it to the browser.
The browser never has direct access to 'link'.

Cannot download using fpassthru

I'm trying to serve a file for download to a user, and I'm having trouble with fpassthru. The function I'm using to download a file is:
http://pastebin.com/eXDpgUqq
Note that the file is successfully created from a blob, and is in fact the file I want the user to download. The script exits successfully, and reports no errors, but the file is not downloaded. I can't for the life of me think what's wrong.
EDIT: I removed the error suppression from fopen(), but it still reports no error. Somehow the data in the output buffer is never being told to be downloaded by the browser.
I tried your code (without the blob part), and it worked fine. I can download a binary file. Based on my experience, here are something to check:
Has the file been completely saved before you initiate the reading? Check the return value of file_put_contents.
How large is the file? fpassthru reads the whole file into memory. If the file is too large, memory might be insufficient. Please refer to http://board.phpbuilder.com/showthread.php?10330609-RESOLVED-php-driven-file-download-using-fpassthru for more information.
Instead of downloading the file to local server (reading the whole file into server’s memory, and letting the client download the file from the server), you can create an SAS URL, and simply redirect the browser to the URL. Azure will take care of download automatically. You many want to refer to http://blogs.msdn.com/b/azureossds/archive/2015/05/12/generating-shared-access-signature-sas-using-php.aspx for a sample.
I was able to download the file by passing a stream obtained with the Azure API directly to fpassthru, without creating a file. Unfortunately, I can't show the code because it belongs to a project that I have finished working on and the code is no longer available to me.

PHP mime type not working for MP3

I am having an issue when using mime types in PHP.
As a security feature, I am placing any content uploaded to my server by users in a directory outside of public_html. When a user wants to access their uploaded files in the browser, I use php script to pull the file from the secure folder using readfile and header('content-type'), the issue I am having so far only pertains to mp3s.
When I try and use this for a mp3 file, the file doesn't actually play in the browser, but M4As work fine. And it isn't every mp3 file too, it just seems that when I get the mime-type of some mp3 files php can't tell that they are mp3s. The mp3s also playback fine if I download them, so it isn't an issue with the file per say (unless the file has the wrong mime-type somehow).
How do I get it to tell that they ARE mp3s?
My getFile code is here: codepad.org
Any help would be appreciated greatly, I'll even give you some of my tracking cookies!
For mp3s, did you try:
header("Content-Type: audio/mpeg");

How to use google docs viewer for embedding ppt files on a webpage, if the file is served by a php script?

I've uploaded an MS powerpoint file to my server, and I'm trying to use Google Docs viewer (http://docs.google.com/viewer) to display it on a webpage.
The file is available here:
http://elgg.wamped.org/test.ppt
If I invoke the docs viewer with the above URL, it is working as expected, see:
http://docs.google.com/viewer?url=http%3A%2F%2Felgg.wamped.org%2Ftest.ppt
But when I'm trying to serve the same file through a very simple php script to the viewer, it fails rendering with the not too helpful error message: "Sorry, we are unable to generate a view of the document at this time", see:
http://docs.google.com/viewer?url=http%3A%2F%2Felgg.wamped.org%2Freadfile.php
The script that serves the file is as follows:
<?php
header('Content-type: application/vnd.ms-powerpoint');
header('Content-Disposition: attachment; filename="test.ppt"');
readfile('test.ppt');
?>
I've tried playing around with various header fields like Pragma and Cache-Control, but nothing helped. I also tried slicing the output file and echoing in chunks, that also did not do any good. Checked the apache log on the server, checked the response headers, everything seems fine to me.
Any ideas what am I doing wrong here?
EDIT: Although I haven't found a solution to this issue, I stumbled upon a site that does the same (or seems to do more than that, actually) as Google Docs Viewer. http://embedit.in has support for a wide range of file types, has an API, and does the job nicely, so I'll just probably go with that one. However, out of curiousity, I'd still like to know what is wrong with the piece of code below. So any suggestions are more than welcome.
Have you tried naming the php file with a .ppt file extension instead of .php? Whether or not your server will process php code in a file that doesn't have a .php file extension is another problem. But Google Docs may simply say NO to loading any file with a .php extension on it.
Capitalize the T in Content-Type. I also provided the Content-Length header. My filehandler works now.
It looks like you might be missing some headers. You might also want to see if the file is in the same directory as readfile.php, because google might store each file on a remote server.

PHP: prompting download from ftp?

hy guys,
i really need your help. i've succesfully connected to ftp server via php.
i'm listing all files that are on the server. if i click a file the browser should prompt a download window to download the file.
i've absolutely no idea how to do that. which method am i going to use. ftp_get kind of confuses me. it says i have to declare a local_file as well. i just want a file on the server to download to my harddrive.
how can i do that?
regards matt
The remote file has to first be downloaded to your server before you can send it to the user. It's invisible to the user, but you don't have a choice. PHP won't let the browser talk directly to the FTP server.
Create a separate php script that calls ftp_get for a specific file, stores it temporarily to your server to allow the user to download it.
Something like:
<?php
//assume the page was called like download.php?filename=downloaded.pdf
header('Content-Disposition: attachment; filename="'.$_GET['filename'].'"');
$tempFile = 'temp'.rand();
ftp_get($ftp, $tempFile, $_GET['filename'], FTP_BINARY);
readfile($tempFile);
You may add code to delete the tempFile too.
If you provide a link to a file that can't be read by the browser (such as a php file, audio, video, etc.) it will ask you to download the file.
The other way is to use PHP headers on a page and print out the page, and link to that page. http://www.ryboe.com/tutorials/php-headers-force-download

Categories