corrupted file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ddtpmyra
    Contributor
    • Jun 2008
    • 333

    #1

    corrupted file

    When Im downloading a file from mysql database the file is already corrupted below are the code I used on downloading to mysql and uploading from mysql. PDF file are doing okay as long as it is a small size, but the excel and words are totally corrupted.

    Can you please take a look if im doing it correctly.

    Note: the filedata in mysql = longblob

    Uploading to mysql


    [PHP]<?php
    # Check if a file has been uploaded
    if(isset($_FILE S['uploaded_file']))
    {
    # Make sure the file was sent without errors
    if($_FILES['uploaded_file']['error'] == 0)
    {
    # Connect to the database
    $dbLink = mysql_connect(" xxx", "xxx", "xxx")
    or die("Error! Failed to connect to the MySQL server!");
    mysql_select_db ("xxx", $dbLink)
    or die("Error! Failed to select a database!");

    # Gather all required data
    $name = mysql_real_esca pe_string($_FIL ES['uploaded_file']['name'], $dbLink);
    $mime = mysql_real_esca pe_string($_FIL ES['uploaded_file']['type'], $dbLink);
    $size = $_FILES['uploaded_file']['size'];
    $author = $_POST['author'];
    $requestor = $_POST['requestor'];
    $description = $_POST['description'];
    $deadlinefeedba ck = $_POST['deadlinefeedba ck'];
    $DateInput = $_POST['deadlinefeedba ck'];
    $category =$_POST['category'];
    $data = mysql_real_esca pe_string(file_ get_contents($_ FILES ['uploaded_file']['tmp_name']), $dbLink);

    # Create the SQL query
    $query = "
    INSERT INTO fileStorage (
    FileName, FileMime, FileSize, FileData, Created, Description,Aut hor, Requestor, DeadLineFeedbac k, category
    )
    VALUES (
    '{$name}', '{$mime}', {$size}, '{$data}', NOW(), '{$description} ', UPPER('{$author }'), UPPER('{$reques tor}'), '{$DateInput}', '{$category}'
    )";

    # Execute the query
    $result = mysql_query($qu ery, $dbLink);

    # Check if it was successfull
    if($result)
    {
    echo "Success! Your file was successfully added!";
    }
    else
    {
    echo "Error! Failed to insert the file";
    echo "<pre>". mysql_error($db Link) ."</pre>";
    }
    }
    else
    {
    echo "Error!
    An error accured while the file was being uploaded.
    Error code: ". $_FILES['uploaded_file']['error'];
    }

    # Close the mysql connection
    mysql_close($db Link);
    }
    else
    {
    echo "Error! A file was not sent!";
    }

    ?>[/PHP]

    Downloading files from mysql


    [PHP]<?php
    # Make sure a valid ID was passed
    if(isset($_GET['id']))
    {
    # Get the ID
    $id = $_GET['id'];

    # Connect to the database
    $dbLink = mysql_connect(" xxx", "xxx", "xxx")
    or die("Error! Failed to connect to the MySQL server!");
    mysql_select_db ("xxx", $dbLink)
    or die("Error! Failed to select a database!");


    # Fetch the file information
    $query = "
    SELECT FileMime, FileName, FileSize, FileData
    FROM fileStorage
    WHERE FileID = {$id}";

    $result = @mysql_query($q uery, $dbLink)
    or die("Error! Query failed: <pre>". mysql_error($db Link) ."</pre>");

    # Make sure the result is valid
    if(mysql_num_ro ws($result) == 1)
    {
    # Get the row
    $row = mysql_fetch_ass oc($result);

    # Print headers
    header("Content-Type: ". $row['FileMime']);
    header("Content-Length: ". $row['FileSize']);
    header("Content-Disposition: attachment; filename=". $row['FileName']);

    # Print data
    echo $row['FileData'];
    }
    else
    {
    echo "Error! MySQL Query returned invalid results!";
    }

    # Close the mysql connection
    @mysql_close($d bLink);

    }
    else
    {
    echo "Error! An invalid ID was passed!";
    }
    ?>[/PHP]
  • ddtpmyra
    Contributor
    • Jun 2008
    • 333

    #2
    anybody who had same encountered problem like this?

    Comment

    Working...