2

I want to use Dropzone JS in Slim Framework V2 on PHP. I can't make it to the controller where php should go ahead and move it to the specified folder.

This is my view in twig:

<div id="page-wrapper">
    <div class="container-fluid">
        <div class="row">
            <div class="col-lg-12">
                <h1 class="page-header">Upload your files <br> <small>{{ datum.title}}</small></h1>
            </div>
        </div>
        {#Dropzone part#}
        <div class="row">
            <div class="col-lg-12">
                <form action="{{ urlFor('uploading.dropzone.post') }}" class="dropzone" id="myDrop">
                    {#CSRF#}
                    <input type="hidden" name="id" id="id" value="{{datum.id}}">
                    <input type="hidden" name="{{ csrf_key }}" value="{{ csrf_token }}"/>
                    {#End of CSRF#}
                </form>
            </div>
        </div>
    </div>
</div>

Here is the Javascript part:

<script>
    $(document).ready(function(){
        var id = "{{ id }}";
        Dropzone.options.myDrop = {
            uploadMultiple: true,
            maxFileSize: 2,
            acceptedFiles: 'image/*',

            init: function init(){
                this.on('error', function(){
                    console.log('Error while uploading for data : '+id);
                });
            }
        }
    });
</script>

Here is my controller

//Handle uploaded files in PHP and move them to the specified folder
$app->get('files/upload',$authenticateForRole('auth'),function ()use($app){
    echo 'How did you get here?';
})->name('uploading.dropzone');

$app->post('files/upload/',$authenticateForRole('auth'),function()use($app){

    $request = $app->request;
    $id = $request->post('id');

    if($id && is_numeric($id)){
        //Definición inicial de variables
        $root = getcwd();/*http://stackoverflow.com/a/28470557/1883256 - XAMPP on Windows*/
        $directory = 'myFiles';
        $sub_destination='/uploaded/dossiers/'.$directory;
        $full_sub_dir=$root.$sub_destination;
        $max_length = 215;/*url length*/


        echo 'Posted! '.$id;

        $file=$_FILES['file'];
        //file properties:
        $file_name=$file['name'];
        $fileName_only=substr($file_name, 0, (strlen($file_name))-(strlen(strrchr($file_name, '.'))));/*http://stackoverflow.com/a/14204781/1883256*/
        $file_tmp=$file['tmp_name'];
        $file_size=$file['size'];
        $file_error=$file['error'];
        //working out the file extension:
        $file_ext=explode('.',$file_name);
        $file_ext=strtolower(end($file_ext));

        $location=$full_sub_dir . '/' . $file_name;

        move_uploaded_file($file_tmp, $location);

    }else{
        return $app->render('errors/400.twig');//bad request
    }

})->name('uploading.dropzone.post');

And this is the error I get.

Status Code:404 Not Found

I had solved it for similar situations here, but for Dropzone, I can't get it work :(

What am I missing? How do I fix it?

Pathros
  • 10,042
  • 20
  • 90
  • 156
  • You get this error in JavaScript console? Did you check to which URL dropzone tries to send the requests? – Nima Feb 15 '18 at 22:57
  • 1
    @Nima There is also XHR error. The URL is fine. I don't know why it says error 404. Maybe there's something wrong with the POST request. The inspection tools adds XHR error but I don't understand that. – Pathros Feb 16 '18 at 05:31

0 Answers0