简体   繁体   English

如何在Laravel 5.1中通过Storage-ftp上传大文件

[英]How to upload big files via Storage-ftp in Laravel 5.1

I'm trying to upload big file to ftp server with Laravel Storage function and it's keep give me the erorr 我正在尝试使用Laravel Storage功能将大文件上传到ftp服务器,并且一直给我erorr

Out of memory (allocated 473432064) (tried to allocate 467402752 bytes) 内存不足(已分配473432064)(试图分配467402752字节)

I've tried to change the memory limit on php.ini and it still won't work, when I upload file to the server normally its work and the size dosent metter. 我试图更改php.ini的内存限制,但是当我将文件正常上载到服务器时,它的工作和大小指示表仍然无法正常工作。

I've tried anything but nothing work. 我已经尝试过任何方法,但是没有任何效果。 Again - I'm trying to upload via FTP. 再次-我尝试通过FTP上传。

One more question : there is a way to uplaod the file direct to the ftp server from the client? 还有一个问题:是否有一种方法可以将文件从客户端直接上传到ftp服务器? I see that the storage upload first to my server and then transfer to the second server... 我看到存储首先上传到我的服务器,然后又转移到第二台服务器...

It definately sounds like a PHP Limitation, raising the memory limit probably isn't the best way to do it though, that leads to nothing but hassle, trust me. 听起来绝对像是一个PHP限制,提高内存限制可能不是实现此目的的最佳方法,这只会带来麻烦,请相信我。

Best method i can think of from the top of my head is to use Envoy (the server script method not the deployment service) to put together an SSH task, that way your job is being executed outside of PHP so you're not subject to the same memory limitations. 我能想到的最好的方法是使用Envoy(服务器脚本方法而不是部署服务)来组合SSH任务,这样您的工作就在PHP 之外执行,因此您不必相同的内存限制。 Your Envoy script ( envoy.blade.php in your project root) would probably look something like this; 您的Envoy脚本(项目根目录中的envoy.blade.php )可能看起来像这样。

@servers(['your_server_name' => 'your.server.ip'])

@task('upload', ['on' => ['your_server_name']])
    // perform your FTP setup, login etc.
    put your_big_file.extension
@endtask

I've only got one of these set up for a deployment job which is called from Jenkins so i'm not sure if you can launch it from within Laravel, but i launch from the command line like this; 我只为詹金斯(Jenkins)所谓的部署工作设置了其中之一,所以我不确定是否可以从Laravel内部启动它,但是我是从这样的命令行启动的;

vendor/bin/envoy run myJobName

Like i said the only thing i can't quite remember is if you can run Envoy from within Laravel itself, and the docs seem a little hazy on it, Definately an option worth checking out though :) 就像我说的那样,我唯一不记得的是是否可以从Laravel本身运行Envoy,并且文档对此似乎有些朦胧,尽管这是一个值得一试的选项:)

https://laravel.com/docs/5.1/envoy https://laravel.com/docs/5.1/envoy

Finally I solve the problem by using curl instead of Storage. 最后,我通过使用curl而不是Storage解决了这个问题。

$ch = curl_init();
$localfile = $file->getRealPath();
$fp = fopen($localfile, 'r');
curl_setopt($ch, CURLOPT_URL, 'ftp://domain/' . $fileName);
curl_setopt($ch, CURLOPT_USERPWD, "user:pass");
curl_setopt($ch, CURLOPT_UPLOAD, 1);
curl_setopt($ch, CURLOPT_INFILE, $fp);
curl_setopt($ch, CURLOPT_INFILESIZE, filesize($localfile));
curl_exec($ch);
$error_no = curl_errno($ch);
curl_close($ch);

Work perfect! 做工完美! Better then Storage option of laravel. 最好是laravel的Storage选项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM