i've written the code to download the files from database, there it includes videos(3gp,mp4, flv etc.)and pdf's.But the problem is..when i click download it downloads the file in txt format(includin g videos),i need the exact format when it is downloaded..so please help me with this..
error in downloading files in php
Collapse
X
-
Tags: None
-
here the code to download files.Code:<?php $fileId = $_GET["fileId"]; $sql = "SELECT buk_file_type, buk_file_name, buk_file_size, buk_file_bytes FROM books WHERE buk_file_id = $fileId"; $dsn = 'mysql:dbname=lr;host=localhost;'; $user = 'root'; $password = ''; try{ $stmt = $dbh->prepare($sql) or die(implode(':', $stmt->errorInfo())); $stmt->bindParam(1, $fileId, PDO::PARAM_INT,60); $stmt->execute() or die(implode(':', $stmt->errorInfo())); $cols = $stmt->columnCount(); $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()); } ?>
-
thanks for the reply!..i don't have any idea about php..i am just a beginner to it...i just got this code and executed though..but the problem is the downloaded files are in txt form...so, can you give the code to download files from database?? thanks in advanceComment
-
if the downloaded files are in text form there are mainly 2 things to do:
- check the MIME-Type of the received file (I’d temporarly disable the content-disposition for that, so you can better examine the Response headers)
- check the MIME-Types saved in the DB
additionally, in which error mode is PDO running? (the default is no message)
PS. your try...catch gives false information. the connection is established outside of it. and there is no need for die() in a catch() statement.
PPS. rule of thumb: only catch an exception when you can handle the problem.Comment
-
thanks! this is my upload code, here i am storing the MIME type in a variable and then inserted it into database using varchar to store it in DB.....Code:<?php include 'core/init.php'; include 'includes/overall/header.php'; ?> <h1>Questions Papers</h1> <?php if(isset($_POST['cmdSubmit']) && $_FILES['userFile']['size'] > 0) { extract( $_POST ); $fileName = $_FILES['userFile']['name']; $tmpName = $_FILES['userFile']['tmp_name']; $fileSize = $_FILES['userFile']['size']; $fileType = $_FILES['userFile']['type']; if(!get_magic_quotes_gpc()) { $fileName = addslashes($fileName); } move_uploaded_file ($tmpName, "/temp/$fileName"); $tmpName = "/temp/$fileName"; #$tmpName="c:/tmp/test.txt"; //echo "<h1>Opening: $tmpName </h1>"; $fp = fopen($tmpName, 'r'); $content = fread($fp, filesize($tmpName)); # $content = addslashes($content); fclose($fp); $dsn = 'mysql:dbname=lr;host=localhost;'; $user= 'root'; $password=''; try { $dbh = new PDO($dsn, $user, $password); $insertSQL = $dbh->prepare( "INSERT INTO documents (email, full_name, file_name, " . "file_size, file_bytes, file_type, received) VALUES (?,?,?,?,?,?,SYSDATE())") or die ($mysqli->error); $insertSQL->bindParam(1, $email, PDO::PARAM_STR, 60); $insertSQL->bindParam(2, $fullName, PDO::PARAM_STR, 60); $insertSQL->bindParam(3, $fileName, PDO::PARAM_STR, 60); $insertSQL->bindParam(4, $fileSize, PDO::PARAM_INT, 60); $insertSQL->bindParam(5, $content, PDO::PARAM_STR, $fileSize); $insertSQL->bindParam(6, $fileType, PDO::PARAM_STR, 60); echo "<h1>About to upload $fileName to database! $fileSize</h1>"; $insertSQL->execute() or die ("<p>Upload Error: $insertSQL->error</p>"); } catch (PDOException $e) { die ('Connection failed: '.$e->getMessage()); } echo "<br>File $fileName uploaded<br>"; } ?> <a href="questions.php">Return to file list</a> <?php include 'includes/overall/footer.php';?>
Code:CREATE TABLE IF NOT EXISTS `documents` ( `file_id` int(10) unsigned NOT NULL AUTO_INCREMENT, `email` varchar(250) DEFAULT NULL, `full_name` varchar(250) DEFAULT NULL, `file_name` varchar(250) DEFAULT NULL, `file_size` int(11) DEFAULT NULL, `file_type` varchar(250) DEFAULT NULL, `file_bytes` mediumblob, `received` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, PRIMARY KEY (`file_id`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=17 ;
Comment
-
this is my upload code, here i am storing the MIME type in a variable and then inserted it into database using varchar to store it in DB.....
some comments to the code:
- never do a DB connection with the root user unless for DB administration. ever!
- don’t use addslashes() to escape DB content. a prepared statement will retain them and \ is not an escape character in HTML or plain text.
- don’t die() in a catch() statement (see above)
- the$mysqli
variable does not exist
- so does the->error
property (PDO uses->errorInfo()
)Comment
-
oh ok!..thank you for your patience having with me.. can you change the above code and send it back here?? because i didn't understand what you said, so please help me!Comment
-
-
thank you!..the status displayed 'success'..if you don't mind will you give your email id please?? i can clarify many doubts with you regarding phpComment
-
sorry!..its showing type='text/plain' status="cancell ed"...what it indicates?? and what should i do to correct it...Comment
Comment