Aristona
Aristona

Reputation: 9341

Laravel3: How to check if there is a file uploaded

I have a page with a form where user can upload an image to replace his existing avatar.

However, if the user submits forms without uploading an image, I will not update his avatar and user can keep his old avatar.

This is an UPDATE issue so I need something like this in pseudo code:

if (Input::has_uploaded_file() === true)
    //User uploaded something, update avatar column/remove old avatar etc.
else
    //User didn't upload anything so don't update avatar column

I just need the first line, though.

Can anybody help? I couldn't find much about this in documentation.

Upvotes: 0

Views: 195

Answers (2)

Phill Sparks
Phill Sparks

Reputation: 20889

If Input::has_file('name') does not work for you then you can use the equivalent of what it is doing, like this...

if (Input::file('name.tmp_name', '') != '')

Taken from laravel/input.php

Upvotes: 2

user1908313
user1908313

Reputation:

I read laravel source code, i think its this:

Input::has_file('input_name');

If that's not work, try using native php:

$_FILES['input_name']['error'] = UPLOAD_ERR_NO_FILE;

Or same above with laravel:

$i = Input::file('input_name');

$i['error'] = UPLOAD_ERR_NO_FILE;

Code above is to check if user leave the upload form empty.

Upvotes: 0

Related Questions