Take a look at this: http://clivern.com/how-to-create-file-upload-with-laravel/
The basic idea is to use the `{{ Form::file('image') }}`` and then store the path to the image in the database ;)
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
is there any link or idea how can i make each user to upload picture as their primary picture on their account?
Take a look at this: http://clivern.com/how-to-create-file-upload-with-laravel/
The basic idea is to use the `{{ Form::file('image') }}`` and then store the path to the image in the database ;)
@blackbird will that work on the latest laravel 5? i have seen the tags "Tags: laravel laravel 4.1 laravel upload"
It's a bit different but it will work. You will need to include this package and the rest will work the same: http://laravelcollective.com/docs/5.0/html
If you have any questions let us know!
@blackbird as per the documentation instruction the file is saved in the storage folder, is it also safe if i place the pictures inside the public folder?
how can i call/display the picture to the view page?
The public folder is accessable for everyone, so if the images are confidental then I would place it in the storage folder. But of course you can place it where you want it to be!
Let's you register a user and you let them upload a profile picture. The picture is then saved in the storage folder. The next thing you do is save the user to the database, then you would have a picture field in the database which contains the location of that picture. In your view you just use the something like this $user->profile and that contains the path to the image. You only have to place that in an img tag ;)
First give it a try, if you are stuck at a point let us know ;)
@dinis @blackbird It's more of security issue than public/private. Read this thread, @Jeffberry made some points there that you should consider.
@blackbird i was able to move and insert data into the database but the filename of the picture that is being saved in the database is the temporary name with path, what i need to now here?
You need the path of the uploaded image, not the path of the temporary name! When it's in the database you can do a query on the database with either using the DB class or by model
Start at the very basic, I don't have the feeling that you understand Laravel enough to dive into uploading and stuff. Check out this tutorial and you will understand what to do: https://laracasts.com/series/laravel-5-fundamentals
@blackbird here is my controller code
public function uploadTest(Request $request)
Photo::create($request->all());
return Input::file('photoFilename')->move(__DIR__.'/../../../storage/hphotos/xpf1',Input::file('photoFilename')->getClientOriginalName());
here is my view code
{!! Form::open(array('url'=>'form-submit','files'=>true)) !!}
{!! Form::label('photoFilename','File',array('id'=>'','class'=>'')) !!}
{!! Form::file('photoFilename','',array('id'=>'','class'=>'')) !!}
{!! Form::close() !!}
i am getting is the temporary name, but the return is correct filename.
another problem is when i upload with a same filename with the uploaded file it overwrite the old one
You don't save the image now to the database, try something like this:
public function uploadTest(Request $request)
{
$photo = Photo::create($request->all());
// Check if there is an image
if (Input::file('photoFilename')
{
// Assuming the photo model has a field profileimage
// We then get the file and upload it
// as you can see we change the name of the file and set a unique_id there, so the image is unique
// In your view you can use the $photo->profileimage to get the correct source
$photo->profileimage = Input::file('photoFilename')->move(__DIR__.'/../../../storage/hphotos/xpf1', uniqid()
$photo->save();
}
// Redirect to another page
}
@blackbird
i was able to save the correct name of the file but im having problem with the unique_id() im getting this message
Call to undefined method Symfony\Component\HttpFoundation\File\UploadedFile::unique_id()
public function uploadTest(Request $request)
$photo = Photo::create($request->all());
// Check if there is an image
if (Input::file('photoFilename'))
// Assuming the photo model has a field profileimage
// We then get the file and upload it
// as you can see we change the name of the file and set a unique_id there, so the image is unique
// In your view you can use the $photo->profileimage to get the correct source
$photo->profileimage = Input::file('photoFilename')->move(__DIR__.'/../../../storage/hphotos/xpf1', Input::file('photoFilename')->unique_id() );
$photo->save();
// Redirect to another page
I updated my code.
The reason I added the uniqid() function is to gerate a random name for your file! You don't need the Input::file('photoFilename') at all. My example will do the trick ;)
In below code I added an image to Group. Please note that this code also uses Intervension facade(Image) which is part of intervension package: so add following to composer.json
require "intervention/image": "~2.1",and use following in your class
use Intervention\Image\Facades\Image;
$imageInput = $request->file('image');
if ($imageInput) {
$imageName = date('Y-m-d-h-i-s') . $imageInput->getClientOriginalName();
$destinationPath = storage_path("app" . DIRECTORY_SEPARATOR . "img" . DIRECTORY_SEPARATOR . "groups" . DIRECTORY_SEPARATOR . $imageName);
$image = Image::make(file_get_contents($imageInput->getPathname()));
$image->resize(600, null, function ($constraint) {
$constraint->aspectRatio();
$constraint->upsize();
});
$image->save($destinationPath);
$input['image'] = $destinationPath;
}
$group = Group::create($input);
If you do not want to do anything extra with image then neglect the code which is using Intervesion and simply move the uploaded image to the destination path.
I am storing these images to storage/app/img/groups folder. You may change that to your preferred location.
@blackbird it works! but display it on the view page has an error message
Undefined variable: photo (View: E:\XAMPP\htdocs\resources\views\layouts\leftWing.blade.php)
You need to pass the value to your view with a controller for example.
Like I said before you don't have the knowledge to do all of this! Follow the tutorials first: https://laracasts.com/series/laravel-5-fundamentals
@blackbird i already passed the view to my controller the problem is that when i call the path from the database is that it cant be viewed. here is an example of what is save to the database E:\XAMPP\htdocs\app\Http\Controllers/../../../storage/hphotos/xpf1\54e3308723cc9 . jpg
when i call it nothing shows in my view page because that path cant be viewed
foreach ($photos as $photo)
echo "<li><img src=".$photo->profileimage."><li>";
i already watched the series laravel 5 fundamentals what confused me is when it comes to manipulating the data
The path to your file is not correct.
You can do something like this:
$photo->profileimage = Input::file('photoFilename')->move(storage_path() . '/hphotos/xpf1', uniqid(
Note: Watch out for extra slashes ;)
@blackbird yup but still cant view the image, this is what my view page generated for the src of image saved from the database
E:\XAMPP\htdocs/storage/hphotos/xpf1\54e333a7928ca . jpg
i just put space on the file extension only here
Check if the file is on the same location with the same name and extension!
BTW: I was thinking, the link should look like this: application.dev/storage/hphotos/xpf1/54e333a7928ca.jpg Note: Your / is to the wrong way near your image name
@blackbird yes i already checked the location and pictures are there
i dont know why the slash is \ before the filename and it does not get the project.dev which i set in my xampp
@blackbird is it possible if i can save only to the database the file name only of the image? like this 54e333a7928ca.jpg
Ok you can do it like this:
if (Input::file('photoFilename')
{
$file = Input::file('photoFilename');
$photoname = uniqid();
$file->move(storage_path() . /hphotos/xpf1, uniqid());
$photo->profileimage = '/hphotos/xpf1/' . $photoname . $file->guessClientExtension();
$photo->save();
}
@blackbird thanks for being patient with me ;) i still dont know why i cant display a file from storage folder but i can display now images but from public folder
It's oke. Just try some stuff out. If you find any thing else let me know! You got it working now with public, I bet you get it working with storage as well ;)
@blackbird haha thanks alot! im done with fundamentals that's why im trying to explore things around im so interested learning this laravel but the tutorials posted are bit outdated due to new file system of laravel. im trying to make a dating website now as my training ground trying to put up things on it while learning laravel more deeply.
The fundamentals of that tutorial should still work in Laravel 5, only the directories are different ;)
@blackbird as i check out the documentation i see this below, but the permissions were not discussed, maybe that is the reason why i can't display images from storage. do you have any idea about this?
Permissions Laravel may require some permissions to be configured: folders within storage require write access by the web server.
That's only on the webserver, you develop on your local environment so you don't have that issue!
@blackbird i already edited the database for my photos with the user's id who uploaded the picture with unique name and successfully save with the id of the user.
the question in my mind now is that how can i display the correct image uploaded by the user beside the users name in my view page?
here is a part of my viewpage where suppose the image of the user will be displayed besides the username
foreach($users as $userlist) img src="" a href="{{ action('ProfileController@show', [$userlist->id]) }}" h2 {{ $userlist->name }} /h2 /a endforeach()
Let say the path to the image of the user is in your $user object
Then you can do something like this:
@foreach ($users as $userlist)
<h2><a href="{{ action('ProfileController@show', [$userlist->id]) }}">{{ $userlist->name }}</a></h2>
<a href="{{ action('ProfileController@show', [$userlist->id]) }}">
<img src="{{ $userlist->image }}" alt="">
</a>
@endforeach
Please or to participate in this conversation.