php - Download a pdf file using path saved in database? -


i want download pdf file folder when try read file gives error "failed load pdf". when try download it, corrupt file downloaded.

here code:

<?php      $con=mysqli_connect("localhost","root","");     if(!isset($con)){         echo "database not connected";     }     $db=mysqli_select_db($con,"mahmood_faridi");     $query=("select * books");     $result=mysqli_query($con,$query);     while(list($id,$file)=mysqli_fetch_array($result)){         echo "<a href=\"download.php?id=\$id\">$file</a><br>";     }      if(isset($_get['id'])){         $id    = $_get['id'];            $query = "select  link books id = '$id' limit=1";         $result=mysqli_query($con,$query);         $file=mysqli_fetch_object($result);          header('content-type: application/pdf');         header('content-disposition: inline');         header('content-transfer-encoding: binary');         header('accept-ranges: bytes');          @readfile($file);     }     mysqli_close($con); ?> 

in script there many errors.

when write php script, first of all, it's practice activate error reporting; not caprice: showing errors our first allied in code debugging. activate error reporting, put 2 line @ top of script:

error_reporting( e_all ); ini_set( "display_errors", 1 ); 

this not resolve error reporting, because compile errors (like if write eco instead of echo) omitted, , see blank page. when occur, check code fails, have read apache logfile. can find location of apache logfile googling “your os apache error log default path”. so, if use os x, can search "mac apache error log default path".

when sure code works fine, preferable remove (or comment) these line, security , esthetic reasons.

another important ally, newbie, php official documentation; copy-and-paste can good, if followed careful reading of code: need understand every command! in case, if had read chapter header(), understand that

header() must called before actual output sent, either normal html tags, blank lines in file, or php. common error read code include, or require, functions, or file access function, , have spaces or empty lines output before header() called.

this first big error: first display list of available files, call header() function, fails. if had activate error reporting, mistake have been reported.

so, code must continue in way:

if( !isset($con) ) die( "database not connected" ); $db = mysqli_select_db( $con, "mahmood_faridi" ) or die( "unable select database" );  if( isset( $_get['id'] ) ) {     $id    = $_get['id'];     $query = "select link books id = '$id' limit 1"; 

then, check result. in our original code, write where id = '$id' limit=1. if had checked result, have got message:

query error: have error in sql syntax; check manual corresponds mysql server version right syntax use near '=1' @ line 1

and have realized wrong in query.

    $result = mysqli_query( $con, $query ) or die( "query error: " . mysqli_error( $con ) );      if( !mysqli_num_rows( $result ) ) die( "id $id not found" );      $file   = mysqli_fetch_object( $result ); 

then, check if file exists. in original code, write readfile( $file ), $file object ($file = mysqli_fetch_object( $result )), not filepath. filepath $file->link.

if had checked if file exists , activated error reporting, have got these messages:

warning: file_exists() expects parameter 1 valid path, object given in /your/script/path.php on line xx
file not found

and have kind of error , line check.

    if( ! file_exists( $file->link ) ) die( "file not found" );      header( 'content-type: application/pdf' );     header( 'content-disposition: inline' );     header( 'content-transfer-encoding: binary' );     header( 'accept-ranges: bytes' );      @readfile( $file->link ); 

also important, place die() or exit after readfile(), otherwise script output following html code, , pdf file result corrupt:

    die(); } 

now, can place code display files list (that executed if pdf file not sended); in case, check result step-by-step:

$query = "select * books";     // <-- brackets superfluous, not error if( ! $result = mysqli_query( $con, $query ) ) die( "query error: " . mysqli_error( $con ) );  if( !mysqli_num_rows( $result ) ) die( "database empty" ); 

in original while loop, write url "<a href=\"download.php?id=\$id\": backslash (\) behind double-quoted string character escape means “not interpret-it, print-it”, dollar sign printed as-it , variable not passsed. effective link 'http://example.com/download.php?id=$id' instead of 'http://example.com/download.php?id=1'. watch out little things.

while( list( $id, $file ) = mysqli_fetch_array( $result ) ) {     echo "<a href=\"download.php?id=$id\">$file</a><br>"; }  mysqli_close($con); 

... , script done.


Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -