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)
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<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<?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')");
$id= mysql_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
<?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...
