Search code examples
imagelaravellaravel-5.3filenamesfile-rename

How can I solve File not found at path:... when rename file? (laravel)


I see from here : rename a directory or a file

I try like this :

$destination_path = public_path() . DIRECTORY_SEPARATOR . 'img' . DIRECTORY_SEPARATOR . 'products'. DIRECTORY_SEPARATOR . $product->id . DIRECTORY_SEPARATOR . $product->photo;

$new_file_name = public_path() . DIRECTORY_SEPARATOR . 'img' . DIRECTORY_SEPARATOR . 'products'. DIRECTORY_SEPARATOR . $product->id . DIRECTORY_SEPARATOR . $new_file_name;

// dd($destination_path, $new_file_name);

Storage::move($destination_path, $new_file_name);

There exist error :

[League\Flysystem\FileNotFoundException] File not found at path: C:/xampp/htdocs/myshop/public/img/products/147/Zl756CDHovOZpEZz0jBYk73UCfO4zNmYDVWgLPpw.png

If I dd($destination_path, $new_file_name), the result :

C:\xampp\htdocs\myshop\public\img\products\147\Zl756CDHovOZpEZz0jBYk73UCfO4zNmYDVWgLPpw.png

C:\xampp\htdocs\myshop\public\img\products\147\147-hazard-chelsea.png

I try check Zl756CDHovOZpEZz0jBYk73UCfO4zNmYDVWgLPpw.png in folder 147, the file exist

Why there exist error?

Update :

I had find the solution. I use php script like this :

rename($destination_path, $new_file_name);. It works


Solution

  • Storage use C:/xampp/htdocs/myshop/storage/app
    but your data store on public path

    you can use \Storage::exists($path);

    $destination_path = public_path() . DIRECTORY_SEPARATOR . 'img' . DIRECTORY_SEPARATOR . 'products'. DIRECTORY_SEPARATOR . $product->id . DIRECTORY_SEPARATOR . $product->photo;
    
    $new_file_name = public_path() . DIRECTORY_SEPARATOR . 'img' . DIRECTORY_SEPARATOR . 'products'. DIRECTORY_SEPARATOR . $product->id . DIRECTORY_SEPARATOR . $new_file_name;
    
    // dd($destination_path, $new_file_name);
    if(\Storage::exists($destination_path)){
        Storage::move($destination_path, $new_file_name);
    }