johnnw's avatar

Image appears as a folder instead a file in the uploadas folder

When a new conference is created is stored and uploaded an image with the code below. But then in the frontend the image doesn't appears. And in the uploads folder instead of appears a jpg file it appears a folder with .jpg extension.

Do you know where is the issue?

if($request->image){
    $featured = $request->image;
    $featured_new_name = 'uploads/conferences/'.time().$featured->getClientOriginalName();
    $featured->move($featured_new_name);
}
else{
    $featured_new_name = '';
}

 $conference = Conference::create([
            'image' =>  $featured_new_name,
        ]);
0 likes
3 replies
skliche's avatar
skliche
Best Answer
Level 42

Because the first parameter of move() is the destination folder, not a full path name:

public function move($directory, $name = null)

Edit: Source is in /vendor/symfony/http-foundation/File/UploadedFile.php

1 like
johnnw's avatar

Thanks, but so do you know how to store in database just when a image is selected? Because the image is not a required field.

Like below is working but it is storing in the database always this "uploads/conferences/" when no image is selected. But if no image is selected in the database the image column should be empty "".

if($request->image){
    $featured = $request->image;
    $featured_new_name = time().$featured->getClientOriginalName();
    $featured->move('uploads/conferences',$featured_new_name);
}
else{
    $featured_new_name = '';
}
$conference = Conference::create([
    'image' =>  'uploads/conferences/'.$featured_new_name,
]);
skliche's avatar

There are lots of ways to do that. Example:

} else {
    $featured_new_name = null;
}

...

$conference = Conference::create([
    'image' => $featured_new_name ? "uploads/conferences/{$featured_new_name}" : null,
    ...
]);

If the column should not be null just use an empty string instead of null in the create array.

1 like

Please or to participate in this conversation.