PHP Download Files from another Server using File URL - php

Right now, if I paste the same URL in Chrome's URL Bar, it downloads the file accurately.
But when I add the same URL inside ANCHOR TAG it just reloads the same page. I have tried both _self and _blank values for TARGET Attribute.
Here are the Anchor tags I used:
Download File
Download File
Download File
NOTE: These anchors are working fine in Microsoft Edge
Also, I tried the different PHP scripts for this and one of them downloads the file but the file is empty. Changing URLs for different types of files including pdf, jpg, png and all of them has filesize of zero.
$file_name = basename($vpf_url);
$fn = file_put_contents($file_name,file_get_contents($vpf_url));
header("Expires: 0");
header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
header("Content-type: application/octet-stream");
header('Content-length: '.filesize($file_name));
header('Content-disposition: attachment; filename="'.basename($file_name).'"');
readfile($file_name);
I don't know if its a cross-domain issue or just some error that's downloading an empty file.

Related

downloaded files going to my root directory

I am currently building a video downloading tool using PHP. The videos are downloading without any issue by selecting the path you want but I always get a copy of the video in my root directory.
I am using the following function to download the files:
function df($urlFile){
$file_name = basename("download.png");
//save the file by using base name
$fn = file_put_contents($file_name,file_get_contents($urlFile));
header("Expires: 0");
header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
header("Content-type: image/jpg");
header('Content-length: '.filesize($file_name));
header('Content-disposition: attachment; filename="'.basename($file_name).'"');
readfile($file_name);
}
df($main_url); `
df($main_url);
Can you please help to avoid the files from being saved in my directory?
I only want the videos to download normally in the path the users want and not a copy in the root directory
This line is doing the saving:
$fn = file_put_contents($file_name,file_get_contents($urlFile));
The file_get_contents($urlFile) part is loading the file and you could simply assign it to a variable, like this:
$myContent = file_get_contents($urlFile);
then you can do what you please with this content.

Download image though .php - Wordpress

I am downloading a .jpg image through a .PHP file. It works perfect. I am testing with a link on a blank .HTML page.
But now i want to integrate the link on an Wordpress page, after click it seems that it can't connect to the .php file.
Anyone knows how to connect the a href URL to the .php file?
HTML
Download
PHP
<?php
$file = 'images/test.jpg';
header("Expires: 0");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
$ext = pathinfo($file, PATHINFO_EXTENSION);
$basename = pathinfo($file, PATHINFO_BASENAME);
header("Content-type: application/".$ext);
// tell file size
header('Content-length: '.filesize($file));
// set file name
header("Content-Disposition: attachment; filename=\"$basename\"");
readfile($file);
exit;
?>
Thank you!
You need to use the right path.
I assume your file is located in the root folder. So try adding the '/':
Download
From the theme folder:
Download

Force file download PDF 'open with' box says its a Firefox document ??

I am trying to force download a PDF file, Everything works fine. Only problem is when it show a "Download box" it says its a "Firefox Document" which actually should say "Adobe Acrobat Document". See the images, and below is the code i am using
1) Force download box from gmail, which works fine
2) Force download box from my application, which says "Firefox Document"
header("Pragma: public");
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Type: application/pdf");
header("Content-Disposition: attachment; filename=\"".basename($file)."\";" );
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize(getcwd().$file));
readfile(getcwd().$file);
for everyone who is searching for the same issue.
the TS go the solution :
add exit # the end (for some reasons firefox needs this)
header("Content-Type: application/pdf");
echo $pdf;
exit;
Try to use
Header('Content-Type: application/octet-stream');
instead of
Header("Content-Type: application/pdf");

php force download on mobile browsers

I'm trying to force download a image file (jpg for example) using php. So I have a script here force.php and here would be the code:
header("Pragma: public"); // required
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false); // required for certain browsers
header('Content-Description: File Transfer');
header("Content-Type: image/jpeg");
header('Content-Length: ' . filesize($file));
header('Content-Disposition: attachment; filename=test.jpg');
readfile($file);
Now the problem is for some browsers (mobile phone browsers especially), it'll work properly and prompt test.jpg for the user to save. However on some browser, it'll prompt force.php as download. Any solution?
Thank you!
Content disposition header highly depends on how a particular browser implements it. Sometimes there are encoding issues (I do not see in your case).
The document at Test Cases for HTTP Content-Disposition shows behavior for various browsers.
To be safe with mobile browsers you should consider having the http request's last part be equal to the actual filename of the attachment, for example http://some.url/download/test.jpg .
Use apache mod_rewrite or similar to route such requests to your download script.

Linking to a forced download script in a pdf

I am generating dynamic PDF reports in PHP and having some issues with links to the web.
My links are to a PHP script that forces the download of a file attachment. This script works perfectly in all browser when accessed via the browser. It also works from the PDF in all browser except Internet Explorer.
Instead of IE seeing the file as a PDF, PNG, or whatever the file is, the download prompt says the document type is: "HTML Plugin Document"
If the user clicks "Open" or "Save" IE says it cannot download the file and gives the filename as "index2.php". That is the beginning of the URI of address.
The correct filesize is given so I know it is getting the file. Maybe it is a header issue?
Here is the header I'm creating on the download script:
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT;");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT;");
header("Pragma: no-cache;"); // HTTP/1.0
header('Content-Type: '.$file->file_mime_type.';');
header("Content-Description: File Transfer");
header("Cache-Control: public");
header('Content-Disposition: attachment; filename="'.$file->file_name.'";');
header('Content-Length: '.$file->file_size.';');
header('Content-transfer-encoding: binary');
Any input would be greatly appreciated.
Here's what I use and that is proven to work:
header('Content-Type: application/pdf');
header('Content-Disposition: attachment; filename="'.$file->file_name.'"');
readfile($filename);
have you tried something like this?
header('Content-Disposition: attachment; filename="'.$file->filename.'"');
It might be worth mentioning that there is also a known issue on several versions of IE when transferring files over SSL which requires the following work around:
header("Cache-Control: maxage=1");
header("Pragma: public");
There is more information regarding this bug here: http://support.microsoft.com/kb/812935

Categories