Download image though .php - Wordpress - php

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

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.

PHP Download Files from another Server using File URL

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.

PHP file download including the .php file when only pdf is wanted

<?php
$file = $_GET['name'];
$path = './curr/'.$file.'.pdf'; // the file made available for download via this PHP file
$mm_type="application/pdf"; // modify accordingly to the file type of $path, but in most cases no need to do so
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Type: " . $mm_type);
header("Content-Length: " .(string)(filesize($path)) );
header('Content-Disposition: attachment; filename="'.basename($path).'"');
header("Content-Transfer-Encoding: binary\n");
readfile($path); // outputs the content of the file
?>
This is a snippet of code in file.php. I am referring to the file using:
File 1
The intent is that on click of the link, ./curr/First File.pdf should download. I do get a download, but on inspecting, it's the webpage with the pdf embedded in the file. Could anyone assist?
If you want to have just the PDF loaded, the above code is all code to be executed.
Drop all surrounding menus, header or footers. Make sure, that no HTML or any other output besides the PDF from readfile() remains, when calling this link.
Try to change the content type to :
header("Content-Type: application/force-download");

PHP: Download a file on button click?

I created a form, which, when a contained button is clicked, should open a download dialog to download a certain file. The file is placed on the same server.
I tried:
header("Content-Type: application/force-download");
header("Content-Disposition: attachment; filename=" . $file . '"');
Where $file is a local path + the file name, for example c:\mypath\myfile.xls. This does not work though. It offers me a file, but its not a valid file. How else could I do that?
Note: I wrote c:\ because its still on my local machine for testing.
Thanks!
Try this
header("Pragma: public", true);
header("Expires: 0"); // set expiration time
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header("Content-Disposition: attachment; filename=".basename($file));
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($file));
die(file_get_contents($file));
I think file_get_contents() function is no longer work with PHP 5.0.3
Try this :
$path = "http://www.example.com/files/";
$filename = "abc.gif";
header("Content-Type:image/gif");
header("Content-Disposition: attachment; filename=".$filename);
header("Cache-control: private");
header('X-Sendfile: '.$path);
readfile($path);
exit;
PHP runs in server side, you can not download the files in clients machine.
Upload the files to server and then give that path for download.
Path must be refered from the site root...move the file
ex:
script path : C:/wamp/www/test.php
file C:/script.js
then:
if(file_exists('../../user.js'))
{
echo "OK";
}
Still a bad ideea..

Force browser of Sony PRS-T1 to download .epub files

The builtin browser of my ebook-reader (Sony PRS-T1) somehow doesn't like to download .epub files.
Normally it opens .epub files as if they were text-files.
With this php-download-script I managed to force the browser to download files I store on my server:
<?php
$path = $_GET['path'];
$mimeType = $_GET['mimeType'];
if(!file_exists($path)) {
// File doesn't exist, output error
die('file not found');
} else {
$size = filesize($path);
$file = basename($path);
// Set headers
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-Disposition: attachment; filename=\"$file\"");
header("Content-Type: $mimeType");
header("Content-Transfer-Encoding: binary");
header("Content-Length: $size");
// Read the file from disk
readfile($path);
}
exit();
?>
Now, the PRS-T1 would download the file but for some reason I don't understand it will change the file extension from .epub to .htm - this is weird.
But it seems like there is a way to do it right: when I download a .epub file from readbeam.com it works just like expected (I found this hint at http://www.mobileread.com/forums/showthread.php?t=163466).
What is it, that makes the difference betweeen their configuration and mine?
Here's what I found out using firebug:
http://tinypic.com/r/vzzkzp/5
http://tinypic.com/r/2h7pbth/5
Your Content-Type header doesn't match the one from readbeam.
application/epub zip != application/epub+zip
The + is probably being seen by PHP as a space since it seems you are passing it via $_GET.

Categories