Does fopen downloads a file i open? - php

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'.

Related

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.

Browser won't download file correcty

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.

Download while uploading

How to use PHP or any other language to read an uploading-file to allow download of the uploading-file while it is uploading?
Example sites that does this are:
http://www.filesovermiles.com/
http://host03.pipebytes.com/
Use this: http://www.php.net/manual/en/apc.configuration.php#ini.apc.rfc1867
In the array the file name is included as temp_filename - so you can pass that to your other program, which can read from the file and stream it live. The array also includes a file size so that program can make sure not to try to read beyond the end of the file.
I don't think this is possible in PHP because PHP takes care of receiving the download and only hands over control when it has the complete file. When writing CGI programs or Java servlets you read the upload from the socket so you are in control while receiving the file and you can administer if it is still uploading and how much has been received so another process could read this data and start sending what is already there.
One of the site's you've given as an example is just downloading a file from an URL or from the client computer, stores it temporarily and assigns a code to that file to make it identifiable.
After uploading, any other user who has the code can then download that file again.
This is more a question how you operate a server system then writing the code.
You can download files to the local system by making use of file_get_contents and file_put_contents.
If you want to stream file-data from the server to the browser, you can make use of readfile PHP Manual.

Allow logged in user to Download File in PHP else nobody can't

I have .mp3 files in my website and I want to set my site so that after my users have logged in they can download files. If users are not logged in they won't be able to download files. I do not want anyone to be able to find the path of the files.
I'd make the file impossible to access via an HTTP request alone, and with PHP, just print it out:
<?php
session_start();
if (isset($_SESSION['logged_in'])) {
$file = '/this/is/the/path/file.mp3';
header('Content-type: audio/mpeg');
header('Content-length: ' . filesize($file));
readfile($file);
}
?>
You can create a token based on something like the user session id and some random value. Then, the logged in user urls would be like :
/download.php?token=4782ab847313bcd
Place the MP3 files above your docroot, or if that is impossible, deny access to them with .htaccess (if using Apache).
Verify a user is logged in.
Send the appropriate headers and readfile() on the MP3 when the user requests the file.
From wordpress.stackexchange.com/a/285018
Caution: Be wary of using this PHP-driven file download technique on larger files (e.g., over 20MB in size). Why? Two reasons:
PHP has an internal memory limit. If readfile() exceeds that limit when reading the file into memory and serving it out to a visitor, your script will fail.
In addition, PHP scripts also have a time limit. If a visitor on a very slow connection takes a long time to download a larger file, the script will timeout and the user will experience a failed download attempt, or receive a partial/corrupted file.
Caution: Also be aware that PHP-driven file downloads using the readfile() technique do not support resumable byte ranges. So pausing the download, or the download being interrupted in some way, doesn't leave the user with an option to resume. They will need to start the download all over again. It is possible to support Range requests (resume) in PHP, but that is tedious.
In the long-term, my suggestion is that you start looking at a much more effective way of serving protected files, referred to as X-Sendfile in Apache, and X-Accel-Redirect in Nginx.
X-Sendfile and X-Accel-Redirect both work on the same underlying concept. Instead of asking a scripting language like PHP to pull a file into memory, simply tell the web server to do an internal redirect and serve the contents of an otherwise protected file. In short, you can do away with much of the above, and reduce the solution down to just header('X-Accel-Redirect: ...').

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