Image dose not move at folder path

2019-06-13 18:51发布

问题:

I already given 777 permission to my images folder. Charts Table is also saving all record of image. I am going to attach table:charts structure here.

public function store(Request $request)
{
        $input = $request->all();
        $tradeID= Auth::user()->trade()->create($input);

    if($file = $request->file('file'))
    {
        $name = time() . $file->getClientOriginalName();
        $file->move('images', $name);
        $photo = Chart::create(['file'=>$name]);
        $input['photo_id'] = $photo->id;
    }
        $tradeID->chart()->create($input);
}

回答1:

Try to change destination path from relative to absolute

public function store(Request $request)
{
    $input = $request->all();
    $tradeID= Auth::user()->trade()->create($input);

    if($file = $request->file('file'))
    {
        $name = time() . $file->getClientOriginalName();
        $file->move( public_path() . '/images/', $name);  // absolute destination path
        $photo = Chart::create(['file'=>$name]);
        $input['photo_id'] = $photo->id;
    }
        $tradeID->chart()->create($input);
}


回答2:

try this

if($file = $request->file('file'))
{
    $name = time() . $file->getClientOriginalName();
    $path = $file->storeAs('images', $name,'public');
    $photo = Chart::create(['file'=>$name]);
    $input['photo_id'] = $photo->id;
}

you can retreive the file with /storage/public/file_name or /file_name depending on where you told laravel to store the public files



回答3:

You are not actually storing the file, you are actually moving a non-existent file.

What you might consider You, most probably want to access uploaded file from public path. If so, you should store this file to particular folder in your storage directory and create a symlink to that directory in your public path. Here's an example:

Storage::disks('public')->put($filename, $uploadedFile); // filename = 'images/imageFile.jpg'

This creates a file in your storage/app/public/images directory.

Then, you can create a symlink, laravel provide a simple command to do so.

php artisan storage:link

This creates a symlink to your public disk.

Then, you can access the image at http://your-site-server/storage/images/imageFile.jpg

Hope it helps.



回答4:

 $name = time().'.'. $file->getClientOriginalName(); //depends on ur requirement
        $file->move(public_path('images'), $name);


回答5:

Are you trying to move uploaded file or existing file ?

To move file you need to do:

File::move($oldPath, $newPath);

the old path and new path should include full path with filename (and extension)

To store an uploaded file you need to do:

Storage::disk('diskname')->put($newPath, File::get($file));

the new path is full path with filename (and extension).

  • disk is optional , you can store file directly with Storage::put()..
  • Disks are located in config/filesystems.php

The File::get($file) , $file is your input from post request $file = $request->file('file') , basically it gets contents of uploaded file.

UPD#1

Okay , you can do this:

$request->file('file')->store(public_path('images'));

Or with Storage class:

    // get uploaded file
    $file = $request->file('file');
    // returns the original file name.
    $original = $file->getClientOriginalName();
    // get filename with extension like demo.php
    $filename = pathinfo($original)['basename'];
    // get public path to images folder
    $path = public_path('images');
    // concat public path with filename
    $filePath = $path.'/'.$filename;
    // store uploaded file to path
    $store = Storage::put($filePath, File::get($file));