music buffering stops while force download box opens - php

I am working on social networking site which has grooveshark integration for music and image sharing like Facebook. This is full ajaxify site. So in this site one can listen music and other side he can do the other things like he can share status , upload images , shares link like facebook. When user open any image there is button to download it.My problem is that when i click on download button to download image while music playing, it makes (if i see in the network by inspect element) streaming fails with the partial content (http code 206) as well as the call for download shows canceled but returns the http code 200ok(in the network by inspect element).
here is code:
if(file_exists($media_name))
$path='../webroot/img/user_media/original_uploaded_size/'.$user_id;
else
$path='../webroot/img/user_media/single_photo_878_549/'.$user_id;
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($media_name));
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($path));
ob_clean();
flush();
readfile($path);
please help.
If someone not understands please comment i will tell you about my problem...

Are you using sessions? Try using session_write_close(); at the end of your request. This will basically finalize the request.

Related

How to download a very large video file instead of playing in browser with PHP

I have created a movie which I have saved as an MP4 file and uploaded to my server. The file is 4.6 GB. When I have tried to send a link to my family, the video tries to play in their browser.
What I want to do is have them click a link and the file downloads to their computer instead. I have searched endlessly for solutions, but they all keep failing, probably due to the size of the file.
Is anyone able to help with a PHP script that will allow the downloading of such a large file?
Thanks
The easiest solution is to press Ctrl+S, select File>Save or do right click + Save as in the browser when the file starts to load - this will open the Save File dialog.
In case you want to return this file from PHP, you can do that with following script:
<?php
$file = 'video.mp4';
if (file_exists($file)) {
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($file));
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
readfile($file);
exit;
}

PHP download file: show progress in browser

User on my site can download files. Sometimes these files are pretty large and I would like users to see download progress bar at their browsers.
I use the following code to give user a file:
header('Content-Type: text/xml');
header('Content-Disposition: attachment; filename='.$fileName);
header('Pragma: no-cache');
header('Content-Transfer-Encoding: binary');
header('Pragma: public');
header('Content-Length: '.filesize($fullFileName));
header("Content-Description: File Transfer");
$fileHandler = fopen($fullFileName, 'r');
while(!feof($fileHandler)){
echo fread($fileHandler, 2048);
}
fclose($fileHandler);
When I run this script browser(FireFox) freezes for some time, I can see loading colour circle and only after save/open dialog file appears. When I click on "save" button, file almost immediately is downloaded to my computer(while file is quite large - 50 Mb).
I want to have downloading system like on this site. Dialog for save/opean appears immediately when you click on any links. And after you can see downloading progress in your browser.
Are there any special headers to display progress bar in browser? How I should change my code?
I use the code below, and in Firefox it does give me the download time and progress:
// send headers first
header('Content-type: application/octet-stream');
header('Content-Transfer-Encoding: binary');
header('Content-disposition: attachment; filename='.$FileName);
header("Content-Length: ".filesize($Path));
// then use an easy way to flush and end all output buffers
while (#ob_end_flush());
// flush file
readfile($Path);
It should work if you specify Content-Length correctly. The only thing I find mildly weird in your code is the Content-Type: text/xml header. Note that I use Content-type: application/octet-stream.

Second sequential function stomps the first function

I guess I have to break down and ask for help. (Should have done it 3 days ago!)
Here's what happens...
PHP reads session & post variables, builds a .csv file from a mysql query.
it attempts to open a 'Save As' dialog box and when that's done, jump to another page.
I'm using nested functions but when run, the dialog box seems to get run over and never appears.
separately the functions work fine.
when run, the 'save as' dialog box doesn't wait for user input
Can anyone see what I've done wrong or can you redirect my thinking?
$filename points to the created CSV file on the server
$suggname is a default filename users should see in the dialog box.
The code:
holdit($filename,$suggname);
function holdit($filename,$suggname) {
$fp=#fopen($filename, 'rb');
if (strstr($_SERVER['HTTP_USER_AGENT'], "MSIE")) {
header('Content-Type: "application/octet-stream"');
header('Content-Disposition: attachment; filename="'.$suggname.'"' );
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header("Content-Transfer-Encoding: binary");
header('Pragma: public');
header("Content-Length: ".filesize($filename));
} else {
header('Content-Type: "application/octet-stream"');
header('Content-Disposition: attachment; filename="'.$suggname.'"' );
header("Content-Transfer-Encoding: binary");
header('Expires: 0');
header('Pragma: no-cache');
header("Content-Length: ".filesize($filename));
}
fpassthru($fp);
fclose($fp);
jump();
}
function jump() {
header('Location: return_from_csv.php');
}
You are adding lots of headers to your HTTP response. One of those is Location which instructs the browser to redirect. Obviously it is interpreting that as a higher priority than your other headers.
Decide if you want to redirect or serve a file in your response and do one or the other.
I suspect you have misunderstood the Location header. Read this: http://en.wikipedia.org/wiki/HTTP_location
By the looks of things you are trying to serve the CSV file and then redirect to another page. Sorry, you cannot do this. An HTTP response does one thing and one thing only. You might consider opening your link to the CSV file in another window using the target attribute of <a>.

Determining successful download using php readfile

I need to know if a user selected download then clicked the cancel button, which is not the same as readfile having an error. I have inspected the count returned by the readfile function, but it shows the bytes in the file even if the user canceled the download from the Save As dialog.
The reason this is needed is because my site has a one-time download, where a member gives permission for another use to download their file one time, then the permission goes away. But if a member clicks the download button then decides not to download it right then, I dont' want my database to get updated to show they got the file.
This deals with intellectual property protection since the files are the property of the member who uploaded them, and I need to keep an audit trail of exactly what other members downloaded the file in case they start floating around the internet. But if the readfile function always reflects the filesize (meaning those bytes were transferred in some way), I have no way to know if the file was actually downloaded.
I have seen a number of posts about this subject, but no real solutions to what has to be a frequent need - did they download it or not? Just knowing that they clicked the download button doesn't really say whether they decided to go through with it since the Save As dialog box allows someone to cancel the actual completion of the download.
For completeness, here is my download code up until the readfile function:
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header("Content-Disposition: attachment; filename=$download_name");
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("sub/$doc_file"));
ob_clean();
flush();
$wasdownloaded = readfile("sub/$doc_file");
I fear the correct answer is "Impossible" - let me explain: You might be able to correctly figure out, when the file has crossed the wire, but you can't figure out reliably, whether the client threw it away or not.
Example (chronological sequence):
A user on MSIE clicks download and is presented with the "Save where" Dialog.
While this dialog is open, the download is started in the background.
The user navigates around in the dialog or simply does nothing (phone rang, he talks)
The background download is finished, your script sees the download as complete
The user clicks on "cancel"
MSIE deletes the tempfile, the download is never stored in a user-accessible form
Result:
The user sees the file as "not downloaded" - and he is correct
Your app sees the file as "correctly downloaded" - and it is correct
You would first need ignore_user_abort().
This would allow your script to continue on after the user has hit cancel, or escape.
You would then have to print out the file and continuously check with connection_aborted().
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header("Content-Disposition: attachment; filename=$download_name");
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("sub/$doc_file"));
ob_clean();
flush();
$fp=fopen("sub/$doc_file","rb");
while(!feof($fp))
{
print(fread($fp,1024*8));
flush();
ob_flush();
if( connection_aborted() )
{
//do code for handling aborts
}
}
Use this comment on php.net : http://www.php.net/manual/en/function.fread.php#72716
On fclose you would be able to determine if file has been downloaded successful, because your are checking if user aborted connection with connection_status()

