Image file corrupted???

Discussion in 'PHP' started by nvidia, May 20, 2010.

  1. #1
    Hi,

    i have created a system wherebuy i can store image files like .JPG's on my filestore database. My system then displays the information about the files like it's name and it's type ect. Next to each file description there is a download button, when i click download and try to view my images i get an error on windows photo gallery saying that Windows can't open this vidoe or picture. The file appears to be corrupted or damaged
    However, when i go the file directly on my machine they are not corrupted i can easily view them on paint or window photo gallery.

    Here is the code snippet for the download:

    
    if(isset($_GET['action']) and ($_GET['action'] == 'view' or $_GET['action'] == 'download') and isset($_GET['id']) )
    {
        include 'db.inc.php';
        $id = mysqli_real_escape_string($link, $_GET['id']);
        
        $sql = "SELECT filename, mimetype, filedata FROM filestore WHERE id ='$id'";
        
        $result = mysqli_query($link, $sql);
        if(!$result)
        {
    	$error = 'Database error fetching requested file';
    	include $_SERVER['DOCUMENT_ROOT'] . '/includes/error.html.php';
    	exit();
        }
        
        $file = mysqli_fetch_array($result);
        if(!$file)
        {
    	$error = 'File with specified ID not found in filestore db';
    	include $_SERVER['DOCUMENT_ROOT'] . '/includes/error.html.php';
    	exit();
        }
        
        // Use variables to  send the data in http headers
        $filename = $file['filename'];
        $mimetype = $file['mimetype'];
        $filedata = $file['filedata'];
        $disposition = 'inline'; // use to download
        
        if($_GET['action'] == 'download')
        {
    	$mimetype = 'application/octet-stream'; // force download in older browsers
    	$disposition = 'attachment';
        }
        
        // Content-type must come before Content-disposition
        header("Content-type: $mimetype"); 
        header("Content-disposition: $disposition; filename=$filename"); // force the browser to display save dialog for download
        header('Content-length: ' . strlen($filedata) );
        
        echo $filedata;
        exit();  
    }
    
    Code (markup):
    Can somebody tell me why i get the error on windows photo gallery?????
     
    nvidia, May 20, 2010 IP
  2. webria

    webria Peon

    Messages:
    43
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Please try to read the content from file saved , into $filedata variable , rather than getting this data from mysql table and see.
     
    webria, May 21, 2010 IP