SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Member
    Join Date
    Jun 2012
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    pdf error: the selected file can not be opened.

    I have a project in which I desire to upload and download blob files in php. I have written the codes to upload the files into mysql however, when I try to download the files, be it pdf, .doc or jpeg, the error message I get is "The selected file can not be opened". I need assistance in this regard pls. The download codes are:

    PHP Code:
    <?php
    if(isset($_GET['download']))
    {
    // if id is set then get the file with the id from database
    include 'open_db.inc';
    $name    $_GET['download'];
    $query "SELECT name, type, size, content " .
             
    "FROM upload WHERE name = '$name'";
    $result mysql_query($query) or die('Error, query failed');
    list(
    $name$type$size$content) = mysql_fetch_array($result);
    header("Cache-Control: maxage=1"); //In seconds
    header("Pragma: public");
    header("Content-length:$size");
    header("Content-type:$type");
    header("Content-type:application/pdf");
    header("Content-Disposition: attachment;filename=\"$name\"");
    header("Content-Description: PHP Generated Data");
    header("Content-transfer-encoding: binary");
    ob_end_clean();
    echo 
    $size;
    echo 
    $type;
    $content stripslashes ($content);
    echo 
    $content;
    exit;
    }
    ?>
    Last edited by ScallioXTX; Jun 2, 2012 at 06:17. Reason: wrapped php in [php]...[/php]

  2. #2
    SitePoint Addict tom8's Avatar
    Join Date
    Mar 2012
    Location
    New Jersey
    Posts
    310
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    I dont see how you could get that error. Try the script below and see if it works with content from the regular file. If it is ok then I would check the database query and make sure the content is valid.

    PHP Code:
    <?php
       
    if(isset($_GET['download']))
       {
           
    $name $_GET['download'];
           
    $type filetype ($name);
           
    $content file_get_contents($name);
           
    $size strlen($content);
           if(!
    $size)
           {
               echo 
    "File $name contains no data!";
               exit;
           }
           
    header("Content-Length:$size");
           
    header("Content-type:$type");
           
    header("Content-Disposition: attachment;filename=$name");
           echo 
    $content;
           exit;
       }
       echo 
    "No file specified!";
    ?>

  3. #3
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Try opening it up in notepad++ or another lightweight text editor. Is there anything unexpected at the beginning of the file? Like... for example... the size and type shouldn't be in the actual file output;

    Also note that you should check that the database record actually exists first.

    Try the following:
    PHP Code:
    <?php
    if(isset($_GET['download']))
    {
        
    // if id is set then get the file with the id from database
        
    include 'open_db.inc';
        
    $name $_GET['download'];
        
    $query "SELECT name, type, size, content " .
             
    "FROM upload WHERE name = '$name'";
        
    $result mysql_query($query) or die('Error, query failed');
        if(
    $row mysql_fetch_array($result)){
            list(
    $name$type$size$content) = $row;
            
    ob_end_clean();
            
    header("Cache-Control: maxage=1"); //In seconds
            
    header("Pragma: public");
            
    header("Content-length:$size");
            
    header("Content-type:$type");
            
    header("Content-type:application/pdf");
            
    header("Content-Disposition: attachment;filename=\"$name\"");
            
    header("Content-Description: PHP Generated Data");
            
    header("Content-transfer-encoding: binary");
            echo 
    stripslashes($content);
            exit;
        }else{
            echo 
    "File does not exist";
        }
    }else{
        echo 
    "No download selected";
    }
    ?>
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  4. #4
    SitePoint Member
    Join Date
    Jun 2012
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks Arkinstall.

    I have tried to open the file in notepad and I get this

    347567application/pdf%PDF-1.4
    %
    1 0 obj
    <</CreationDate (D:20120601151956)/ModDate ()/Producer (PDF Complete version 3.5.1.1)/Title (Microsoft Word - JMPR-10-176 Harun et al)>>

    with some other corrupted documents.

    I tried working with the codes you gave me, but the pdf downloads but has nothing on it, the message still remains "The selected file can not be opened". I am still open for more suggestions. Thanks once again.

  5. #5
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    With my code, what does notepad output as the file?
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  6. #6
    SitePoint Member
    Join Date
    Jun 2012
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks tom8. I have tried using your codes but no improvement. When I checked my DB, I find that the file had been uploaded. Funny enough, when I download, it gives me the exact size that was uploaded into the DB but it comes blank with the message I mentioned earlier. I am still open to further suggestions.

  7. #7
    SitePoint Member
    Join Date
    Jun 2012
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    %PDF-1.4
    %
    1 0 obj
    <</CreationDate (D:20120601151956)/ModDate ()/Producer (PDF Complete version 3.5.1.1)/Title (Microsoft Word - JMPR-10-176 Harun et al)>>

    That is what I get with your code with the corrupted file below this. Thanks for your assistance.

  8. #8
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Ok, what's at the very bottom of the file?
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  9. #9
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Note that the general format should be:
    Code:
    %PDF-1.3
    %*
    4 0 obj
    <<... stuff ...>>
    {Some ugly code}
    %%EOF
    With that empty line after %%EOF present
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  10. #10
    SitePoint Member
    Join Date
    Jun 2012
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks so much Jake. I have resolved it with these codes:

    <?php
    if(isset($_GET['download']))
    {
    // if id is set then get the file with the id from database
    include 'open_db.inc';
    //include 'library/opendb.php';
    $name = $_GET['download'];
    $query = "SELECT name, type, size, content " .
    "FROM upload WHERE name = '$name'";
    $result = mysql_query($query) or die('Error, query failed');
    list($name, $type, $size, $content) = mysql_fetch_array($result);
    ob_end_clean();
    header("Cache-Control: maxage=1"); //In seconds
    header("Pragma: public");
    header("Content-length:$size");
    header("Content-type:$type");
    header("Content-type:application/pdf");
    header("Content-Disposition: attachment;filename=\"$name\"");
    header("Content-Description: PHP Generated Data");
    header("Content-transfer-encoding: binary");
    ob_end_clean();
    //$content = stripslashes ($content);
    echo $content;
    //while (@ob_end_clean());
    //include 'library/closedb.php';
    exit;
    }else{
    echo "File does not exist";
    }
    ?>


    It was suggested by a friend that the two echos preceding echo $content should be removed. I did that and it working well. Thanks for your assistance. I am excedingly grateful.


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
  •