skip to Main Content

I get the file:

  $fileExcel = Storage::path($finreport->filename);

How to creat a copy file and place in the same directory with prefix name _copy?

2

Answers


  1. First google answer: use File facade

    File::copy($fileExcel, $copyPath);
    

    for the file name you can explode, implode the path

    $array = explode('.', $fileExcel);
    $index = count($array) != 1 ? count($array) - 2 : 0;
    $array[$index] = $array[$index].'_copy';
    $copyPath = implode('.', $array);
    
    Login or Signup to reply.
  2. If you’re using Storage, to keep it consistent you might want to use

    Storage::copy('old/file.jpg', 'new/file.jpg');
    

    If you’re wanting to prefix you can do something like

    Storage::copy("old/$finreport->filename", "old/copy_$finreport->filename");
    

    Docs: https://laravel.com/docs/10.x/filesystem#copying-moving-files

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search