mbo's avatar
Level 3

How to view variables?

good day,

I'm using dropzone js to upload my images. This package automatically uploads my images to a temp folder. This is done by an function in a controller. (route brings request to controller)

I want to get the values of the variables in the controller. But when i use

dd($xx);

it won't show anything,

Is there a way to breakout the function in the controller when it used to show the variable?

thanks in advance.

0 likes
9 replies
Nakov's avatar

Why isn't this enough:

public function fileupload(Request $request){

     if($request->hasFile('file')) {

       $file = $request->file('file');

       $extension = $file->getClientOriginalExtension();
       $filename = $file->getClientOriginalName();
       dd($extension, $filename); // print the file 
      
     }
}
mbo's avatar
Level 3

Nakov,

thanks for your reply. The function is not loaded by a request. The page does not refresh. So i think it i done by javascript. How sends it by the router.

https://www.dropzonejs.com/bootstrap.html

Route::post('dropzone/store', 'UploadController@store');
Route::put('dropzone/store', 'UploadController@store');

uploadcontroller:


 /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store()
    {
      //session_start();
      $inp = Input::all();
      $destinationPath = 'storage/xxx/uploads/temp'; // upload path
      $extension = $inp["file"][0]->getClientOriginalExtension(); // getting image extension
      $fileName = Auth::id().time().$inp['file'][0]->getClientOriginalName(); // renameing image
      $inp["file"][0]->move($destinationPath, $fileName); // uploading file to given path

      return $fileName;
    }

Someone else has installed this for me in the past. Secondly i'm new in programming and don't have knowledge from javascript etc.

Any other suggestion?

Nakov's avatar

@mbo you will not be able to get it from the input, try this instead:

public function store(Request $request)
{
      //session_start();

    if($request->has('file'))
    {
        $file = $request->file('file');
        $destinationPath = 'storage/xxx/uploads/temp'; // upload path
        $extension = $file->getClientOriginalExtension(); // getting image extension
        $fileName = Auth::id().time().$file->getClientOriginalName(); // renameing image
        $file->move($destinationPath, $fileName); // uploading file to given path

        return $fileName;
    }	


      return 'No file';
}

Here is a tutorial for more: https://makitweb.com/drag-and-drop-file-upload-using-dropzone-in-laravel/#controller

mbo's avatar
Level 3

@nakov,

i tried. But got the following error;

dropzone.min.js:1 POST http://ligplaats.test/dropzone/store 500 (Internal Server Error)
c.submitRequest @ dropzone.min.js:1
c.uploadFiles @ dropzone.min.js:1
c.processFiles @ dropzone.min.js:1
c.processQueue @ dropzone.min.js:1
(anonymous) @ dropzone.min.js:1
setTimeout (async)
c.enqueueFile @ dropzone.min.js:1
(anonymous) @ dropzone.min.js:1
accept @ dropzone.min.js:1
c.accept @ dropzone.min.js:1
c.addFile @ dropzone.min.js:1
(anonymous) @ dropzone.min.js:1

As soon as i introduce the request $request it goes down.

The script i posted is working but i want to have insights wat is happening. With the script i have to issues:

  1. if i do a multi load of the images they all get the same name (he overrides them). So the file name is not set unique. I think it has to do with the [0]. Does it?

  2. i like to upload the images to DO spaces. So i have to adjust the $destinationpath. I can't get it working because i don't know how it looks like (can't see the variables)

Any idea how to solve those things?

Nakov's avatar

@mbo if what you have works, then you just need to loop over the input data and add each one separately:

$inp = Input::all();

foreach($inp['file'] as $file)
{
    $destinationPath = 'storage/xxx/uploads/temp'; // upload path
    $extension = $file->getClientOriginalExtension(); // getting image extension
    $fileName = Auth::id().time().$file->getClientOriginalName(); // renameing image
    $file>move($destinationPath, $fileName); // uploading file to given path
}

mbo's avatar
Level 3

nakov,

thanks for your reaction. At the end i was able to solve it. I seems that the package also runs a foreach. So that was not needed. I also had to do with the multi upload settings of the package. That was causing the problems.

Thanks for the help anyway!

public function store(Request $request){

          
            $filename = sha1(time()).$request->file('file')->getClientOriginalName();
            $request->file('file')->storeAs('xxx/uploads/temp/',$filename, 'do-spaces');

            return Response::json($filename);
            
    }

settings: uploadmultiple was true is to false.

//images zone
Dropzone.options.myAwesomeDropzone = { // The camelized version of the ID of the form element
            url: '/dropzone/store',

            uploadMultiple: false, 
            parallelUploads: 5,


mbo's avatar
Level 3

thanks, that can be very helpful indeed!

Please or to participate in this conversation.