Page 1 of 1

Having problem downloading files from database but able to w

Posted: Wed Jul 30, 2008 6:56 pm
by nmay
Gentlemen, I really need your help:

I am writing few piece of code that uploads files into database, then download the files from database:

Here is upload

Code: Select all

if ( is_file($uploadedfile) && isset($userid)   )
    {
        $database =& JFactory::getDBO();
        $filesize = filesize($uploadedfile);
        $fp    = fopen($uploadedfile, 'r');
        $content = fread($fp, $filesize);
        $content = addslashes($content);
                
        fclose($fp);
        
        $storedata = "insert into udp_uploads values ('', '$userid', '$filename' , '$filesize', 'unreviewed', '$recordid', '$content',
                               '$original_type')";
        $database->setQuery($storedata);
        if (!$database->query()) { echo "<script>alert('".$database->getErrorMsg()."'); window.history.go(-1); </script>";    }
        
    }
 
 
And here is download.php that's being called from a page that list all files available to that user who owns:

Code: Select all

 
<?php
 
 
if(isset($_GET['fileid']))
{
    include 'config.php';
    include 'opendb.php';
 
    $fileid      = $_GET['fileid'];
    $query   = "SELECT name, size, type, filecontent FROM udp_uploads WHERE fileid = '$fileid'";
    $result  = mysql_query($query) or die('Error, query failed');
    list($name, $size, $type, $content) = mysql_fetch_array($result);
    
    
    header("Content-length: $size");
    header("Content-type: $type");
    header("Content-Disposition: attachment; filename=$name");
    echo $content;
    include 'closedb.php';
    exit;
}
 
?>
 
Files uploaded to mysql with no problem, I can use mysql's field viewer to view jpgs I uploaded, but the weird thing is I cannot download. The downloaded JPEGs, Zips has the same size as the original file, but cannot be opened(corrupted).
But I can download PDFs!!!!

I even tried to write the file from db to a directory:

Code: Select all

   <?php
 
 
if(isset($_GET['fileid']))
{
    include 'config.php';
    include 'opendb.php';
    
    $fileid      = $_GET['fileid'];
    $query   = "SELECT name, size, type, filecontent FROM udp_uploads WHERE fileid = '$fileid'";
    $result  = mysql_query($query) or die('Error, query failed');
    list($name, $size, $type, $content) = mysql_fetch_array($result);
    //$content=base64_decode($content);
    
    
    header("Content-length: $size");
    header("Content-type: $type");
    header("Content-Disposition: attachment; filename=$name");
    echo $content;
        //$content is saved to desktop, but cannot be opened. For JPEGS, it says "No preview". But for PDF it just works.
    
    $filename = $name;
    $FileHandle = fopen($filename, 'w') or die("can't open file");
    fwrite($FileHandle, $content);
    fclose($FileHandle);
        //Yet, files are successfully written to directory in webroot.
 
    include 'closedb.php';
    exit;
}
 
?>
Why is it working for PDF but not JPEG, TIF, ZIP? Please HELP! I thought it's something wrong with encoding, so I even tried base64 encoding and decoding. But still only works for PDF, not JPEG, had people around me tried on their machine, they cannot open the JPEG either. I am going nuts..... :cry:

Re: Having problem downloading files from database but able to w

Posted: Wed Jul 30, 2008 7:07 pm
by nmay
nmay wrote:Gentlemen, I really need your help:

I am writing few piece of code that uploads files into database, then download the files from database:

Here is upload

Code: Select all

if ( is_file($uploadedfile) && isset($userid)   )
    {
        $database =& JFactory::getDBO();
        $filesize = filesize($uploadedfile);
        $fp    = fopen($uploadedfile, 'r');
        $content = fread($fp, $filesize);
        $content = addslashes($content);
                
        fclose($fp);
        
        $storedata = "insert into udp_uploads values ('', '$userid', '$filename' , '$filesize', 'unreviewed', '$recordid', '$content',
                               '$original_type')";
        $database->setQuery($storedata);
        if (!$database->query()) { echo "<script>alert('".$database->getErrorMsg()."'); window.history.go(-1); </script>";    }
        
    }
 
 
And here is download.php that's being called from a page that list all files available to that user who owns:

Code: Select all

 
<?php
 
 
if(isset($_GET['fileid']))
{
    include 'config.php';
    include 'opendb.php';
 
    $fileid      = $_GET['fileid'];
    $query   = "SELECT name, size, type, filecontent FROM udp_uploads WHERE fileid = '$fileid'";
    $result  = mysql_query($query) or die('Error, query failed');
    list($name, $size, $type, $content) = mysql_fetch_array($result);
    
    
    header("Content-length: $size");
    header("Content-type: $type");
    header("Content-Disposition: attachment; filename=$name");
    echo $content;
    include 'closedb.php';
    exit;
}
 
?>
 
Files uploaded to mysql with no problem, I can use mysql's field viewer to view jpgs I uploaded, but the weird thing is I cannot download. The downloaded JPEGs, Zips has the same size as the original file, but cannot be opened(corrupted).
But I can download PDFs!!!!

I even tried to write the file from db to a directory:

Code: Select all

   <?php
 
 
if(isset($_GET['fileid']))
{
    include 'config.php';
    include 'opendb.php';
    
    $fileid      = $_GET['fileid'];
    $query   = "SELECT name, size, type, filecontent FROM udp_uploads WHERE fileid = '$fileid'";
    $result  = mysql_query($query) or die('Error, query failed');
    list($name, $size, $type, $content) = mysql_fetch_array($result);
    //$content=base64_decode($content);
    
    
    header("Content-length: $size");
    header("Content-type: $type");
    header("Content-Disposition: attachment; filename=$name");
    echo $content;
        //$content is saved to desktop, but cannot be opened. For JPEGS, it says "No preview". But for PDF it just works.
    
    $filename = $name;
    $FileHandle = fopen($filename, 'w') or die("can't open file");
    fwrite($FileHandle, $content);
    fclose($FileHandle);
        //Yet, files are successfully written to directory in webroot.
 
    include 'closedb.php';
    exit;
}
 
?>
Why is it working for PDF, TXT, DOC but not JPEG, TIF, ZIP? When I use winrar to open the zip I downloaded. It says unexpected end of archive. Please HELP! I thought it's something wrong with encoding, so I even tried base64 encoding and decoding. But still only works for PDF, not JPEG, had people around me tried on their machine, they cannot open the JPEG either. I am going nuts..... :cry:

Re: Having problem downloading files from database but able to w

Posted: Sun Aug 03, 2008 2:25 pm
by pkbruker
Sounds like an encoding problem. Try setting the database table field which stores the files to "binary".

But, why store the files like this and not simply as a file? Sounds like a troublesome way of doing it. If you're doing it for security reasons, consider encrypting the files in stead.