PHP generate file, download and unlink in mozilla doesnt work - php

I have problem with mozilla firefox in this case. I am generating file on the fly and then trying to download the file and then delete it from the server. Problem is, Firefox says the file doesnt exists, even though I can see it is generated on the server. Chrome and Opera works perfectly.
firefox version: 58.0.2
Any ides what has to be tuned for firefox ?
header('location: /path/'.$name.'.pptx');
header('Content-Disposition: attachment; filename="' .basename($name).'.pptx"');
ob_start();
flush();
sleep(5);
unlink($name.'.pptx');

First of all, just for sanity, please use Location instead of location.
Also, as per my understanding, once you send the Location header, the browser does a redirect to the specified URL. When the browser has redirected to a different page, I feel all the headers post Location shall be rendered useless. The URL from which you are downloading should send the Content-Disposition header. Also, I would suggest adding the headers Content-Type and Content-Length on the download URL.

Related

Empty lightbox when pdf confugered save file in firefox

I am using joomla 2.5 with ROXBOX plugin and using this showing the PDF's in lightbox. I am facing problem when user configured Firefox auto download PDF files.
When Firefox configured as save PDF instead of open it in browser the light box stays blank and file started download. As we can not have control on browser, is there any way show any message when Firefox auto download for PDF is enabled?
Please Help!!
I assume you want the PDF to display in the browser, rather than forcing a download. If that is the case, try setting the Content-Disposition header with a value of inline. and 'Content-type to application/pdf.
header('Content-type: application/pdf');
header('Content-Disposition: inline; filename="the.pdf"');
You can use PHP to set header as shown above.

Hide url download by php header

I have a link of another domain (ex : http://domain2.com/file.zip ). Can i ask how to hide this link and force download. I try but can't hide this file url;
header('Content-Description: File Transfer');
header("Location: $datas", true, 303);
You will need to look into something like readfile. See example one for the code to set it up with. You will probably need to change the content-type header to be: header('Content-Type: application/octet-stream'); as an fyi.
EDIT
Also, since that will be reading from your other domain, it will download to your server to the user's computer, so it will be "dupping" bandwidth so to speak. If you have access to the other domain / it is on the same server, it would probably be better to read that file locally, or setup a similar download script at domain2, so you just redirect them to that download script for domain2 instead of the file.

Forcing to Download A File

I'm developing a web service. With this service, user's will upload their .php files, and service will remove UTF8 BOM characters from php file. And then, There will be a link like this :
Download Your File
But when i click this link, browser browsing to this file. I don't want browse it, i want to download it. So , when user click this link, downloading will start.
Any ideas ?
(P.S. I don't want modify uploadedfile.php file, also i read 5 questions about this, but still i have problem.)
You need to supply this HTTP header:
Content-Disposition: attachment; filename=example.txt
You can usually specify this for entire directories at a time by configuring your web server appropriately. If you mention which web server you are using, somebody may be able to suggest how to do this.
The problem is that you're allowing people to upload PHP files on your server, then giving them a link to execute that PHP file. The web server is automatically treating those uploaded PHP files like any other PHP file, i.e. executing it, which opens you up to a massive security hole.
Whatever purpose your web service has, I'd suggest renaming the file on your server when it is uploaded (something 'random' is best, without an extension), then having a PHP script feed it back out with the appropriate headers set when it is requested.
The URL for such a script would look like:
http://www.example.com/get_uploaded_file.php?id=jgh3h8gjdj2389
It would link the value in id with the file on the server, and if you've saved the original filename somewhere (flat file, DB), you can serve it out using its original name, so long as you set the right HTTP headers.
Linking directly to the PHP file may end up executing it. One way is (like somebody above suggested) to rename it. Or, you can have a downloader.php which does below:
<?php
header('Cache-Control: no-cache, must-revalidate');
header('Expires: Mon, 01 Jan 2000 01:00:00 GMT'); // some date in past
header('Content-type: text/plain');
header('Content-Disposition: attachment; filename='.basename($filepath));
header('Content-Length: ' . filesize($filepath));
flush(); // or any other flush function/mechanism you use.
readfile($filepath);
and link it something like:
Download
This method will let you retain the .php extension. Also, if the PHP file is big and connection is slow, they progress-bar would be accurate (because you've flushed the content length upfront.

What HTTP headers do I need to stream an ASF file?

I have a simple PHP script that will either serve up a streaming ASF file or not, depending on whether you're logged in and have access to the file. It basically does this:
<?php
header('Content-Type: video/x-ms-asf');
header('Content-Disposition: inline; filename="file.asf"');
readfile('file.asf');
?>
This already works fine in Firefox; when you navigate to this file it starts the video streaming right away. But Internet Explorer is a different story. When I go to this link in IE, it consistently tries to download the file as if it were an attachment rather than streaming it in the browser. What I am missing that IE's getting hung up on?
I don't think you need to add the Content-Disposition. Did you try removing it?
It look like that your IE or Media Player isn't correctly configured. Check this link link text for more details.
From your description, it sounds like your IE is configured to save a .asf file rather than stream it. Perhaps you can try opening a similar file on your local drive in IE to check whether it's configured to stream it.

Renaming File on another server as user downloads it [2] - using PHP

I have asked this question today already but this time I want to know if I can achieve this via PHP since Javascript wasn't up to it.
I have a link to a file on another server. If i provide this link to my users the headers are pushed out to download that file from that server.
Is there a way for me to capture those headers and file and redirect the download to the user? I would like to do this so that I can change the filename of the download since it is
always 'file.zip'.
Is this possible with PHP?
Thank you for any help.
You can download the file to your server using curl and serve it correctly(with a Content-Disposition header). As long as you are using HTTP, there's no way to send just the header and let another server stream the content directly to the client.
You could do this, and you can do it in several ways.
1) (simple) copy the file to your server, and rename it. Point your download links to this copy.
2) (harder) Create a stub php file, called , read the file from the remote server within php, and stream the content to the script output. This will need you to set appropriate headers, etc. as well as setting up your webserver to parse through PHP.
Seriously, I'd go with option 1. (assumes you have a legal right to serve the content, etc.)
Maybe you can use a script similar to the following one:
<?php
header("HTTP/1.1 301 Moved Permanently");
header("Location: http://www.example.com/the_path/file.zip");
header('Content-Disposition: attachment; filename="alternate_filename.zip"');
exit();
?>

Categories