473,378 Members | 1,360 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

error in downloading files in php

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(including videos),i need the exact format when it is downloaded..so please help me with this..
Apr 8 '13 #1
19 1660
here the code to download files.
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $fileId = $_GET["fileId"];
  3. $sql = "SELECT buk_file_type, buk_file_name, buk_file_size, buk_file_bytes FROM books WHERE buk_file_id = $fileId";
  4. $dsn = 'mysql:dbname=lr;host=localhost;';
  5. $user = 'root';
  6. $password = '';
  7.  
  8.  
  9. try{
  10.  
  11. $stmt = $dbh->prepare($sql) or die(implode(':', $stmt->errorInfo()));
  12. $stmt->bindParam(1, $fileId, PDO::PARAM_INT,60);
  13. $stmt->execute() or die(implode(':', $stmt->errorInfo()));
  14. $cols = $stmt->columnCount();
  15. $row = $stmt->fetch();
  16. header("Content-type: $row[0]");
  17. header("Content-length: $row[2]");
  18. header("Content-disposition: attachment; filename=$row[1]");        
  19. print $row[3];
  20. }
  21.     catch(PDOException $e){
  22.                 die ('Connection Failed:' .$e->getMessage());
  23.             }
  24. ?>
Apr 8 '13 #2
Dormilich
8,658 Expert Mod 8TB
are you sure the code works at all? I’d expect an error when I try to bind a parameter to a non-existing placeholder.
Apr 9 '13 #3
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 advance
Apr 9 '13 #4
Dormilich
8,658 Expert Mod 8TB
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.
Apr 9 '13 #5
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.....
Expand|Select|Wrap|Line Numbers
  1.  <?php 
  2. include 'core/init.php';
  3. include 'includes/overall/header.php';
  4. ?>
  5.       <h1>Questions Papers</h1>
  6.         <?php
  7.         if(isset($_POST['cmdSubmit']) && $_FILES['userFile']['size'] > 0) {
  8.             extract( $_POST );
  9.             $fileName = $_FILES['userFile']['name'];
  10.             $tmpName  = $_FILES['userFile']['tmp_name'];
  11.             $fileSize = $_FILES['userFile']['size'];
  12.             $fileType = $_FILES['userFile']['type'];
  13.             if(!get_magic_quotes_gpc()) {
  14.                 $fileName = addslashes($fileName);
  15.             }
  16.             move_uploaded_file ($tmpName, "/temp/$fileName");
  17.             $tmpName = "/temp/$fileName";
  18.  
  19. #$tmpName="c:/tmp/test.txt";
  20.             //echo "<h1>Opening: $tmpName </h1>";
  21.  
  22.             $fp      = fopen($tmpName, 'r');
  23.             $content = fread($fp, filesize($tmpName));
  24. #        $content = addslashes($content);
  25.             fclose($fp);
  26.  
  27.             $dsn = 'mysql:dbname=lr;host=localhost;';
  28.             $user= 'root';
  29.             $password='';
  30.  
  31.             try {
  32.                 $dbh = new PDO($dsn, $user, $password);
  33.  
  34.                 $insertSQL = $dbh->prepare(
  35.                         "INSERT INTO documents (email, full_name, file_name, " .
  36.                             "file_size, file_bytes, file_type, received) VALUES (?,?,?,?,?,?,SYSDATE())")
  37.                         or die ($mysqli->error);
  38.  
  39.                 $insertSQL->bindParam(1, $email, PDO::PARAM_STR, 60);
  40.                 $insertSQL->bindParam(2, $fullName, PDO::PARAM_STR, 60);
  41.                 $insertSQL->bindParam(3, $fileName, PDO::PARAM_STR, 60);
  42.                 $insertSQL->bindParam(4, $fileSize, PDO::PARAM_INT, 60);
  43.                 $insertSQL->bindParam(5, $content, PDO::PARAM_STR, $fileSize);
  44.                 $insertSQL->bindParam(6, $fileType, PDO::PARAM_STR, 60);
  45.  
  46.                 echo "<h1>About to upload $fileName to database! $fileSize</h1>";
  47.                 $insertSQL->execute() or die ("<p>Upload Error: $insertSQL->error</p>");
  48.             } catch (PDOException $e) {
  49.                 die ('Connection failed: '.$e->getMessage());
  50.             }
  51.  
  52.             echo "<br>File $fileName uploaded<br>";
  53.         }
  54.         ?>
  55.         <a href="questions.php">Return to file list</a>
  56.  
  57.  
  58.  
  59.  
  60. <?php include 'includes/overall/footer.php';?>
