Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

move_uploaded_file is not fetching path in laravel

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

     <div class="mb-3 col-12">                                            
        <label class="col-4 text-label form-label">Banner Photo*</label>                                            
        <input type="file" name="bannerPic" class="form-control input-rounded col-4 mb-3" 
         required>                                                                            
     </div>                  
     <div class="mb-3 text-end">
        <input type="submit" class="btn btn-primary" 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

>Solution :

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/');
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading