Laravelファイルの格納、削除、移動などの操作
1構成
ファイルシステムのプロファイルはconfig/filesyetems.phpでは、コメントがはっきり書かれています.また、disks配列に新しいdiskを作成することができます.
一般的に最もよく使われるのはlocal(ローカル)ストレージです.特に、rootを変更することでrootパスを変更できます.
2ハードディスク(HDD)インスタンスの取得
ファイル管理を行うには、ハードディスク(HDD)インスタンスに至る必要があります.Storageフェースのdiskメソッドで取得し、目的の操作を行うことができます.
public function index()
3ファイル操作
3.1ファイルの取得
get()メソッドを使用して、ファイルが文字列としてファイル名に転送されるように取得できますが、サブディレクトリ以下のファイルを取得する場合は、$disk->get('subpath/.../.../file.txt')などの転送パスが必要です.
3.2ファイルが存在するか否かを判断する
3.3ファイル情報の取得
ファイルサイズ:
public function index()
最終変更日:
public function index()
3.4ファイルを保存する
3.5 Copyファイル
3.6ファイルの移動
3.7ファイルの先頭/末尾に内容を追加する
3.8ファイルの削除
3.8ファイルのダウンロード
}
4ディレクトリ操作
4.1ディレクトリから取り出したファイル
4.2サブディレクトリへのアクセス
4.3ディレクトリの作成
4.4ディレクトリの削除
ファイルシステムのプロファイルはconfig/filesyetems.phpでは、コメントがはっきり書かれています.また、disks配列に新しいdiskを作成することができます.
return [
/*
|--------------------------------------------------------------------------
| Default Filesystem Disk
|--------------------------------------------------------------------------
|
| Here you may specify the default filesystem disk that should be used
| by the framework. A "local" driver, as well as a variety of cloud
| based drivers are available for your choosing. Just store away!
|
| Supported: "local", "ftp", "s3", "rackspace"
|
*/
'default' => 'local',
/*
|--------------------------------------------------------------------------
| Default Cloud Filesystem Disk
|--------------------------------------------------------------------------
|
| Many applications store files both locally and in the cloud. For this
| reason, you may specify a default "cloud" driver here. This driver
| will be bound as the Cloud disk implementation in the container.
|
*/
'cloud' => 's3',
/*
|--------------------------------------------------------------------------
| Filesystem Disks
|--------------------------------------------------------------------------
|
| Here you may configure as many filesystem "disks" as you wish, and you
| may even configure multiple disks of the same driver. Defaults have
| been setup for each driver as an example of the required options.
|
*/
'disks' => [
'local' => [
'driver' => 'local',
'root' => storage_path('app'),
],
'ftp' => [
'driver' => 'ftp',
'host' => 'ftp.example.com',
'username' => 'your-username',
'password' => 'your-password',
// Optional FTP Settings...
// 'port' => 21,
// 'root' => '',
// 'passive' => true,
// 'ssl' => true,
// 'timeout' => 30,
],
's3' => [
'driver' => 's3',
'key' => 'your-key',
'secret' => 'your-secret',
'region' => 'your-region',
'bucket' => 'your-bucket',
],
'rackspace' => [
'driver' => 'rackspace',
'username' => 'your-username',
'key' => 'your-key',
'container' => 'your-container',
'endpoint' => 'https://identity.api.rackspacecloud.com/v2.0/',
'region' => 'IAD',
'url_type' => 'publicURL',
],
],
];
一般的に最もよく使われるのはlocal(ローカル)ストレージです.特に、rootを変更することでrootパスを変更できます.
'local' => [
'driver' => 'local',
// 'root' => storage_path('app'), /storage/app/
'root' => public_path('uploads'), // public/uploads/
],
2ハードディスク(HDD)インスタンスの取得
ファイル管理を行うには、ハードディスク(HDD)インスタンスに至る必要があります.Storageフェースのdiskメソッドで取得し、目的の操作を行うことができます.
public function index()
{
$disk = Storage::disk('local');
//
$disk->put('file1.txt', 'Laravel Storage');
}
3ファイル操作
3.1ファイルの取得
public function index()
{
//
$disk = Storage::disk('local');
//
$file = $disk->get('test.txt');
dd($file);
}
get()メソッドを使用して、ファイルが文字列としてファイル名に転送されるように取得できますが、サブディレクトリ以下のファイルを取得する場合は、$disk->get('subpath/.../.../file.txt')などの転送パスが必要です.
3.2ファイルが存在するか否かを判断する
public function index()
{
//
$disk = Storage::disk('local');
//
$exists = $disk->exists('image.png');
dd($exists); // false
}
3.3ファイル情報の取得
ファイルサイズ:
public function index()
{
//
$disk = Storage::disk('local');
//
$size = Storage::size('/home/test.txt');
dd($size); // 4
}
最終変更日:
public function index()
{
//
$disk = Storage::disk('local');
//
$time = Storage::lastModified('file1.txt'); // 1507701271
$time = Carbon::createFromTimestamp($time);
echo $time; // 2017-10-11 05:54:31
}
3.4ファイルを保存する
public function upload(Request $request){
if ($request->isMethod('POST')){
$file = $request->file('source');
//
if ($file->isValid()){
//
$originalName = $file->getClientOriginalName();
//
$ext = $file->getClientOriginalExtension();
//MimeType
$type = $file->getClientMimeType();
//
$realPath = $file->getRealPath();
$filename = uniqid().'.'.$ext;
$bool = Storage::disk('uploads')->put($originalName,file_get_contents($realPath));
//
if($bool){
echo 'success';
}else{
echo 'fail';
}
}
}
return view('upload');
}
3.5 Copyファイル
public function index()
{
//
$disk = Storage::disk('local');
// ,
$disk->copy('home/test.txt','rename.txt');
}
3.6ファイルの移動
public function index()
{
//
$disk = Storage::disk('local');
// ,
$disk->move('file2.txt', 'home/file.txt');
}
3.7ファイルの先頭/末尾に内容を追加する
public function index()
{
//
$disk = Storage::disk('local');
//
$disk->prepend('file1.txt', 'test prepend');
//
$disk->append('file1.txt', 'test append');
}
3.8ファイルの削除
public function index()
{
//
$disk = Storage::disk('local');
//
$disk->delete('test.txt');
//
$disk->delete(['test22.txt', 'icon.jpg']);
}
3.8ファイルのダウンロード
public function download(){
$data = './uploads/20190104/5c2f6248614a7.avi';
return response()->download($data);
}
4ディレクトリ操作
4.1ディレクトリから取り出したファイル
public function index()
{
//
$disk = Storage::disk('local');
$directory = '/';
//
$files = $disk->files($directory);
// ( )
$allFiles = $disk->allFiles($directory);
dd($files, $allFiles);
}
4.2サブディレクトリへのアクセス
public function index()
{
//
$disk = Storage::disk('local');
$directory = '/';
//
$directories = $disk->directories($directory);
// ( )
$allDirectories = $disk->allDirectories($directory);
dd($directories, $allDirectories);
}
4.3ディレクトリの作成
public function index()
{
//
$disk = Storage::disk('local');
//
$disk->makeDirectory('test');
$disk->makeDirectory('test1/test2');
}
4.4ディレクトリの削除
public function index()
{
//
$disk = Storage::disk('local');
//
$disk->deleteDirectory('test');
$disk->deleteDirectory('test1/test2');
}