Jump to content

Force Download


Dragosvr92

Recommended Posts

Hello

i want to make a Link that when its clicked it forces the download by php  of a .PNG Image from a specified directory like

 

<a href="http://www.My_Image_Site.com/image.png">Download Link</a> 

 

dos anyone have a idea what will be the php script like ?

i hope i posted it where it should ..

 

edit : added code tags

Link to comment
Share on other sites

Thanks , i googled myself for Download image onclick and found this Site]http://stackoverflow.com/questions/1227157/html-javascript-download-a-picture-onclick]Site and come up with this script

<?php
if (isset($_GET['ID'])){
$file = 'http://img.msg.yahoo.com/avatar.php?format=jpg&yids=' .$_GET['ID'];
header("Content-Type: application/octet-stream; ");
header("Content-Transfer-Encoding: binary"); 
header('Content-Disposition: attachment; filename="'.$_GET['ID'].'.png"');

readfile($file);
}
?>

 

But after i submit $_GET['ID'] it starts to download the image

i only want to add a link so when its clicked it shows the download box

Link to comment
Share on other sites

No, there's no way to force download the way you want. You can start a download, like you wanted, but there's no way to make a download go without going through the browser, and there's no way to have the browser show the progress. If there were a way, it would be a major security hole. Imagine if you could, people could download files onto your computer without knowing about it at all

Link to comment
Share on other sites

mate ... that download gows within a browser

 

if you run that script on a php page it will just start to open the download box you will click ok and it will download it

 

 

i only want that php code to be executed only when someone clicks a link

Link to comment
Share on other sites

Oh, well that's easy

 

<?php

if (isset($_GET['ID'])){

$file = 'images/png' . $_GET['ID'] . '.png';

header("Content-Type: application/octet-stream; ");

header("Content-Transfer-Encoding: binary");

header('Content-Disposition: attachment; filename="'.$_GET['ID'].'.png"');

 

readfile($file);

}

?>

 

For that script, just have that as image.php

 

and when you want them to download an image, just link to <a href="image.php?ID=3">Click Here to Download Image</a>

 

and it'll download images/png3.png

Link to comment
Share on other sites

i put this page up

http://rising-dead.com/Look/

so u may see better what i want to do..

 

 

its script is :

<?php
if (isset($_GET['ID'])){
$file = 'http://img.msg.yahoo.com/avatar.php?format=png&yids=' .$_GET['ID'];
header("Content-Type: application/octet-stream; ");
header("Content-Transfer-Encoding: binary"); 
header('Content-Disposition: attachment; filename="'.$_GET['ID'].'.png"');

readfile($file);
}
?>
	 <form action="<? $_SERVER['PHP_SELF']; ?>" method="get">ID: 
	 <input type="text" name="ID"/>
	 <input type="submit" value="Download" title="Submits" /> 
	 <input type="reset" value="Cancel" title="resets the form" /><br />
	 </form>

 

When you enter a yahoo id for example rdragos18

it will show the download box to download my avatar

 

i only want it to show the download box when you click a link

 

is there any thing like :

<?php

if (THE XX LINK WAS CLICKED){

echo 'Executed script'

}

?>

 

only execute the script when the link is clicked

 

Link to comment
Share on other sites

So you want the page to only work when you click a link rather than if you typed it into the brower url bar for example?  If the link and the script are on the same domain, you can use $_SESSION to sign the link.

 

Page the link is on:

<?php
session_start();
$hash = sha1(time());
$id = "rdragos18";
$_SESSION['hash'] = $hash;

echo "<a href='image.php?hash=$hash&id=$id'>Download avatar</a>";

 

Download script:

<?php
session_start();
if($_GET['hash'] == $_SESSION['hash'])
{
//Do download stuff.
}
else
{
//Do error stuff.
}

 

I'm not suggesting that my code is secure but it gives you a start point to work from.

Link to comment
Share on other sites

that dosnt really help me much

 

if you look into my script well you will see that the image/avatar is not hosted on my site

Your form works as it should... You press Download and it starts downloading.

 

I assume that you wish to preview the image before you download it, so you would need to use a form handler to check if the form has been submitted if(isset($_POST['id'])) { and then have a link to the actual download php script from there.

Link to comment
Share on other sites

that dosnt really help me much

 

if you look into my script well you will see that the image/avatar is not hosted on my site

 

It doesn't matter where the images are hosted, you only need to have the download script and the link on your site.  My script only adds to what you've already got which seems to work fine.

Link to comment
Share on other sites

Nevermind I've Done it myself

 

 

Like this:

//Download Avatar Script
if (isset($_GET['Download'])){
$file = 'http://img.msg.yahoo.com/avatar.php?format=png&yids=' .$_GET['ID'];
header("Content-Type: application/octet-stream; ");
header("Content-Transfer-Encoding: binary"); 
header('Content-Disposition: attachment; filename="'.$_GET['ID'].'.png"');

readfile($file);
}
//End


<?echo '<a href="http://localhost/MyYahooScript/test.php?ID='.$_GET['ID'].'&Download=+">Download Avatar</a>';?>

 

it was pretty simple..

Link to comment
Share on other sites

lol i find a other bug...

the image script starts after a empty line that stops displaying the image

 

like :

1. 
2. ‰PNG

 

the ‰PNG line must be the first at the top not on the second line

 

Nevermind i figured what was it !!

 

it was because i had a space after the ?> ending tag

<?php
Some Script
?>
         //Space was here
<?php
image script
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.