Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

downloadind a picture

Status
Not open for further replies.

kusarigama

Technical User
Apr 24, 2002
45
DE
Hi all,

Is it possible via PHP to create a link,where you are able to download a picture from a certain folder.

Thanx in advance

Olli
 
Sure. Have PHP output an &quot;<A...>&quot; tag which has as its href attribute the web-server-relative path to the file. ______________________________________________________________________
Never forget that we are
made of the stuff of stars
 
When you click on your link, your browser is going to use its defined behavior for acting on the type of file to which the &quot;<A>&quot; tag links.

In the case of an image file, a browser's defined action is to download it and display it. There is no way via HTML to change that behavior that I know of. ______________________________________________________________________
Never forget that we are
made of the stuff of stars
 
Heres a page I wrote for downloading stuff from a folder that I dont want the public to know exists, This also qrites a textfile for use as a counter to track downloads.

you need two parts (requestfile.php - the bit the user sees) and download.php, (this should remain invisible to the user and if given the $filename will return nothing)

so here goes:

------------------requestfile.php
<?php
$folder=&quot;hidden_folder&quot;;
if(!file_exists(&quot;$folder/$filename&quot;)){
echo &quot;<html><head><title>404 - File not found</title></head<<body>&quot;;
echo &quot;<h1>Unspecified file or file not found</h1><br> File: <b>$filename</b> Not located on this server&quot;;
echo &quot;</body></html>&quot;;
}else{
if(isset($filename)){
$counter = &quot;$filename.txt&quot;;

if(!file_exists($counter)){
echo &quot;File: $filename has been downloaded 0 times<br>&quot;;
}else{

$fd = fopen ($counter, &quot;r&quot;);
$count = fread ($fd, filesize ($counter));
fclose ($fd);

echo &quot;File: <b>$filename </b>has been downloaded <b>$count</b> time(s)<br>&quot;;
}

echo &quot;<a href=download.php?filename=$filename&count=$count&counter=$counter>download it</a>&quot;;
}else{

echo &quot;<h1> Select a file you muppet!</h1>&quot;;
}
}
?>

-------------------download.php
<?php
$folder=&quot;hidden_folder&quot;;
if(isset($filename)){
if(file_exists(&quot;$folder/$filename&quot;)){
$size = filesize('$folder/$filename');
header(&quot;Content-Type: application/save&quot;);
header(&quot;Content-Length: $size&quot;);
header(&quot;Content-Disposition: attachment; filename=$filename&quot;);
header(&quot;Content-Transfer-Encoding: binary&quot;);
$fh = fopen(&quot;$folder/$filename&quot;, &quot;r&quot;);
fpassthru($fh);


// increment counter

$count++;
$fp=fopen(&quot;$counter&quot;,&quot;w&quot;);
fputs($fp, $count);
fclose($fp);
exit;

}else{

echo &quot;No file selected&quot;;

}
}else{
echo &quot;<h1>Use the proper file selection method or go away!</h1>&quot;;
}

?> ______________________________________________________________________
There's no present like the time, they say. - Henry's Cat.
 
Hi,

all works fine, but when the JPEG is download and I try to open it, then it is empty. The filesize is also smaller ..

here is the snippet

-------> request.php4
<html>
<head>
<title>Untitled</title>
</head>
<body>
<?
$folder = &quot;test/&quot;;
$filename = &quot;test.jpg&quot;;
$size = filesize($folder.$filename);
echo &quot;<a href=download.php?filename=$filename&size=$size>download it</a>&quot;;
?>
</body>
</html>

-------> downlaod.php4
<?
header(&quot;Content-Type: application/save-as&quot;);
header(&quot;Content-Length: $size&quot;);
header(&quot;Content-Disposition: attachment; filename=$filename&quot;);
header(&quot;Content-Transfer-Encoding: binary&quot;);
?>
<html>
<head>
<title>Untitled</title>
</head>
<body>
</body>
</html>

Olli
 
don't change the download.php code, its a must that it stays as was or it'll send ou 0 length files. no-one will ever get to se it if things work according to plan, the headers will just &quot;shove&quot; the file out. ______________________________________________________________________
There's no present like the time, they say. - Henry's Cat.
 
Also , to give you an idea, its what I use on my site.

- check out mysqlexplorer page and then hit the download link. its not a big file but you'll see how download.php reacts to requests :) ______________________________________________________________________
There's no present like the time, they say. - Henry's Cat.
 
Hi,

When I put code over the headers, I get that anoying &quot;Headers already sent&quot; message ...

Olli
 
So I'll say it again.

DON'T change download.php its invisible to the end user - they NEVER see that page if they use the site properly and if they deliberately navigate to it .. well try it before changing it :) ______________________________________________________________________
There's no present like the time, they say. - Henry's Cat.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top