Not looping through $_files array and would this file upload method not slow down my website?

93 Views Asked by At

I was wondering why the for loop won't cycle through the $_files in this php code:

<html>
    <form action="" method="post">
        Your name: <input type="text" name="name"/><br>
        Select your files: <input type="file" name="upload[]" multiple /><br>
        <input  type="submit" name="submit" value="Send">
    </form>
</html>

<?php
require_once "dropbox-sdk/Dropbox/autoload.php";
use \Dropbox as dbx;

$dbxClient = new dbx\Client("************", "PHP-Example/1.0");

if(isset($_POST["name"])){
    $dbxClient->createFolder("/" . $_POST["name"]);
    echo "folder created";

    //Loop through each file
    for($i=0; $i<count($_FILES['upload']['name']); $i++) {
        echo "I now looped " . $i . " times<br>";
        //Get the temp file path
        $tmpFilePath = $_FILES['upload']['tmp_name'][$i];

        //check if file exists
        if ($tmpFilePath != ""){
            echo "Loop " . $i . " - file exists<br>";
            //Setup our new file path
            $newFilePath = $_POST["name"] . "/" . $_FILES['upload']['name'][$i];

            //Upload the file to dropbox
            $f = fopen($tmpFilePath, "rb");
            $result = $dbxClient->uploadFile($tmpFilePath, dbx\WriteMode::add(), $f);
            fclose($f);
            print_r($result);
        }
    }
}
?>

In this code I use dropbox to upload files always to my account. But will this dropbox method actually help to not slow down my website since it still uses some kind of tmp folder. Is this tmp folder on my website or on the clients pc?

3

There are 3 best solutions below

1
On

Try checking count($_FILES) instead of count($_FILES['upload']['name']).

3
On

I think it won't help to not slow down your website. Also tmp folder is the folder on your own server

1
On

Change your form definition to:

     <form action="DOSOMETHING.PHP" method="post" enctype="multipart/form-data>

You need an action to process it and the enctype to recognize the files. I put the PHP in a separate file, DOSOMETHING.PHP. Other than that your form and PHP loop seem to work fine.