4

I want save uploaded file with original name.What i must add to this code?

Below's my code

public function store(Request $request)
{
    if($request->hasFile('image'))
    {
        $file = $request->file('image');
        $originalname = $file->getClientOriginalName();
        $filename =$originalname;
        $file->move('public/', $filename);
    }
6
  • laravel.com/docs/5.5/requests#storing-uploaded-files Commented Jan 2, 2019 at 8:06
  • how to use it? it give me error everytime i try it Commented Jan 2, 2019 at 8:23
  • Checked the folder permissions? What error would that be? Can you please edit your question and paste the error there? Commented Jan 2, 2019 at 8:26
  • "The file "C:\xampp\tmp\phpD2B2.tmp" does not exist" when i add $path = $request->image->store($originalname); Commented Jan 2, 2019 at 8:28
  • $path = $request->photo->store('images'); ... "The store method accepts the path where the file should be stored relative to the filesystem's configured root directory." Is $originalname a path? Sounds more like a complete filename. "If you do not want a file name to be automatically generated, you may use the storeAs method, which accepts the path, file name, and disk name as its arguments" Commented Jan 2, 2019 at 8:32

3 Answers 3

7

You may use the storeAs method, which receives the path, the file name, and the (optional) disk as its arguments:

public function store(Request $request)
{
    if($request->hasFile('image'))
    {
        $file = $request->file('image');
        $originalname = $file->getClientOriginalName();
        $path = $file->storeAs('public/', $originalname);
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Error "Call to a member function hashName() on string"
1

Upload files with original fileName.

if ($request->hasFile('image')) {
    $file_path = $request->file('image')->storeAs('dir_name', request()->file('image')->getClientOriginalName(), 'optional_disk_name');
}

// pass the disk name as the third argument to the storeAs method: ex. local,s3,etc

Comments

0

Try this:

if($request->hasFile('image'))
{
    $file = $request->file('image');
    $originalname = $file->getClientOriginalName();
    $img = Image::make($file->getRealPath());
    $img->stream();
    Storage::disk('local')->put('images/'.$originalname, $img, 'public');
}

3 Comments

Error "Class 'App\Http\Controllers\Image' not found"
Please add use Image; on top of your controller
In your app.php Add this in your aliases: "'Image' => Intervention\Image\Facades\Image::class," and in your providers "Intervention\Image\ImageServiceProvider::class," Don't forget to do php artisan config:cache after this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.