Prompt user to download PDF file instead of opening

In my project site, if I click on a link, the PDF opens in a new or parent window. Well I want a box to appear that prompts the user to download the file instead of opening it.
Does anyone know of a simple JavaScript onClick event that will do this, in all browsers, with default settings?
My server is PHP based.
Since your edit states that you're using PHP, here's how to do the same in PHP:
<?php
header('Content-type: application/pdf');
header('Content-Disposition: attachment; filename="downloaded.pdf"');
readfile('original.pdf');
?>
Since you've tagged it .NET, I'd say this is your best solution:
Response.ClearContent();
Response.ClearHeaders();
Response.ContentType = "application/pdf";
Response.AddHeader("Content-Disposition", "attachment;filename=download.pdf");
Response.WriteFile(Server.MapPath("~/files/myFile.pdf"));
Response.Flush();
Response.Close();
Change the Content-Type to application/octet-stream. You may find however, that some browsers will infer from the file extension that it should open as a PDF with your favorite PDF viewer.
Response.ContentType = "application/octet-stream";
Also, set the following:
Response.AppendHeader( "content-disposition", "attachment; filename=" + name );
You can't do it via javascript, you need server side implementation.
Here's the SO Post which should help:
Allowing user to download from my site through Response.WriteFile()
http://aspalliance.com/259_Downloading_Files__Forcing_the_File_Download_Dialog
If you are getting a corrupted file error try this:
header('Pragma: public');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Cache-Control: private', false);
header('Content-Type: application/pdf');
header('Content-disposition: attachment; filename=' . basename($file));
header("Content-Transfer-Encoding: binary");
header('Content-Length: ' . filesize($file)); // provide file size
header('Connection: close');
readfile($file);
Where $file is the full path or url of the file.

Categories