david001's avatar

Data not saved and image not uploaded

My data(title and images name) are not saved in database and images not uploaded to public directory.I got data saved message but my data is not saved in database . i have a form with two fields title input field and browse image field .i have add more button ,from which javascript generates again two more fields each time user click that button.

public function postUpload(Request $request)
{
 //dd($request->all());
 foreach($request->get('textbox') as $textbox) {

        //file upload code
        if ($request->hasFile('file')) {

                $destinationPath = public_path() . '/uploads/photos'; // upload path
                $extension = $request->file('file')->getClientOriginalExtension(); // getting image extension
                $fileName = rand(11111,99999).'.'.$extension; // renamaing image
                $request->file('file')->move($destinationPath, $fileName); // uploading file to given path


                $data = new Item;
                $data->title = $textbox['name'];
                $data->file= $fileName;
                $data->save();
        }else{

        }            

    }

    return "data saved";

}   

form

<form action="{{url('upload')}}" method="post" enctype="multipart/form-data">
<input type="hidden" name="_token" value="{{csrf_token()}}">

    <div id='TextBoxesGroup'>
        <div id="TextBoxDiv1">
            <label>Textbox #1 : </label>
            <input type='textbox' id='textbox1' name="textbox[1][name]">
            <input type='file' id='desc1' name="textbox[1][file]">
        </div>
    </div>
    
    <input type='button' value='Add Button' id='addButton'>
    <input type='button' value='Remove Button' id='removeButton'>
    <input type="submit" value="submit">
    
</form>

model

class Item extends Model
{
    
    protected $table = 'items';


    public $fillable = ['title','file'];

}

ondd($request->all())i got

array:2 [▼
  "_token" => "e1JhMKXZmJGJ2GYxrDlZU26JcyMOnUoyyDMNnRsz"
  "textbox" => array:2 [▼
    1 => array:2 [▼
      "name" => "first image"
      "file" => UploadedFile {#27 ▼
        -test: false
        -originalName: "Screenshot (1).png"
        -mimeType: "image/png"
        -size: 164089
        -error: 0
      }
    ]
    2 => array:2 [▼
      "name" => "second image"
      "file" => UploadedFile {#28 ▼
        -test: false
        -originalName: "Screenshot (2).png"
        -mimeType: "image/png"
        -size: 164334
        -error: 0
      }
    ]
  ]
]
0 likes
8 replies
drilon's avatar

Because:

if ($request->hasFile('file')) { // <-- this return false
Khudadad's avatar

User Validation and see the errors maybe your image is too large.

Snapey's avatar

tell me this what site this will be so that I can make sure I don't visit it! I strongly suggest that you check what 'image' types you allow to be uploaded because badstuff.php uploaded could be quite risky.

change

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

to

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

I suggest that you move your 'data saved' message into the same code block as the actual save since whatever happens its going to say the same thing.

Validation... recommended.

filenames - you have chosen a method that does not check for duplications

When you save data, don't you have to worry about what or who it belongs to? How will you retrieve it later?

david001's avatar

i got error Call to a member function getClientOriginalExtension() on null on removing if ($request->hasFile('file'))

Snapey's avatar

You can't use $request->has('file') because you have multiple files. One per text box. So, don't remove that line, change it to use $textbox

1 like
david001's avatar

@Snapey i got error Call to a member function hasFile() on array on using if ($textbox->hasFile('file')) {how can i solve this.

Snapey's avatar

got to dash now but I think you need instead, something like

if($textbox['file'] instanceof UploadedFile) {

Please or to participate in this conversation.