How to copy files in laravel 5 controller
Asked Answered
P

2

6

I want copy file to folder in laravel. when I copy file show me this error

The second argument to copy() function cannot be a directory

My code:

$success = \File::copy(base_path('test.text'),base_path('public/'));
Pistil answered 18/5, 2016 at 7:35 Comment(0)
S
17

The error says what it means. If you read the docs on the copy() function it states you must copy from a file to a new file. Eg copying content from a .txt file to another.txt file.

So just add test.text after the public part and it will create a new file or overwrite an existing one.

$success = \File::copy(base_path('test.text'),base_path('public/test.text'));
Seafood answered 18/5, 2016 at 7:41 Comment(0)
C
8

You can use the public_path() function to get the path to the public/ folder you can copy that file like this

$destinationPath=public_path()."/UploadFolderName/FileName.ext";
$success = \File::copy(base_path('test.text'),$destinationPath);

now the question is where is your source file? if it is already in the public folder than you get the full path of that file like this.

$sourceFilePath=public_path()."/SomeFolderName/FileName";
$destinationPath=public_path()."//UploadFolderName/FileName.ext";
$success = \File::copy($sourceFilePath,$destinationPath);

Happy Coding!!!

EDIT

public_path() is laravel inbuilt function that give the full path of the public folder. for more information read here: https://laravel.com/docs/5.2/helpers#method-public-path

Counterproof answered 18/5, 2016 at 7:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.