Home > Blockchain >  move_uploaded_file is not fetching path in laravel
move_uploaded_file is not fetching path in laravel

Time:03-16

Here is my controller

    public function createAdmin()
     {
        
        $photo=$_FILES['bannerPic']['name'];
        $tempname=$_FILES['bannerPic']['tmp_name'];

        // echo $tempname." ".$photo;
        move_uploaded_file($tempname, 'picture/banner/'.$photo);

        $data=[
        'sliderPic' => $photo,
        ];

        dd($data);
        // \App\Models\Banner::create($data);
        // return view('Banner');
    }

here is my route

    Route::post('/BannerEdit', [App\Http\Controllers\BannerController::class, 
    'createAdmin']);

here is my blade form

    <form action="{{ url('') }}/BannerEdit" method="post"  
    enctype="multipart/form-data">
     @csrf

     <div >                                            
        <label >Banner Photo*</label>                                            
        <input type="file" name="bannerPic"  
         required>                                                                            
     </div>                  
     <div >
        <input type="submit"  value="Upload">
     </div>                                        
  </form>

When i submit the data it Gives me an Error as

move_uploaded_file(picture/banner/favicon.jpg): Failed to open stream: No such file or directory

But this Directory Exists

And i checked with full pathof localhost then it does not supports http path

CodePudding user response:

You need to use getcwd() function like this:

$dirpath = realpath(dirname(getcwd()));

So your controller code will be:

public function createAdmin()
     {
        
        $photo=$_FILES['bannerPic']['name'];
        $tempname=$_FILES['bannerPic']['tmp_name'];

        // echo $tempname." ".$photo;
        $dirpath = realpath(dirname(getcwd()));
        move_uploaded_file($tempname, $dirpath.'/'.$photo);

        $data=[
        'sliderPic' => $photo,
        ];

        dd($data);
        // \App\Models\Banner::create($data);
        // return view('Banner');
    }

let me know if it works.. if not then we'll modify $dirpath variable

PS: this solution will work on server. are you working on server or in localhost?

EDIT:

Other solution is to use below function to get proper directory structure:

$dirpath = public_path('picture/banner/');
  • Related