Results 1 to 9 of 9
  1. #1
    Join Date
    Jul 2009
    Posts
    6

    Unanswered: unknown format while downloading pdf or images or compressed files from mysql

    hello friends,

    this is praveen.I am new to this forum.

    I had one problem while downloading images or pf or compressed files
    It display unknown format while downloading pdf or images or compressed files from mysql databse.i am using php to upload to database.

    this is my table structure.

    CREATE TABLE upload_download (
    id INT NOT NULL AUTO_INCREMENT,
    name VARCHAR(50) NOT NULL,
    type VARCHAR(50) NOT NULL,
    size INT NOT NULL,
    content MEDIUMBLOB NOT NULL,
    PRIMARY KEY(id)
    );


    is there any modification needed in table structure.?
    waiting for ur reply.
    thanks

  2. #2
    Join Date
    Sep 2009
    Posts
    64
    Not sure where you download it from. My assumption is that you have some script accessed from browser. The script selects from the database and return the output to browser with Content-Disposition header set somewhere.

    If the above is correct, you may want to add Content-type header depending on the content.

    If you already did, then there may be some corruption when inserting the data to database.

    PS. Any reasons why you want to put the file in the database? It may be better to just keep it as a file and just put a reference to the file in database. DBMS was created to handle data and file system was created to handle file. I think performance wise, it's better to stick with what's best for each system.

  3. #3
    Join Date
    Jul 2009
    Posts
    6
    thanks for ur answer .
    i am developing an application (using php) which downloads images or pdf or rar files from mysql database which is on my system(localhost).

    while uploading is sucessfull ,it fails during download i,e images,pdf files are downloaded sucessfully but when try to open these files it shows corrupted.

  4. #4
    Join Date
    Jul 2009
    Posts
    6
    thanks mnirwan,

    i didnt get u exactly what u mean . do u know about php.i will send php script if u know u can suggest me any modification needed.

  5. #5
    Join Date
    Nov 2004
    Location
    out on a limb
    Posts
    13,692
    Provided Answers: 59
    personally I wouldn't store stuff like PDF's in the db in the first place
    im just wondering if there is a character in the data stream which has a different meaning between the db and the application and this is causing trouble. it could also be that the resource is too big for the db so the data is getting truncated

    personally I'd store a URL to the resource in the db and then get the browser to download the resource itself.
    I'd rather be riding on the Tiger 800 or the Norton

  6. #6
    Join Date
    Sep 2009
    Posts
    64
    Praveendussa : What I meant is exactly what healdem meant. Why would you want to store PDF or other files in DB?

    DB was designed to store / retrieve data fast and easily. Although you can store files as data, it's not really what it was designed for.

    File system was designed to store files. So why would you want to use DB as a storage for files, while file system was exactly designed for it?

    If you insist on storing it on DB (eventhough it's inferior vs storing it as regular file), do paste the script. Maybe I'll be able to see what's wrong with it.

  7. #7
    Join Date
    Jul 2009
    Posts
    6
    thanks mnirwan,

    i will try to use file system..thanks for ur reply.

  8. #8
    Join Date
    Jul 2009
    Posts
    6
    hello mnirwan,

    before trying file system i want to make it workout with database,providing small application for users to download from my database .

    i am sending php script for uploading and downloading files.

    pasting my php script here.please go throw it.

    below was my table structure

    CREATE TABLE upload_download (
    id INT NOT NULL AUTO_INCREMENT,
    name VARCHAR(50) NOT NULL,
    type VARCHAR(50) NOT NULL,
    size INT NOT NULL,
    content MEDIUMBLOB NOT NULL,
    PRIMARY KEY(id)
    );

    below was the script for uploading files

    <?php

    include 'functions.inc';

    $links = "<A HREF='upload.html'>Click here to proceed to the upload page</A>";

    if(isset($_POST['upload']) && $_FILES['userfile']['size'] > 0)
    {
    $fileName = $_FILES['userfile']['name'];

    $tmpName = $_FILES['userfile']['tmp_name'];

    $fileSize = $_FILES['userfile']['size'];

    $fileType = $_FILES['userfile']['type'];

    $fp = fopen($tmpName, 'r');
    $content = fread($fp, filesize($tmpName));
    $content = addslashes($content);

    fclose($fp);

    if(!get_magic_quotes_gpc())
    {
    $fileName = addslashes($fileName);
    }

    Connect_mysqldb();

    $query = "INSERT INTO upload_download(name, size, type, content ) VALUES('$fileName', '$fileSize', '$fileType', '$content')";

    mysql_query($query) or die('Error, query failed');


    echo "<br>File $fileName uploaded<br>";
    echo '"<a href="upload.html">click here to go back to upload page </a>"';

    }else{

    echo "please select the file to download <br /> ";
    echo $links;
    }


    ?>

    below was the script for downloading files

    <?php

    require "functions.inc";



    // if id is set then get the file with the id from database

    $downloadid = $_GET['id'];

    connect_mysqldb();

    echo $id;

    $query = "SELECT name, type, size, content FROM upload_download WHERE id = '".$id."' ";

    $result = mysql_query($query) or die('Error, query failed');

    list($name, $type, $size, $content) = mysql_fetch_array($result);



    header("Content-type: $type");

    header("Content-Length: $size");

    header("Content-Type: application/octet-stream");

    header('Content-Disposition: attachment; filename=$name');

    header("Content-Transfer-Encoding: binary\n");

    header("Content-Disposition: attachment; filename=$name");

    echo $content;

    exit;


    ?>

    is there any modification needed in script.?
    if u have any script send to this mail
    praveen8632@gmail.com
    waiting fro ur reply.thanks in advance

  9. #9
    Join Date
    Sep 2009
    Posts
    64
    If you're reading a binary file, why would you want to addslashes? That maybe your issue.

Posting Permissions

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