• I have a php script that downloads files stored in a database. It works fine outside of WordPress. When I put the same code in the plugin and insert the snippet in a page, it tries to download the file, but the file is corrupted and won’t open. This happens with multiple file types (pdf, xlsx).

    Any ideas why this might be happening.

Viewing 8 replies - 1 through 8 (of 8 total)
  • Plugin Author webcraftic

    (@webcraftic)

    Hi,

    I’m sorry, I needed to update other plugins. Now I’m back to you and can help you.

    Please show your code so I can figure out why this is happening.

    Best regards, Alex

    Thread Starter chrisbeck

    (@chrisbeck)

    Alex,
    Here is the code.

    $sql = “SELECT uploaded_file, uploaded_file_name FROM v36 WHERE chapter = $chapter”;

    $result = @mysql_query($sql);
    $data = @mysql_result($result, 0, “uploaded_file”);
    $name = @mysql_result($result, 0, “uploaded_file_name”);
    $filename=$name.”.pdf”;

    header(“Content-type: application/pdf”);
    header(“Content-Description: PHP Generated Data”);
    header(“Content-Disposition: inline; filename=”.$filename);

    echo $data;

    I have tried alternative code with mysqli and mysqli_fetch_assoc(). I have tried changing headers as well. If I use the code in a normal php page, it runs fine, but if I embed it within a WP page, it tries to download the pdf, but the pdf is corrupted.

    Thanks,
    Chris

    Plugin Author webcraftic

    (@webcraftic)

    Hi,

    Create a snippet that needs to be run everywhere. Look at my code below, it works fine. You can apply this example to your code.

    This code will work for you if you got to link https://yoursite.test/?download_pdf=1

    function wbcr_download_pdf() {
      	if(isset($_GET['download_pdf'])) {
            $file_path = "tech-task.pdf";
            
            if(!file_exists($file_path)) {
               echo 'PDF file not found!';
               exit;
            }
          	
            header('Content-type: application/pdf');
    	header('Content-Description: PHP Generated Data');
            header('Content-Disposition: attachment; filename='.$file_path);   
    	// The PDF source is in original.pdf
    	readfile($file_path);
            exit;
        }
    }
    
    add_action('init', 'wbcr_download_pdf');

    The most important thing that you need to consider is the right way to pdf files. My additional check will let you know if the path is correct or not.

     if(!file_exists($file_path)) {
               echo 'PDF file not found!';
               exit;
            }

    Best regards, Alex

    Thread Starter chrisbeck

    (@chrisbeck)

    Alex,
    I should have been clearer in my original post. The files are stored as longblobs in a MySQL database. They aren’t residing on the server.

    The php script that I have used in the past retrieves the file using a query and then echoes it. The thing that is confusing is that it works fine outside of the WP context but returns a corrupted pdf in the WP context.

    Thanks,
    Chris

    Plugin Author webcraftic

    (@webcraftic)

    Hi, Chris

    I can not verify your code, because I do not have a copy of your database.

    You have provided too little information about your code.

    Best regards, Alex

    Plugin Author webcraftic

    (@webcraftic)

    Duplicate

    • This reply was modified 7 years, 6 months ago by webcraftic.
    Plugin Author webcraftic

    (@webcraftic)

    Duplicate

    • This reply was modified 7 years, 6 months ago by webcraftic.
    Plugin Author webcraftic

    (@webcraftic)

    Duplicate

    • This reply was modified 7 years, 6 months ago by webcraftic.
    • This reply was modified 7 years, 6 months ago by webcraftic.
Viewing 8 replies - 1 through 8 (of 8 total)

The topic ‘Files stored in database corrupted when downloaded’ is closed to new replies.