Web Design Forums

Welcome! Please register or log in: Forgot your password? Why register?
You are here: Web Design Forums » Programming Help » PHP » need code if this is possible RSS

need code if this is possible

This thread was started by chikulanikader and has been viewed 752 times, and contains 6 replies, with the last reply made by nitin.
Post Reply
1
View chikulanikader's reputation
Posted May 30 '09 at 03:40 AM
      Posts: 2
What I want to do is add a code that makes a picture downloadable. (hope that makes sense)

I don't want the picture to appear like what happens when you click on a thumbnail and a larger picture appears. The one I have is 3 mgs in size and is for club members who want it for printing purposes.

Is there some kind of code where if I click on the words "corvette sunset" a dialog box appears and asks if you want to download the file.

Hope I explained this well enough

Thanks for any help

Advertisement Register for free to hide these ads and participate in discussions!

2
1,251 points at 99% Moderator Repute
mlseim, WDF Moderator Private message  
Posted May 30 '09 at 12:49 PM
      Posts: 3,100
This has a couple of parts to it.

1) You mention "club members".
Does that mean they have logged into your website?
I ask this because you may have something that will check to
see if the site visitor is logged in ... other visitors can't download the image.

2) The image could be stored in a secret directory, and you
serve the image (or photo) using PHP. The site user would be
able to see the photo on their browser (and copy it), but they
won't see where the photo originated from.

This is a simple example (I found this on the internet).
It does open a dialog box (open or save?), but the user
does not know where the image came from.
<?php    
if($_GET){
    if(
$_GET['file']) {
        
$filename $_GET['file'];
        
$download_path "files/";
    }

    if(
eregi("\.\."$filename)) die("I'm sorry, you may not download that file.");
    
$file str_replace(".."""$filename);
    if(
eregi("\.ht.+"$filename)) die("I'm sorry, you may not download that file.");
    
$file "$download_path$file";
    if(!
file_exists($file)) die("I'm sorry, the file doesn't seem to exist.");
    
$type filetype($file);
    
$today date("F j, Y, g:i a");
    
$time time();

    
header("Content-type: $type");
    
header("Content-Disposition: attachment;filename=$filename");
    
header("Content-Transfer-Encoding: binary");
    
header("Cache-Control: ");
    
header("Pragma: ");
    
set_time_limit(0);
    
readfile($file);
}
?>
If it's zero degrees outside today, and it's supposed to be twice as cold tomorrow, how cold is it going to be?

3
32 points at 100%
Posted June 10 '09 at 05:05 AM
      Posts: 76
The best way to do it:

define('FILE_PATH', '/path/to/your/file/');
define('FILE_NAME', 'some_image.jpg');
header('Content-type: application/file');
header('Content-disposition: attachment; filename="'. FILE_NAME .'"');
readfile(FILE_PATH . FILE_NAME);

4
34 points at 74%
Dorky, Freelance Home page   Private message  
Posted June 10 '09 at 01:38 PM
      Posts: 782
how would you implement that without getting a header already sent error? is this to be the start of a new .php or on the same page.
“Imagination is more important than knowledge. For knowledge is limited to all we now know and understand, while imagination embraces the entire world, and all there ever will be to know and understand.” Albert Einstein

5
32 points at 100%
Posted June 10 '09 at 02:36 PM
      Posts: 76
how would you implement that without getting a header already sent error? is this to be the start of a new .php or on the same page.

Yes, quite frankly that has to be the first and only thing in the document...

If you want to make the file more universal... Say if you have just document.php to display some content, but display something else by appending ?file=filename to document.php without creating a new php file you would do something like this:
<?php
if(isset($_REQUEST['file']))
{
define('FILE_PATH', '/path/to/your/file/');
define('FILE_NAME', 'some_image.jpg');
header('Content-type: application/file');
header('Content-disposition: attachment; filename="'. $_REQUEST['file'] .'"');
readfile(FILE_PATH . $_REQUEST['file']);
} else {

?>
<html>
<head>
<title>Somepage</title>
</head>
<body>
Some body stuff here.
</body>
</html>
<?php
}
?>

6
34 points at 74%
Dorky, Freelance Home page   Private message  
Posted June 10 '09 at 04:11 PM
      Posts: 782
very cool
“Imagination is more important than knowledge. For knowledge is limited to all we now know and understand, while imagination embraces the entire world, and all there ever will be to know and understand.” Albert Einstein

7
View nitin's reputation
nitin, WDF Noob Private message  
Posted July 3 '09 at 03:14 AM
      Posts: 14
for this you can use code behind.

Post Reply

Similar Threads
Thread Thread Starter Forum Replies Last Post
Top 21 PHP Programming mistakes thexchord PHP 18 March 10 '10 09:52 AM
Modifying code to make file upload optional ravensjeff PHP 3 March 12 '09 10:23 AM
Code tags reminder filburt1 Announcements 4 November 3 '05 06:29 PM
JavaScript Dropdown Menu Tutorial [Li] Brad Coding Articles & Tutorials 12 August 17 '04 04:16 PM
Stop direct linking with this code illu HTML and CSS Help 0 February 19 '04 11:40 AM