Downloading a file with a different name to the stored name - php

Is it possible to let your user download a file with a different name?
For example, there is a file called "4324ffsd34.jpg". I want people to download it via download.php, with a different name (like "filetodownload.jpg"), without renaming the original file.

Sure, use a Content-disposition header
header('Content-Disposition: attachment; filename="filetodownload.jpg"');
if you wish to provide a default filename, but not automatic download, this seems to work.
header('Content-Disposition: filename="filetodownload.jpg"');

Sure you can, just try something like this:
$original_filename = '4324ffsd34.jpg';
$new_filename = 'my_new_filename_is_detailled.jpg';
// headers to send your file
header("Content-Type: application/jpeg");
header("Content-Length: " . filesize($original_filename));
header('Content-Disposition: attachment; filename="' . $new_filename . '"');
// upload the file to the user and quit
readfile($original_filename);
exit;
Hope it helps!

There is also another way if you are using html5
Download
Cheers :3

Nothing wrong with the above but I had to add:
ob_clean();
flush();
before readfile
otherwise I can not open the download jpg/png file

Download

Related

Force download php returns okb if there are spaces in the file name

I am trying to force download an mp3 file from a server using PHP.
I am using absolute paths for example my-domain.../my-file.mp3
This is the code that I am using to download the file.
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=". $_GET['name']);
$path = urldecode($_GET['path']);
if(isset($path))readfile($path);
If the file path has some spaces in it, it downloads a 0kb file and I have no idea why, I need help with this, so if the file path is my-domain.../my-file.mp3 it works great. If it is like this my-domain.../my file.mp3 it does download the file but it has 0kb.
I have tried everything I could think of but I can't figure it out.
Thank you.
If the filename contains spaces, you should output the name in qoutes like this:
header('Content-Disposition: attachment; filename="'. $_GET['name'] . '"');

Php File Size header()

$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);

Change file header name in php [duplicate]

Is it possible to let your user download a file with a different name?
For example, there is a file called "4324ffsd34.jpg". I want people to download it via download.php, with a different name (like "filetodownload.jpg"), without renaming the original file.
Sure, use a Content-disposition header
header('Content-Disposition: attachment; filename="filetodownload.jpg"');
if you wish to provide a default filename, but not automatic download, this seems to work.
header('Content-Disposition: filename="filetodownload.jpg"');
Sure you can, just try something like this:
$original_filename = '4324ffsd34.jpg';
$new_filename = 'my_new_filename_is_detailled.jpg';
// headers to send your file
header("Content-Type: application/jpeg");
header("Content-Length: " . filesize($original_filename));
header('Content-Disposition: attachment; filename="' . $new_filename . '"');
// upload the file to the user and quit
readfile($original_filename);
exit;
Hope it helps!
There is also another way if you are using html5
Download
Cheers :3
Nothing wrong with the above but I had to add:
ob_clean();
flush();
before readfile
otherwise I can not open the download jpg/png file
Download

connect ftp account with file_get_content function

I have an FTP account. I want to connect to FTP with file_get_contents. Is this possible? If not, how can I do it?
I want to do this for an upload script similar to hotfile or rapidshare.
file_get_contents('ftp://username:password#host.com/');
If I understand what you want to do from your comments on #genesis' answer, what you need to do is this:
$file = file_get_contents("derpost:pass**#ftp1.freakshare.com/Freez%20Screen%20Vide‌​o%20Capture.lnk");
header('Content-Disposition: attachment; filename="image.jpg"'); // Change file name as appropriate
header('Content-Length: '.strlen($file));
header('Content-Type: image/jpeg'); // Change this MIME type as appropriate
echo $file;

PHP File Download

I'm currently building a script that will allow a user to download a file via a URL without actually seeing the filename or where the file is stored. So far I have everything built out, but I need to know how I would go about calling the file to open and download. I currently have a working version (code below), but for some reason the PHP is corrupting the download. Everytime I try to open a file that downloads to my desktop I get a corrupt error message. When I open the same file on the server itself, the file works just fine.
URL Structure:
http://www.example.com/download/file/cjVQv0ng0zr2
Code that initiates the download
$fullpath = BASE_PATH . '../uploads/brochures/' . $vendors['0']['filename'];
header("Content-type: application/pdf");
header('Content-disposition: attachment; filename="' . $fullpath . '"');
Am I doing something wrong that would cause the file to become corrupt? Am I missing a header or two?
Thanks in advance,
Jake
You need to call the following line after sending the header.
readfile($fullpath);
and also adjust in the header like this:
header('Content-disposition: attachment; filename="' . basename($fullpath) . '"');
One thing i am not sure about is the $fullpath .. try to see if the $fullpath you have is correct and you can actually reach the file, this needs to be the full physical path of the file.
I think it would also be a good idea to add the following header as well:
header("Content-Transfer-Encoding: binary");
I had a similar issue a while back. Make sure you don't have any extra whitespace in your script file, either before the "<?php" tag or after the "?>" tag. In my case the last character of my script was "\n" instead of the expected ">".
I had faced the same problem sometime back, following worked for me; put a
while( #ob_end_clean() );
just before header functions:
header("Content-Type: ". $row['p_mime']);
header("Content-Length: ". $row['p_size']);
header("Content-Disposition: inline; filename=".$row["p_name"]);
Content-disposition: attachment/inline has to be set according to cases (1. prompt for download / 2. open in browser)
NOTE: Take care that you are not echoing and value before the header function, and being over cautious will not do any harm, silent out all the function before header function which you think would fail or spawn a warning message prefixing "#" symbol to those lines of php code.
all the best :)
Make sure you exit...
(i'm using a blob)
header("Content-Type: " . $response['content_type'] );
header("Cache-Control: maxage=1");
header("Pragma: public"); //fixes ie bug
echo trim($_data);
exit();

Categories