0

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.

2
  • what do you have so far? Commented Dec 17, 2011 at 21:27
  • I have nothing. Like I said, I have zero experience coding the uploading part. In other words, I need the code for the interface to upload multiple files, and the part of the php file that would generate the array with the text. I'm good from there Commented Dec 17, 2011 at 21:35

4 Answers 4

3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

This is perfect in terms of file to array. THANK you
No prob, if you have any questions just post em here
3

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:

See also: Handling multiple file uploads in PHP.

6 Comments

the link is super helpful. is there a way to do shift-clicks in the file selector, so that you could simply select several files from within a single link?
That would be down to the browser, not your coding - in theory, HTTP provides for this possibility, but I don't see it implemented much (in fact, I don't think I've ever seen it) - you might want to look into some kind of AJAX-style uploader like uploadify.
@UnoMeinAme I have just been playing with it and I can't get IE8 or IE9 to let me select more than one file no matter what I do, and if IE doesn't support it that basically means you can't do it - even if other browsers support it, by not supporting IE you've lost a large portion of your audience
I guess I could make the two users use whatever browser I'd tell them to use. So, I don't care about compatibility or scalability at all. As long as it can be done with one browser (preferably not IE), I'd be happy. See, it would STILL be a pain to click 200 buttons to upload 200 files...
@UnoMeinAme Unfortunately, that is definitely one place where the WWW currently does not make life easy - HTML5 fixes this, but is not widely implemented, if you can force your users to use a browser where it will work, you might get something useful from this. Most websites that implement this sort of thing currently use things like Flash and Java, so that might also be a route you could look at - Google would be the best place to start with this (but only if you already know some of the language you choose)
|
1
<!-- 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>';
}
?>

Comments

1

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>";

    }
}
?> 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.