Page 1 of 1

Reading the content of files

Posted: Sat Jun 16, 2012 10:56 am
by Pavilion
Hello:

I'm working on an exercise in which users can upload multiple files at the same time, give them descriptions and then INSERT said files with descriptions into a mySQL table.

So far there is a successful user inter-face up and running (mostly used jQuery). Now I am trying to process the user input through PHP. Following is the applicable PHP script:

Code: Select all

<?php
// Array used to push/insert data
$merged_array=array();
// Arrays used to process files
$up_array=array();
$names_array=array();
$types_array=array();
$temp_array=array();
$desc_array = array();
// set up increment counter
$line_counter=-1;


if (!empty($_POST)) 
{
$desc_array=array("description"=>$_POST['descriptions']);

if (!empty($_FILES))
{
$up_array=$_FILES['uploads'];
$names_array=array("name"=>$up_array["name"]);
$types_array=array("type"=>$up_array["type"]);
$temp_array=array("temp"=>$up_array["tmp_name"]);

$merged_array = array_merge($names_array, $types_array, $temp_array, $desc_array);
$file_ct=count($names_array,1);

while ($line_counter < $file_ct-2) {
	$line_counter++;
foreach($merged_array as $line) {
	$file_name=$merged_array["name"];
	$file_type=$merged_array["type"];
	$file_temp=$merged_array["temp"];
	$file_desc=$merged_array["description"];
	echo $content;
} // end foreach
	
echo "Data packet: " . $file_name[$line_counter] . ", " . $file_type[$line_counter] . ", " . $file_temp[$line_counter] . ", " . $file_desc[$line_counter] . "<br />";	
} // end while

}
}
?>
The echo data packet is returning everything I expect. But... how can I get it to echo the actual file content?

As stated earlier, my main goal is to store these files (with descriptions) in a mySQL table. Storing file name, size, type is NOT the same thing as storing content. How do I attach the file content to a variable so that I can INSERT into mySQL?

Thanks in advance - Pavilion

Re: Reading the content of files

Posted: Sat Jun 16, 2012 7:00 pm
by xtiano77
You need to open the uploaded file and read its contents into a variable before you can echo it or store it into MySQL. For example:

Code: Select all

<?php
     $fh = fopen($_FILES["tmp_name", 'r');
     $content = fread($fh, filesize($_FILES["tmp_name"]));
     $content = addslashes($content);
     fclose($fh);
?>

Re: Reading the content of files

Posted: Sat Jun 16, 2012 8:39 pm
by califdon
Pavilion wrote:As stated earlier, my main goal is to store these files (with descriptions) in a mySQL table. Storing file name, size, type is NOT the same thing as storing content. How do I attach the file content to a variable so that I can INSERT into mySQL?
As a general rule, databases operate much more efficiently when you don''t store large objects such as files and images. The customary way to handle such storage is to store the images or other large files in folders on the server, and use the database to index them (that is, locate them by key words, etc.). That's certainly not a rule that applies to all cases, but it probably is better to start from there and then justify storing the entire contents if the situation requires it.

Relational databases were designed to hold DATA, in the sense that each column of a table should contain "atomic" values--that is, raw data that cannot be broken into smaller elements. Again, there are valid situations when you must violate that principle, but it takes a lot of experience and knowledge to make a good decision to do so.

Re: Reading the content of files

Posted: Sat Jun 16, 2012 9:09 pm
by Pavilion
califdon wrote:
Pavilion wrote:As stated earlier, my main goal is to store these files (with descriptions) in a mySQL table. Storing file name, size, type is NOT the same thing as storing content. How do I attach the file content to a variable so that I can INSERT into mySQL?
As a general rule, databases operate much more efficiently when you don''t store large objects such as files and images. The customary way to handle such storage is to store the images or other large files in folders on the server, and use the database to index them (that is, locate them by key words, etc.). That's certainly not a rule that applies to all cases, but it probably is better to start from there and then justify storing the entire contents if the situation requires it.

Relational databases were designed to hold DATA, in the sense that each column of a table should contain "atomic" values--that is, raw data that cannot be broken into smaller elements. Again, there are valid situations when you must violate that principle, but it takes a lot of experience and knowledge to make a good decision to do so.
Hello Califdon:

Thanks for responding to my query. I appreciate your concerns about storing files in tables. You should know that I've over 20 years of experience building classical databases. During those 20 years I've build a good sized client list ranging from small organizations with just a handful of users to very large organizations with 100s of users. You are right, "there are valid situations when you must violate that principle, but it takes a lot of experience and knowledge to make a good decision to do so".

I am entirely new to php, javascript, jQuery, HTML, etc.. If it's on-line - it's new to me. However, I've years (decades) of experience building classical relational database. This specific application is a learning exercise for me, but it is based upon an existing database that one of my clients is already using and has been using for several years. I specifically chose this learning exercise, so that i could learn how to work with files.

I've learned much already with this exercise. But, now I need to learn how to store actual file content in a mySQL table, hence my original question:

How do I attach the file content to a variable so that I can INSERT into mySQL? :D

Again, thank you for jumping in here. I look forward to learning even more about file processing with php.

Pavilion

Re: Reading the content of files

Posted: Sat Jun 16, 2012 10:53 pm
by xtiano77
At the link below you should find the information you are seeking. Cheers!
http://www.php-mysql-tutorial.com/wikis ... abase.aspx

Re: Reading the content of files

Posted: Sun Jun 17, 2012 7:16 am
by Pavilion
xtiano77 wrote:At the link below you should find the information you are seeking. Cheers!
http://www.php-mysql-tutorial.com/wikis ... abase.aspx
Hello xtiano77:

Thank you for jumping in here. As it happens, I've already found that tutorial. I found it yesterday, along with the following:
  1. http://mirificampress.com/permalink/sav ... into_mysql
  2. http://www.developershome.com/wap/wapUp ... ?page=php4
All these links have been helpful, and I've learned much. However, by the time users finish selecting files and submitting them, my php will receive a multidimensional $_FILES array. Following is sample output from my $_FILES array:
array(1) { ["uploads"]=> array(5) { ["name"]=> array(5) { [0]=> string(9) "test3.txt" [1]=> string(13) "Timestamp.doc" [2]=> string(14) "csv_1stRow.csv" [3]=> string(21) "CSV_OutlookExport.csv" [4]=> string(13) "CSVExport.csv" } ["type"]=> array(5) { [0]=> string(10) "text/plain" [1]=> string(18) "application/msword" [2]=> string(24) "application/octet-stream" [3]=> string(24) "application/octet-stream" [4]=> string(24) "application/octet-stream" } ["tmp_name"]=> array(5) { [0]=> string(14) "/tmp/phpSsyhGN" [1]=> string(14) "/tmp/phpVxRJlh" [2]=> string(14) "/tmp/phpiRrd1K" [3]=> string(14) "/tmp/php5rBHGe" [4]=> string(14) "/tmp/phpctbcmI" } ["error"]=> array(5) { [0]=> int(0) [1]=> int(0) [2]=> int(0) [3]=> int(0) [4]=> int(0) } ["size"]=> array(5) { [0]=> int(9527) [1]=> int(19968) [2]=> int(1354) [3]=> int(1356) [4]=> int(1354) } } }
This example holds data for 5 different files.

All the examples, tutorials I can find deal with one file at a time. That is why I came here. :wink:

Pavilion

Re: Reading the content of files

Posted: Sun Jun 17, 2012 5:08 pm
by xtiano77
Check out the link below:
http://www.phpeasystep.com/phptu/2.html