File uploading to server


#1

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.


#2

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:


#3

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.


#4

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.


#5

Thanks ,i will check it .


#6

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.


#7

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


#8

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


#9

There already is a widely-used behavior for this.


#10

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

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


#11

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.


#12

#13

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