SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Enthusiast
    Join Date
    Apr 2006
    Posts
    50
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    download file from MySQL not working

    Hey,
    I pulled this code from php-mysql-tutorial.com and am trying it to undersatnd what it is doing before editing for my needs. The upload.php works fine, but I can't get the download.php to work. When I load it I just get a blank screen with no errors. I think the problem is on the <a href> line but can't figure it out.

    <?php
    include 'includes/configdb.php';
    include 'includes/opendb.php';

    $query = "SELECT id, filename FROM upload";
    $result = mysql_query($query) or die('Error, query failed');
    if(mysql_num_rows($result) == 0)
    {
    echo "Database is Empty <br>";
    }
    else
    {
    while(list($id, $filename) = mysql_fetch_array($result))
    {
    ?>
    <a href="download.php?id=<?php=$id;?>"><?php=$filename;?></a> <br>
    <?php
    }
    }
    include 'includes/closedb.php';
    ?>

  2. #2
    SitePoint Zealot diskhub's Avatar
    Join Date
    Jun 2004
    Location
    singapore
    Posts
    161
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    the screen is empty because you have already access the database without any error.

    as for your a href tag

    it should be

    PHP Code:
    <a href="download.php?id="<?php echo $id;?>>
    <?php echo $filename?></a>
    by doing so, you are actually passing the $id into the url so that you can retrieve it from the download.php page using $_GET["id"].
    I LOVE PHP!!!

  3. #3
    SitePoint Enthusiast
    Join Date
    Apr 2006
    Posts
    50
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That worked great thanks. Now I put in the rest of the code with the $_GET
    When I click on the file It doesn't do anything. Once again I don't see the problem.

    PHP Code:
    <?php
    include 'includes/configdb.php';
    include 
    'includes/opendb.php';

    $query "SELECT id, filename FROM upload";
    $result mysql_query($query) or die('Error, query 1 failed');
    if(
    mysql_num_rows($result) == 0)
    {
    echo 
    "Database is Empty <br>";
    }
    else
    {
    while(list(
    $id$filename) = mysql_fetch_array($result))
    {
    ?>
    <a href="download.php?id="<?php echo $id;?>><?php echo $filename?></a>
    <?php
    }
    }
    include 
    'includes/closedb.php';
    ?>
    </body>
    </html>

    <?php
    if(isset($_GET['id']))
    {
    // if id is set then get the file with the id from the database

    include 'includes/configdb.php';
    include 
    'includes/opendb.php';

    $id $_GET['id'];
    $query "SELECT filename, filetype, filesize, content " .
             
    "FROM upload WHERE id = '$id'";
             
    $result mysql_query($query) or die('ERROR, query 2 failed');
    list(
    $filename$filetype$filesize$content) = mysql_fetch_array($result);

    header("Content-length: $filesize");
    header("Content-type: $filetype");
    header("Content-Disposition: attachment; filename=$filename");
    echo 
    $content;

    include 
    'includes/closedb.php';
    exit;
    }
    ?>

  4. #4
    SitePoint Enthusiast
    Join Date
    Aug 2005
    Posts
    38
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    This is not safe

    There is no safety for download.php.People will leech your downloads.Add Protection to your script.

  5. #5
    SitePoint Wizard siteguru's Avatar
    Join Date
    Oct 2002
    Location
    Scotland
    Posts
    3,608
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    <a href="download.php?id=<?php echo $id;?>"><?php echo $filename?></a>
    The $id value needs to be before the closing " not after else it doesn't exist in the querystring.
    Ian Anderson
    www.siteguru.co.uk

  6. #6
    SitePoint Enthusiast
    Join Date
    Apr 2006
    Posts
    50
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks siteguru, wish I had noticed that. That is why I love this site!

    Anyway now when I click on the file that is in the database, it pulls the file that uploaded into the database up in the browser and reads the raw data instead of displaying the picture (a jpg in this case). Actually most of my users will be uploading xls files so I need the admin to be able to save the file to their machine in the download portion. Is there a good tutorial anyone knows of on this subject?

    Also thanks for the security tip _rajesh_ That is something I do on the actual site, but this is for learning what the code is doing before I implement it on the full site.

  7. #7
    SitePoint Wizard siteguru's Avatar
    Join Date
    Oct 2002
    Location
    Scotland
    Posts
    3,608
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    You need to send the content-type headers for the filetype in question else only the raw data will be sent to the browser. Also, the PHP file sending the data needs to be separate from the file trying to display the data. For example ...

    Code:
    <img src="thumbnail.php?id=1" />
    Here the browser would load the file holding the <img> tag, and then thumbnail.php would get the filedate and send it back in the right format. Here's an example script for thumbnail.php

    PHP Code:
    <?php
    /*
    =============================================================================================
    Script Name:    thumbnail.php
    Version:    1.1
    Author:        Ian Anderson
    Date:        November 2002
    Acknowledge:    Teekai - http://www.teekai.info/v8/home.php (see the original script at
            http://www.hotscripts.com/Detailed/18727.html on which this script is based).

    This script is a self-contained Thumbnail Image Generator. It should be called as follows ...

    <img src="/path/to/thumbnail.php?gd=N&src=/path/to/image.EXT&maxw=NNN" />

    where     N     = the GD library version (supported values are 1 and 2)
        EXT    = the file extension of the image file
                (supported values are gif (if gd = 2), jpg and png)
        NNN    = the desired maximum width of the thumbnail

    If the actual image is narrower than the desired maximum width then the original image size
    is used for the thumbnail copy.

    This script checks for the following errors and generates an error JPEG image accordingly ...

        GD version selected neither 1 nor 2;
        Image create functions not supported;
        Image file not found at the selected location;
        GD version 2 functions not supported on the running version of PHP.

    This script is available for use as freeware subject to the retention of the preceding
    information and acknowledgements in any copy or modification that is made to this code.
    =============================================================================================
    */

    function ErrorImage ($text) {
        global 
    $maxw;
        
    $len strlen ($text);
        if (
    $maxw 154$errw 154;
        
    $errh 30;
        
    $chrlen intval (5.9 $len);
        
    $offset intval (($errw $chrlen) / 2);
        
    $im imagecreate ($errw$errh); /* Create a blank image */
        
    $bgc imagecolorallocate ($im1536363);
        
    $tc imagecolorallocate ($im255255255);
        
    imagefilledrectangle ($im00$errw$errh$bgc);
        
    imagestring ($im2$offset7$text$tc);
        
    header ("Content-type: image/jpeg");
        
    imagejpeg ($im);
        
    imagedestroy ($im);
        exit;
    }

    // Change $maxw=190 to your desired default width for the thumbnail.
    // Note: any changes will mean the $offset value for the ErrorImage ( )
    // function calls will require amending accordingly.
    function thumbnail ($src$maxw=190) {

        if (!
    function_exists ("imagecreate") || !function_exists ("imagecreatetruecolor")) {
            
    ErrorImage ("No image create functions!");
        }

        if ( @
    imagecreatetruecolor (5,5) ) {
            
    $gdver 2;
        } else {
            
    $gdver 1;
        }

        
    $size getimagesize ($src);
    //echo "<pre>\n";
    //print_r ($size);
    //echo "</pre>\n";
    //exit;
        
    if (!$size) {
            
    ErrorImage ("Image File Not Found!");
        } else {

            if (
    $size[0] > $maxw) {
                
    $newx intval ($maxw);
                
    $newy intval ($size[1] * ($maxw $size[0]));
            } else {
                
    $newx $size[0];
                
    $newy $size[1];
            }

            if (
    $gdver == 1) {
                
    $destimg imagecreate ($newx$newy );
            } else {
                
    $destimg = @imagecreatetruecolor ($newx$newy ) or die (ErrorImage ("Cannot use GD2 here!"));
            }
            
            if (
    $size[2] == 1) {
                if (!
    function_exists ("imagecreatefromgif")) {
                    
    ErrorImage ("Cannot Handle GIF Format!");
                } else {
                    
    $sourceimg imagecreatefromgif ($src);

                    if (
    $gdver == 1)
                        
    imagecopyresized ($destimg$sourceimg0,0,0,0$newx$newy$size[0], $size[1]);
                    else
                        @
    imagecopyresampled ($destimg$sourceimg0,0,0,0$newx$newy$size[0], $size[1]) or die (ErrorImage ("Cannot use GD2 here!"));

                    
    header ("content-type: image/gif");
                    
    imagegif ($destimg);
                }
            }
            elseif (
    $size[2]==2) {
                
    $sourceimg imagecreatefromjpeg ($src);

                if (
    $gdver == 1)
                    
    imagecopyresized ($destimg$sourceimg0,0,0,0$newx$newy$size[0], $size[1]);
                else
                    @
    imagecopyresampled ($destimg$sourceimg0,0,0,0$newx$newy$size[0], $size[1]) or die (ErrorImage ("Cannot use GD2 here!"));

                
    header ("content-type: image/jpeg");
                
    imagejpeg ($destimg);
            }
            elseif (
    $size[2] == 3) {
                
    $sourceimg imagecreatefrompng ($src);

                if (
    $gdver == 1)
                    
    imagecopyresized ($destimg$sourceimg0,0,0,0$newx$newy$size[0], $size[1]);
                else
                    @
    imagecopyresampled ($destimg$sourceimg0,0,0,0$newx$newy$size[0], $size[1]) or die (ErrorImage ("Cannot use GD2 here!"));

                
    header ("content-type: image/png");
                
    imagepng ($destimg);
            }
            else {
                
    ErrorImage ("Image Type Not Handled!");
            }
        }

        
    imagedestroy ($destimg);
        
    imagedestroy ($sourceimg);
    }

    thumbnail ($_GET["src"], $_GET["maxw"]);
    ?>
    Ian Anderson
    www.siteguru.co.uk


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •