1

I'm having a problem with dropzone and the upload.php file. I'm trying to save the files that are uploaded in a directory on my computer. I'm working with MAMP, so the directory is inside the htdocs. The code for my form is:

<form action="./upload.php" class="dropzone" id="upload-form" method="post" enctype="multipart/form-data">
    <div class="fallback">
        <input id="upload" name="upload[]" type="file" multiple/>
    </div>

    <input type="submit" name="uploaden[]" id="uploaden" multiple/>
</form>

My dropzone initialisation:

Dropzone.autoDiscover = false;

$myDropzone = new Dropzone('#upload-form', {
    url: "/upload.php",     
    uploadMultiple: true,
    addRemoveLinks: true,
    maxFiles: 25,
    
   accept: function(file, done) {
        console.log("uploaded");
        done();
    },
    
    init: function() {
        this.on("addedfile", function() {
          if (this.files[25]!=null){
            this.removeFile(this.files[0]);
          }
        });

        var submitButton = document.querySelector("#uploaden")
        myDropzone = this; // closure

        submitButton.addEventListener("click", function() {
        myDropzone.processQueue();
        });

    }
});

and my NEW upload.php file:

    <?php

ini_set('display_errors', 'On');
error_reporting(E_ALL);

$ds          = DIRECTORY_SEPARATOR;  //1
 
$storeFolder = '/uploads';   //2

if (!empty($_FILES)) {

if(isset($_FILES['files']['tmp_name']) && is_array($_FILES['files']['tmp_name'])){
     
    foreach($_FILES["files"]["tmp_name"] as $key=>$tmp_name) {

            $tempFile = $_FILES['file']['tmp_name'];          //3 

            $targetPath = dirname( __FILE__ ) . $ds. $storeFolder . $ds;  //4

            if(!file_exists($targetPath)) {
                
                $targetFile =  $targetPath. $_FILES['file']['name'];  //5

                move_uploaded_file($tempFile,$targetFile); //6
            }

            else {
                $targetFile =  $targetPath. $_FILES['file']['name'];  //5

                move_uploaded_file($tempFile,$targetFile); //6
            }
    }
}
}

?> 

I really don't know what I'm doing wrong. At first I got the following errors, but now those aren't even showing anymore..

[18-May-2015 12:12:31 Europe/Amsterdam] PHP Notice: Array to string conversion in /Users/anoukkolkman/Desktop/School/Afstuderen/Website/htdocs/upload.php on line 17

[18-May-2015 12:12:31 Europe/Amsterdam] PHP Warning: move_uploaded_file() expects parameter 1 to be string, array given in /Users/anoukkolkman/Desktop/School/Afstuderen/Website/htdocs/upload.php on line 19

Those lines are:

$targetFile =  $targetPath. $_FILES['file']['name'];  //5
 
move_uploaded_file($tempFile,$targetFile); //6

Can somebody help me what I'm doing wrong and why the files aren't places in the directory?

2 Answers 2

4

at first glance , I noticed that you are doing

<input id="upload" name="upload[]" type="file" multiple/>

I am not sure why you set the name attribute to upload[] instead of upload.

Update 22 May 2015

Ok, I tried to debug for you and I found the reason.

Because you set uploadMultiple to true in Dropzone. So it sends multiple files, hence $_FILES['file']['name'] is an array.

Solution

There are two options:

  1. Update your upload.php file, to loop through the $_FILES['file']['name'] array to upload.

  2. Simply set uploadMultiple to false. It should work.

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

3 Comments

I don't now it either. At some point I just got lost in the code and tried everything. I've deleted it, but it still doesn't work.
I've changed it and now it uploads a picture, but not all the pictures. It's a start, but still not what I want to achieve..
well, it is a start from here. we have found the issue for you. you need to pick up from here.
0

If you are not seeing your uploaded files inside of the directory that you are trying to upload them in I would consider making sure that your permissions for that folder are the appropriate type. Since you are developing locally you can probably just set it to be writeable by all.

bash:

sudo chmod -R a+w /path/to/your/uploads_folder

cmd:

icacls "D:\path\to\your\uploads_folder" /grant Anouk:(OI)(CI)F /T

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.