php 在 Laravel 5 文件夹结构中保存文件的位置?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32005110/
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
Where to save files in Laravel 5 folder structure?
提问by koalaok
I have my Laravel project that will save files (txt or csv) after making some computations.
我的 Laravel 项目将在进行一些计算后保存文件(txt 或 csv)。
I'm looking after a best practice on where to save these files. Maybe /resources/csv/...?
我正在寻找关于在哪里保存这些文件的最佳实践。也许 /resources/csv/...?
Second question how would it be the best way to reference this path from within classes? Setting up the abs path in .env file? Is there a laravel method that will return the path to resources?
第二个问题,从类中引用这条路径的最佳方式是什么?在 .env 文件中设置 abs 路径?是否有一个 Laravel 方法可以返回资源的路径?
回答by jedrzej.kurylo
/resourcesare not the best place, as this folder is used for source files and is usually stored in source code repository (e.g. git).
/resources不是最好的地方,因为这个文件夹用于存放源文件,通常存储在源代码存储库(例如 git)中。
Files that application generates usually end up somewhere in /storage folder - just create a /storage/csvfolder there.
应用程序生成的文件通常最终位于 /storage 文件夹中的某个位置 - 只需在那里创建一个/storage/csv文件夹。
You should never reference those files directly from your classes. Laravel's filesystems are what you need - you can read more about them here: http://laravel.com/docs/master/filesystem. They make operations on the files (like read, write, prepend, append, delete, move, get all filesand many more...) much simpler.
你永远不应该直接从你的类中引用这些文件。Laravel 的文件系统正是你所需要的——你可以在这里阅读更多关于它们的信息:http: //laravel.com/docs/master/filesystem。它们使对文件的操作(如读取、写入、前置、追加、删除、移动、获取所有文件等等)变得更加简单。
Start with defining a filesystems in your config/filesystems.php
从在config/filesystems.php 中定义文件系统开始
'disks' => [
'csv' => [
'driver' => 'local',
'root' => storage_path().'/csv',
],
],
Now you can read/write your csv files via Storagefacade from anywhere in your code like that:
现在,您可以从代码中的任何位置通过Storagefacade读/写您的 csv 文件,如下所示:
Storage::disk('csv')->put('file.csv', $content);
$content = Storage::disk('csv')->get('file.csv');
回答by Hasan Tareque
You can save files in storage folder.
您可以将文件保存在存储文件夹中。
For example:
例如:
You can create a folder named csv in storage folder and get the path as follows:
可以在storage文件夹中创建一个名为csv的文件夹,获取路径如下:
storage_path().'/csv';
You can find the storage folder in
您可以在其中找到存储文件夹
Laravel 4.2 : app>storage
Laravel 5+ : in root directory
Laravel 4.2 : app>storage
Laravel 5+ : 在根目录中
回答by Rubén Ruíz
public function storePhotos($data, Request $request, $requirement)
{
//setlocale(LC_ALL, 'en_US.UTF-8'); //for spanish names
$fileUploaded = $request->file('someVarFormName');
$folder = 'docs/';
$destinationFolder = date("Y-m");
$destinationFolderInServer = $folder.$destinationFolder;//.'/';
//$rules = array('file' => 'required|mimes:png,gif,jpeg,txt,pdf,doc,docx,exel,exelx'); //'required|mimes:png,gif,jpeg,txt,pdf,doc'
$validator = Validator::make(array('file' => $fileUploaded), $rules);
if($validator->passes())
{
$originalFileName = $fileUploaded->getClientOriginalName();
$fileName = pathinfo($originalFileName, PATHINFO_FILENAME);
$fileExtension = strtolower(pathinfo($originalFileName, PATHINFO_EXTENSION));
$linkFilenameTemp = strtolower(ToolText::createLink($fileName));
$linkFilename = $linkFilenameTemp.'.'.$fileExtension;
//get versioin number if filename exist
$i = 1;
while(file_exists($destinationFolderInServer.'/'.$linkFilename))
{
$linkFilename = $linkFilenameTemp.'('.$i.').'.$fileExtension;
$i++;
}
//* quitar / si falla
$upload_success = $fileUploaded->move($destinationFolderInServer, $linkFilename);
$dbRegOfFile = NULL;
if($requirement->have_file)
$dbRegOfFile = new mdl_TramitesRequisitosFiles();
else
$dbRegOfFile = mdl_TramitesRequisitosFiles::where('requisito_id', $requirement->id)
->get();
$dbRegOfFile->requisito_id = $data['requirementId'];
$dbRegOfFile->publisher = $data['publisher'];
$dbRegOfFile->publisher_entity = $data['publisher_entity'];
$dbRegOfFile->nice_name = $fileName;
$dbRegOfFile->link_file_name = $linkFilename;
$dbRegOfFile->extension = $fileName;
$dbRegOfFile->size = $fileUploaded->getClientSize();
$dbRegOfFile->save();
}