Upload multiple text files into MySQL - php

I have ZERO experience coding uploading files through browser, so this part is all very new to me.
I need to give users (in fact they will be only one or two authorized users) a way to upload multiple text files (think 50-200 files) directly into a MYSQL database.
I don't want to give them FTP access, but am OK allowing them to enter files into the database.
I can figure out how to get the data from a PHP array into the MYSQL database.
What I can't figure out is how to get the contents of multiple files into the PHP array(s).
Please help out with the code.

This example should help you understand the basic idea
<?php
$fileContents = Array();
if(count($_FILES) != 0){
foreach($_FILES as $file){
$fp = fopen($file["tmp_name"], "r");
array_push($fileContents, fread($fp, $file["size"]));
fclose($fp);
}
//$fileContents now holds all of the text of every file uploaded
}
?>
<html>
<head>
</head>
<body>
<form action="test.php" method="post" enctype="multipart/form-data">
<input type="file" name="file1" id="file" />
<input type="file" name="file2" id="file" />
<input type="submit" name="submit" value="Submit" />
</form>
</body>
</html>
It first checks that there has been files posted to itself.
If there is files, it loops through each and opens them while they are in their temporary file state.
After that it reads all of the contents at once (be careful with this) using the size attribute that sent with it.
At the same time, it is pushing the contents into the array called $fileContents.
So $fileContents[0] holds the first text file and so on.

Just add more <input type="file">s to your page, and they will all appear in the $_FILES array, which you can loop to retrieve them.
However:
The structure of the $_FILES array is slightly illogical when it comes to multiple files - make sure you read the manual carefully, it is a little counter intuitive.
Make sure that your upload_max_filesize, post_max_size, max_file_uploads and max_input_time PHP.ini directives are generous enough.
See also: Handling multiple file uploads in PHP.

<!-- FORM -->
<form method="post" enctype="multipart/form-data">
<?php
for($i=1;$i<=10;$i++) //change 10 to any number for more upload fields
{
echo '<input type="file" name="files[]" /><br />';
}
?>
<input type="submit" name="submit" value="Submit" />
</form>
<?php
//Processor
if(isset($_POST['submit']))
{
foreach($_FILES['files']['tmp_name'] as $tmp_name)
{
if(!empty($tmp_name))
{
$filecontent[] = file_get_contents($tmp_name);
}
}
//Test
echo '<pre>';
print_r($filecontent);
echo '</pre>';
}
?>

Thank you everyone who has contributed to this. I am having a very hard time choosing the answer, because I think it's a 50/50 effort by John and DaveRandom.
In case someone wants to see the end product here it is:
HTML:
<html>
<head>
</head>
<body>
<form method="post" action="test.php" enctype="multipart/form-data">
<input name="filesToUpload[]" id="filesToUpload" type="file" multiple="" />
<input type="submit" name="submit" value="Submit" />
</form>
</body>
</html>
PHP:
<?php
function rearrange( $arr ){
foreach( $arr as $key => $all ){
foreach( $all as $i => $val ){
$new[$i][$key] = $val;
}
}
return $new;
}
$fileContents = Array();
if(count($_FILES['filesToUpload'])) {
$realfiles=rearrange($_FILES['filesToUpload']);
foreach ($realfiles as $file) {
$fp = fopen($file["tmp_name"], "r");
array_push($fileContents, fread($fp, $file["size"]));
fclose($fp);
}
foreach ($fileContents as $thisone) {
echo "<textarea wrap='off'>\n";
echo $thisone;
echo "</textarea>\n";
echo "<br>----<br>";
}
}
?>

Related

HTML not posting file to PHP

Here is my code:
<html>
<body>
<div>
<form action="<?=$_SERVER['PHP_SELF'];?>" method="post">
<input type="file" id="myFile" name="filename">
<input type="submit" name="Submit">
</form>
</div>
<?php
if(isset($_POST['Submit']))
{
$file = $_POST["myFile"];
echo("hello");
}
?>
</body>
</html>
when I run it and input a file it says: Undefined array key "myFile"
what am I doing wrong?
Uploaded files don't get added to the $_POST array but they are loaded into the $_FILES array. Try check that out.
You can then use the move_uploaded_file() to save it wherever you want.
Important: as soon as the script execution is over, the temporary uploaded file is removed if it was not saved somewhere else with move_uploaded_file().
The request transfers the data using the names of the form fields and not the IDs, the file data can be found in the global variable $_FILES.
Use $file = $_FILES['filename']['name']; for the name of the uploaded file
or $file = $_FILES['filename']['tmp_name']; for the temporary file path of the uploaded file.
Add on form enctype="multipart/form-data"
This value is necessary if the user will upload a file through the form.
Your file type name is "filename".
<html>
<body>
<div>
<form action="<?=$_SERVER['PHP_SELF'];?>" method="post" enctype="multipart/form-data">
<input type="file" id="myFile" name="filename">
<input type="submit" name="Submit">
</form>
</div>
<?php
if(isset($_POST['Submit']))
{
$file = $_FILES['filename']['tmp_name'];
echo("hello");
}
?>
</body>
</html>

Trouble accessing HTML form data using PHP