and here's my db sql
Expand|Select|Wrap|Line Numbers
  1. CREATE TABLE IF NOT EXISTS `documents` (
  2.   `file_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  3.   `email` varchar(250) DEFAULT NULL,
  4.   `full_name` varchar(250) DEFAULT NULL,
  5.   `file_name` varchar(250) DEFAULT NULL,
  6.   `file_size` int(11) DEFAULT NULL,
  7.   `file_type` varchar(250) DEFAULT NULL,
  8.   `file_bytes` mediumblob,
  9.   `received` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  10.   PRIMARY KEY (`file_id`)
  11. ) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=17 ;
  12.  
  13.  
Apr 10 '13 #6
Dormilich
8,658 Expert Mod 8TB
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.....
that was not the question. it was "what MIME type did you receive upon downloading?"

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())
Apr 10 '13 #7
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!
Apr 10 '13 #8
Dormilich
8,658 Expert Mod 8TB
what browser do you use?
Apr 10 '13 #9
oh! sorry for late reply..i use google chrome
Apr 10 '13 #10
Dormilich
8,658 Expert Mod 8TB
finding headers in Chrome:
- open the developer tools (F12)
- go to "Network" tab
- load page (download script) in the browser window
- check what’s written in the "Status text" column in the Dev Tools
Apr 11 '13 #11
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 php
Apr 11 '13 #12
Dormilich
8,658 Expert Mod 8TB
argh, for the MIME type you need of course the "Type" field (should be the next one), my bad.
Apr 11 '13 #13
sorry!..its showing type='text/plain' status="cancelled"...what it indicates?? and what should i do to correct it...
Apr 12 '13 #14
Dormilich
8,658 Expert Mod 8TB
you probably have an error interferring ...

... or you really send a text header.
Apr 12 '13 #15
thank you sir!..i fixed the problem!...now, i want the php code to see online users. so can you help me with this or can you send any link regarding to this?? thanks in advance
Apr 13 '13 #16
Dormilich
8,658 Expert Mod 8TB
i want the php code to see online users
what does that mean?
Apr 13 '13 #17
php code to display the users who are online?
Apr 14 '13 #18
Dormilich
8,658 Expert Mod 8TB
how should I know?

you’ll probably find sufficient scripts via google, but I imagine that you need some kind of intermediat storage (DB, File) to have cross-user access.
Apr 14 '13 #19
ok..thank you sir! for having time with me..
Apr 15 '13 #20

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Kajol | last post by:
Hi I am facing a strange problem in downloading files from the web. CASE 1: I have an ActiveX.dll file developed using Visual Basic 6.0 and uploaded on a Linux Server. I am typing...
6
by: Tony K | last post by:
I have the most peculiar problem with an ASP.NET page which we use for downloading a file. When the user clicks on a link, the link points to an ASPX page which downloads the file selected. ...
8
by: Ken | last post by:
Can anyone point me to some info, which gives me an idea of how to go about making an ASP.NET web service to download files I plan to access the web service from a C# Windows App. Thanks. ...
4
by: PiotrKolodziej | last post by:
hi I have a thread that downloades a file. Problem is : Not all files are beeing downloaded. I observed that only the small files are beeing downloaded correctly. I also cant download two files...
0
by: ed | last post by:
Hello All. I have seen many posts slightly similar to this question with no answer. Maybe you have some FAQs or something, sorry if I looks repetitive. Here is my Problem: In my...
4
by: c83 | last post by:
I am using Active Perl, and i-am having problems downloading files from internet because of the proxy. The script is workin when no proxy is present, but now it refuses to download files. Here is...
7
by: Ehsan | last post by:
I foundd this code in ASPN Python Cookbook for downloading files in python but when it finished downloading files the files became corrupted and didn't open, the files in internet havn't any...
221
Atli
by: Atli | last post by:
You may be wondering why you would want to put your files “into” the database, rather than just onto the file-system. Well, most of the time, you wouldn’t. In situations where your PHP application...
2
by: Paulo | last post by:
Hi, after generating a text file on server and showing the path on a hyperlink component to user download the file, shows me the error below: "There is no build provider registered for the...
1
by: getin4bob | last post by:
Please provide me with the asp.net C# code for downloading files from ftp server to client
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.