File uploading to server

Hi, I’m going to upload a image. I already doing same process but now i got the error warning Please let me know what is going wrong in my code

Here is div tag where front end code:


<?php echo $this->Form->control('file', ['type' => 'file',"class"=>"form-control" ]);?>

here is controller code :-
$gallery = $this->Galleries->newEntity($this->request->getData());
$uploaded_path = “/img/uploads/”;
$tmp_name = $this->request->data[‘file’][‘tmp_name’]; // this line got an error saying Undefined index: file

anyone can Please let me know.

https://book.cakephp.org/3.0/en/controllers/request-response.html#request-body-data

$this->request->getData('key');

i recommend the use of a plugin to upload:

1 Like

What is the output of

pr($this->request->getData('file'));
exit;

Undefined index suggests that the file array does not have tmp_name in it. Quite possibly, it is because the form element is not submitted correctly.

Put full view file also.

1 Like

If i print the following code then get all the data as per array format
$form_data = $this->request->getData();

but i getData though the following line get
$image_name = $this->request->data['file']['name'];
Accessing routing parameters through getData will removed in 4.0.0. Use getParam() instead.

Thanks ,i will check it .

Yes, show that full array.

Undefined index means there is no tmp_name, which tells me that file isn’t uploaded correctly.

Quite possibly, the form enctype is set differently.

Need more code to help you.

If you want to adhere to MVC style uploads then change request object through behaviours. For example, if there a column which stores user’s avatar file path on webroot and if you are doing something like

psuedo-code below:

move uploaded file > dest
$this->request->data[<field>] = <path of dest>
$modelEntityCollection->save($this->request->data)

Then, it is ugly. DO NOT FOLLOW the method above.

Instead, make a behaviour which does the file upload and changes before array is converted to objects. Use BeforeMarshal for Cake. [https://book.cakephp.org/3.0/en/orm/saving-data.html#modifying-request-data-before-building-entities]
Change the $data (2nd argrument) and then you will no longer have to disturb the controller.

Try to make use of models as much as you can and try to follow Fat-models-Slim-Controllers theory.

1 Like

I follow your instruction but i didn’t get idea. i will try it as soon as possible.

Let me create a Behavior which you can use. Infact, anyone can use.

There already is a widely-used behavior for this.

Thank you captain, I never knew!!! :wink:

Does that mean a plugin is supposed to establish some sort of a monopoly? :slight_smile:

What often seems to happen is that someone sees there’s software that does most of what they want, but instead of adding what it’s missing, they write their own, but leaving out the stuff they don’t need. And then someone else does the same, and someone else, and the projects all wither and die because there’s not enough people using any of them.

I took your feedback and added a webp conversion :slight_smile:
What do you think? Will look forward to your collab !