DeluXe Network Forums
March 15, 2010, 01:48:16 pm *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
News: Please do not add ads in your signature!
 
   Home   Help Search Members Login Register  
Pages: [1]
  Print  
Author Topic: Upload files to MySQL using PHP  (Read 694 times)
komunikol
Full Member
***

Honor: 0
Offline Offline

Posts: 132



View Profile WWW
« on: September 14, 2009, 10:25:03 am »

Hi there,
I spent last few days trying to figure out how to upload files (like .doc .xls .pdf) to a MySQL database, using php pages.
I'm making a small cms for myself. I can't find a good set of insructions that will help me make php page that will allow me to upload a file to a sql database, and also to allow me to download/list/delete all the stored files.

Can somebody link me a good tutorial/example that is proven to work on dhost? Tried a coupple of them myself but they fail.

Question #2: what is your oppinion on storing files in sql table? Is it better/worse than direct storing to file system?

Thanks

P.S. this is what I've tried, but it won't work, the file as well as it's description aren't stored in mysql table... (from http://php.about.com/od/phpbasics/ss/mysql_files_3.htm)
Code:
CREATE TABLE uploads (id INT(4) NOT NULL AUTO_INCREMENT PRIMARY KEY, description CHAR(50), data LONGBLOB, filename CHAR(50), filesize CHAR(50), filetype CHAR(50) );

now the file file.php
Code:
<form method="post" action="upload.php" enctype="multipart/form-data">
Description:<br>
<input type="text" name="form_description" size="40">
<input type="hidden" name="MAX_FILE_SIZE" value="1000000">
<br>File to upload:<br>
<input type="file" name="form_data" size="40">
<p><input type="submit" name="submit" value="submit">
</form>

now the file upload.php
Code:
<?php
// identifikacija
include("dbinfo.incl.php");
mysql_connect(mysql,$username,$password);
@
mysql_select_db($database) or die( "Unable to select database");

$data addslashes(fread(fopen($form_data"r"), filesize($form_data)));
$result=MYSQL_QUERY("INSERT INTO uploads (description, data,filename,filesize,filetype) ""VALUES ('$form_description','$data','$form_data_name','$form_data_size','$form_data_type')");
$idmysql_insert_id();
print 
"<p>File ID: <b>$id</b><br>";
print 
"<p>File Name: <b>$form_data_name</b><br>";
print 
"<p>File Size: <b>$form_data_size</b><br>";
print 
"<p>File Type: <b>$form_data_type</b><p>";
print 
"To upload another file <a href=fajl.php> Click Here</a>";
?>


and the result should be download.php
Code:
<?php
// identifikacija
include("dbinfo.inc.php");
mysql_connect(mysql,$username,$password);

@
mysql_select_db($database) or die( "Unable to select database");
$query "SELECT data,filetype FROM uploads where id=$id";
$result MYSQL_QUERY($query);
$id=mysql_result($result,$i,"id");
$data MYSQL_RESULT($result,0,"data");
$type MYSQL_RESULT($result,0,"filetype");
Header"Content-type: $type");
echo 
"broj $id";
print 
$data;
?>
allas, this isn't working, mysql creates a new row, but the name, description and the file itself are not stored in the database, just empty fields, and the fields filename, filesize and filetype become NULL... Sad
« Last Edit: September 14, 2009, 10:34:58 am by komunikol » Logged

Michael
Administrator
Hero Member
*****

Honor: 11
Offline Offline

Germany Germany

Posts: 3253



View Profile WWW
« Reply #1 on: September 14, 2009, 11:09:40 pm »

hm, I think the issue behind is that many documents are simply to huge for the database. As you need to transfer them in one go. There's a limitation on "packet size" which you cannot exceed, one packet = one query. really it makes more sense to use file-based storage
Logged
darkspin
Newbie
*

Honor: 1
Offline Offline

United States United States

Posts: 23


View Profile
« Reply #2 on: October 08, 2009, 07:15:52 am »

hm, I think the issue behind is that many documents are simply to huge for the database. As you need to transfer them in one go. There's a limitation on "packet size" which you cannot exceed, one packet = one query. really it makes more sense to use file-based storage

I second that.
Logged
Pages: [1]
  Print  
 
Jump to:  

 

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Page created in 0.427 seconds with 16 queries.