pandiyan's avatar

laravel file upload

Hi when i upload a image, temp file only uploaded in the directory ,that image wasn't upload at that same time image file name also has changed.

0 likes
12 replies
pandiyan's avatar

@nakov

    if($request->hasfile('custom_attribute_sizechart_image'))
    {
        $custom_attribute_sizechart_image=$request->file('custom_attribute_sizechart_image');
        $filename = $custom_attribute_sizechart_image->getClientOriginalName();
        @mkdir(public_path("/images/.$custom_attribute_category_id."));
        $custom_attribute_sizechart_image->move(public_path("/images/.$custom_attribute_category_id", $filename));
    }
    else
    {
        dd('failed to upload');
    }

and the file in directory was php6801.tmp

pandiyan's avatar

@nakov Thanks its working fine.but the file name is not correct. it generate random number

Nakov's avatar

@pandiyan you can try this:

$filename = $custom_attribute_sizechart_image->getClientOriginalName();

dd($filename);

and it will tell you which filename is being uploaded. Original name is used here.

pandiyan's avatar

@nakov your right, but i am using this code

            $custom_attribute_sizechart_image=$request->file('custom_attribute_sizechart_image');
        $filename = $custom_attribute_sizechart_image->getClientOriginalName();
        $path="/images/$custom_attribute_category_id";
        if(!Storage::disk('public')->put($path, $custom_attribute_sizechart_image)) {

when i use $filename upload not working

Nakov's avatar

@pandiyan then make your path this:

$filename = $custom_attribute_sizechart_image->getClientOriginalName();
$path="/images/$custom_attribute_category_id/" . $filename;

if(!Storage::disk('public')->put($path, $custom_attribute_sizechart_image)) {
..
pandiyan's avatar

@nakov this code for create new folder as filename,but file name not changed

Nakov's avatar

@pandiyan try one of this:

$path="/images/$custom_attribute_category_id";
Storage::disk('public')->put($path, file_get_contents($custom_attribute_sizechart_image));

or

$path="/images/$custom_attribute_category_id";
Storage::disk('public')->putFile($path, $custom_attribute_sizechart_image);
pandiyan's avatar

@nakov 1st option not working and 2nd option working same like previous.

Nakov's avatar
Nakov
Best Answer
Level 73

@pandiyan okay friend, this is what I use and it works just fine:

$file = $request->file('custom_attribute_sizechart_image');
$directory="/images/$custom_attribute_category_id";
$filename = $file->getClientOriginalName() . '.' . $file->getClientOriginalExtension();
$file->move(public_path() . $directory, $filename);
pandiyan's avatar

thank u very much @nakov .finaly this code work.

        $custom_attribute_sizechart_image=$request->file('custom_attribute_sizechart_image');
        $path="/images/$custom_attribute_category_id";
        $filename = $custom_attribute_sizechart_image->getClientOriginalName();
        // .'.'.$custom_attribute_sizechart_image->getClientOriginalExtension();
        $custom_attribute_sizechart_image->move(public_path() . $path, $filename);

Please or to participate in this conversation.