I'm trying to build a file upload form and I'm having trouble with the very basics. My form is this:
<html>
<body>
<form action="fileuploader.php" method="POST" enctype="multipart/form-data">
<input type="file" name="filename" />
<input type="submit"/>
</form>
</body>
</html>
My php code so far is one line and it doesn't do anything:
<?php
echo $_POST['filename'];
?>
The idea (at this point) is just to display the name of the file entered in the form. What am I doing wrong?
Based on your code I modified it. Have a try it.
HTML Part
<html>
<body>
<form action="fileuploader.php" method="POST" enctype="multipart/form-data">
<input type="file" name="filename" />
<input type="submit" name="submit" />
</form>
</body>
</html>
PHP
if (isset($_POST['submit'])) {
// Check if files array is not empty
if (!empty($_FILES)) {
$imageName = $_FILES['filename']['name'];
echo $imageName;
// Insert your code related to upload
}
}
You can print the filename using the following code:
<?php
echo $_FILES["filename"]["name"];
?>

how to count number of uploaded files in php

How can i count the number of uploaded files?
This is my form:
<div id="dragAndDropFiles" class="uploadArea">
<h1>Drop Images Here</h1>
</div>
<form id="sfmFiler" class="sfmform" method="post" enctype="multipart/form-data">
<input type="file" name="file" id="file" multiple />
<input type="submit" name="submitHandler" id="submitHandler" class="buttonUpload" value="Upload">
</form>
and this is the piece of php which uploads the files:
if($_SERVER['REQUEST_METHOD'] == "POST") {
$tmpFilePath = $_FILES['file']['tmp_name'];
$newFilePath = $dir.'/' . $_FILES['file']['name'];
if(move_uploaded_file($tmpFilePath, $newFilePath)) {
echo "xxx files are successfully uploaded";
}
}
In this code you are getting only one file thats why you are getting count result 1. if change your input file name like "file[]"
<input type="file" name="file[]" id="file" multiple />
and then use the below line code you will get your desire result. Cause its needs an array filed to hold the input data.
<?php echo count($_FILES['file']['name']); ?>
Thanks, i tried in my system get the result.
AFriend is correct. The above answers always return 1.
Try:
echo count(array_filter($_FILES['file']['name']))
Worked for me anyway.
_t
Using the array_filter function it works
try
$countfiles = count(array_filter($_FILES['file']['name']));
It returns 0 in case of NULL, 1 in case of 1 file uploaded, etc.
Check this answer
<?php echo count($_FILES['file']['name']); ?>
php multiple file uploads get the exact count of files a user uploaded and not the count of all input fields in the array
You could use the count function:
$no_files = count($_FILES);
If no files are selected and your file count is 1, you can use this line before moving the file:
if (!empty($_FILES['file']['tmp_name'][0])) {
for($i=0;$i<$countfiles;$i++){

How can I allow a user to upload multiple text files, then save them to a string and display them (in PHP)?

I want to allow a user to upload multiple text files using a simple HTML form, and then save them to a string. After that, I want to display them to the user. For example, say I have three text files with the following contents:
file1.txt: this is some text.
file2.txt: this is some more text.
file3.txt: even more text!
When the user uploads them using an HTML form, I want to save them to a string and display them like so:
this is some text.
this is some more text.
even more text!
I have the following (incomplete) code that attempts to get only one file:
Upload text documents: <br /><br>
<form action="output.php" method="post" enctype="multipart/form-data">
<input type="file" name="file" size="50" />
<br />
<input type="submit" value="Upload File" />
</form>
<?php
$doc = $_POST['file'];
echo $doc;
?>
How can I loop through the files and save them to a string, then display them in the best way possible?
$filenames = array();
if(isset($_FILES["documents"]))
{
for($i=0; $_FILES['documents']['name'][$i] != '' ; $i++)
{
$filenames[$i] = $_FILES['documents']['name'][$i];
$myfile = fopen("$_FILES['documents']['name'][$i]", "r") or die("Unable to open file!");
echo fread($myfile,filesize("$_FILES['documents']['name'][$i]"));
fclose($myfile);
}
}
I think this is helps for you!!!!!!!1
In completion of White Marcus answer
use the multiple="multiple"attribute in the input file element.
You should have : $_FILES variable, instead of $_POST
<?php
var_dump($_FILES);
?>
To print a file use file_put_contents()
Best regards
$filenames = array();
if(isset($_FILES["documents"]))
{
for($i=0; $_FILES['documents']['name'][$i] != '' ; $i++)
{
$filenames[$i] = $_FILES['documents']['name'][$i];
}
print_r($filename);
}
use the multiple="multiple"attribute in the input file element.

multiple file upload (array) php

PHP script
$count = 0;
foreach ($_FILES['filesToUpload'] as $file) {
//upload process
echo $file[$count]['tmp_name'].',';
$count ++;
}
HTML
<form method="POST" action="action-here" enctype="multipart/form-data">
<input class="btn" name="filesToUpload[]" type="file" multiple="" />
<input class="btn primary" type="submit" value="Submit">
</form>
I'm doing this majorly wrong. What i'm trying to do is make it so you select the files then the php script processes it like an array?
I keep getting out puts such as 1,i,C,,,.
I know other ways to do multiple uploads, but I know this is one of the simplest.
foreach ($_FILES['filesToUpload']['error'] as $k => $error) {
echo $_FILES['filesToUpload']['tmp_name'][$k].',';
}
Tips: debug it with print_r($_FILES).
you should write it this way:
echo $file['tmp_name'][$count].',';

Categories