#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    2
    Rep Power
    0

    Arrow Downloading from mysql databse


    hey pros, i have a problem with my download.php page i have a database with a table that stores pdf files and these files are displayed on the files page and want users to download them
  2. #2
  3. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    119
    Rep Power
    33
    Originally Posted by odongo
    hey pros, i have a problem with my download.php page i have a database with a table that stores pdf files and these files are displayed on the files page and want users to download them
    So what problem are you having and what is the relevant code causing the problem???
  4. #3
  5. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,908
    Rep Power
    6351
    Yeah...what's the problem here? You list files and you want to make them clickable? Do that, see what happens. Look into readfile() and the header() function to send the appropriate file headers for PDFs (google will help with that)
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    2
    Rep Power
    0

    here is the code


    $fileId = $_GET["fileId"];
    $sql = "SELECT File_Type, File_Name, File_Size, File_Bytes FROM exams WHERE File_ID = ?";
    $dsn = 'mysql:dbname=fred;host=localhost;port=3306';
    $user = 'root';
    $password = '';

    try {
    $dbh = new PDO($dsn, $user, $password);

    $stmt = $dbh->prepare($sql) or die (implode(':', $stmt->errorInfo()));
    $stmt->bindParam(1, $fileId, PDO::PARAM_INT, 60);
    $stmt->execute() or die (implode(':', $stmt->errorInfo()));

    $row = $stmt->fetch();
    header("Content-type: $row[0]");
    header("Content-length: $row[2]");
    header("Content-disposition: attachment; filename=$row[1]");
    print $row[3];

    } catch (PDOException $e) {
    die ('Connection failed: '.$e->getMessage());
    }
    ?>
  8. #5
  9. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Sep 2002
    Location
    Seattle, U.S.A.
    Posts
    712
    Rep Power
    13
    Are you getting an error message? Also wrap code with the php tags.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2009
    Posts
    677
    Rep Power
    7
    Just a total guess... Is your SQL statement supposed to end as "WHERE File_ID = ?". It seems your '?' should be replaced with your $fileId value.
  12. #7
  13. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,908
    Rep Power
    6351
    try/catch only catches exceptions. PDO doesn't throw an exception when a query fails. You need to handle the errors yourself.
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.

IMN logo majestic logo threadwatch logo seochat tools logo