yes it forces download but when i chose to save or open file just empty file opened..!! if i try to open pdf or doc files just empty file opened but it works with plain/text files..but i need it to work with pdf files :$
Uploading files into a MySQL database using PHP
Collapse
X
-
Great tutorial Atli
I was wanting to be able to add accompanying text along with uploading the file. I get I'd need to add text fields to the form and in sert new fields in the MySql database that would need populating but I can't figure out how I would gather that data in the add_file.php
Cheers
RobComment
-
Jim Gordon
Would anyone happen to have a link to the older version of this code which doesnt use mysqli?
Much appreciated
JimmyComment
-
Sagar Joshi
Hey Guys i have been in PHP coding for about 6 months and also tried out this very fantastic tutorial of downloading and uploading.... well upload works like charm! but so does download but when i download the file such as doc file or image file i am not able to view and image shows that it is not supported and word shows that it is corrupted or unsupported format! pls i need help! thanking you in advance!Comment
-
Hey Guys i have been in PHP coding for about 6 months and also tried out this very fantastic tutorial of downloading and uploading.... well upload works like charm! but so does download but when i download the file such as doc file or image file i am not able to view and image shows that it is not supported and word shows that it is corrupted or unsupported format! pls i need help! thanking you in advance!
If you are getting corrupted files, a likely reason is that PHP is printing errors or warnings into the file. To see those errors you can simply change line #32 in the get_file.php script so it reads:header('Content-Type: text/html');
, and then you need to comment out line #34.
This should print the error, along with all the data, into your browser instead of you getting a download dialog. Then you can browse through it and read the errors/warnings being printed.Comment
-
Kenneth Roggers
-
Hi Atli,
I you help me change the mysqli to mysql?
Code:<?php // Check if a file has been uploaded if(isset($_FILES['uploaded_file'])) { // Make sure the file was sent without errors if($_FILES['uploaded_file']['error'] == 0) { // Connect to the database $dbLink = new mysqli('localhost', 'root', '###', '###'); if(mysqli_connect_errno()) { die("MySQL connection failed: ". mysqli_connect_error()); } // Gather all required data $name = $dbLink->real_escape_string($_FILES['uploaded_file']['name']); $mime = $dbLink->real_escape_string($_FILES['uploaded_file']['type']); $data = $dbLink->real_escape_string(file_get_contents($_FILES ['uploaded_file']['tmp_name'])); $size = intval($_FILES['uploaded_file']['size']); // Create the SQL query $query = " INSERT INTO `file` ( `name`, `mime`, `size`, `data`, `created` ) VALUES ( '{$name}', '{$mime}', {$size}, '{$data}', NOW() )"; // Execute the query $result = $dbLink->query($query); // Check if it was successfull if($result) { echo 'Success! Your file was successfully added!'; } else { echo 'Error! Failed to insert the file' . "<pre>{$dbLink->error}</pre>"; } } else { echo 'An error accured while the file was being uploaded. ' . 'Error code: '. intval($_FILES['uploaded_file']['error']); } // Close the mysql connection $dbLink->close(); } else { echo 'Error! A file was not sent!'; } // Echo a link back to the main page echo '<p>Click <a href="index.html">here</a> to go back</p>'; ?>
Comment
-
I like above code that is very simple and useful
but its giving me error on
db->close();
while uploading the fileComment
Comment