如何在 Laravel 5 控制器中复制文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37293049/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 13:49:51  来源:igfitidea点击:

How to copy files in laravel 5 controller

phplaravellaravel-5laravel-5.2file-copying

提问by paranoid

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

我想将文件复制到 Laravel 中的文件夹。当我复制文件时向我显示此错误

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

copy() 函数的第二个参数不能是目录

My code:

我的代码:

$success = \File::copy(base_path('test.text'),base_path('public/'));

回答by Ben Rhys-Lewis

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.

错误说明了它的含义。如果您阅读关于copy()函数的文档,它会指出您必须从文件复制到新文件。例如,将内容从 .txt 文件复制到另一个 .txt 文件。

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

所以只需在公共部分后添加 test.text ,它就会创建一个新文件或覆盖现有文件。

$success = \File::copy(base_path('test.text'),base_path('public/test.text'));

回答by Kalpesh Rajai

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

您可以使用该public_path()功能获取public/文件夹的路径,您可以像这样复制该文件

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

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

现在question你在哪里source file?如果它已经在public folder比你得到该文件的完整路径,像这样。

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

Happy Coding!!!

编码快乐!!!

EDIT

编辑

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

public_path()laravel,让全内置功能path的的public文件夹中。有关更多信息,请阅读此处:https: //laravel.com/docs/5.2/helpers#method-public-path