LARAVEL 如何使用 Laravel 将文件从本地(公共)传输到 FTP

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45093582/
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 16:17:51  来源:igfitidea点击:

LARAVEL How to transfer file from local (public) to FTP using Laravel

laravel

提问by yulianto saparudin

I wanna ask a question, anyone know how to transfer data from local / public to ftp, I'm using Storage class https://laravel.com/docs/5.4/filesystem

我想问一个问题,任何人都知道如何将数据从本地/公共传输到 ftp,我正在使用存储类 https://laravel.com/docs/5.4/filesystem

Here's my code

这是我的代码

$file_local = Storage::disk('local')->get('public/uploads/ftp/file.pdf');
$file_ftp = Storage::disk('ftp')->put('/file.pdf', $file_local');

But my code is not working, when I open the file on ftp, the file is broken, then I open it with notepad, inside file, the content is 'public/uploads/ftp/file.pdf' the content I mean content should on local not what I was wrote,

但是我的代码不起作用,当我在 ftp 上打开文件时,文件坏了,然后我用记事本打开它,里面的文件,内容是“public/uploads/ftp/file.pdf”我的意思是内容应该在本地不是我写的,

Anyone know how to transfer file from local to ftp, sorry for my bad English, Thanks for your answer anyway

任何人都知道如何将文件从本地传输到 ftp,抱歉我的英语不好,无论如何感谢您的回答

回答by Sodruldeen Mustapha

To get started, you should know the details of your FTP host, FTP username, and FTP password. Once, you are ready with the details open the .env files and add the details as below:

首先,您应该知道您的 FTP 主机、FTP 用户名和 FTP 密码的详细信息。一旦,您准备好详细信息,打开 .env 文件并添加如下详细信息:

    FTP_HOST=YOUR_FTP_HOST_VALUE
    FTP_USERNAME=YOUR_FTP_USERNAME_VALUE
    FTP_PASSWORD=YOUR_FTP_PASSWORD_VALUE

Make sure to replace the placeholders with the actual values. Next, open the config/filesystems.php file and add the ‘ftp' configuration to the ‘disks' array.

确保用实际值替换占位符。接下来,打开 config/filesystems.php 文件并将 'ftp' 配置添加到 'disks' 数组。

<?php
return [
    ......
    'disks' => [
        ......
        'ftp' => [
            'driver' => 'ftp',
            'host' => env('FTP_HOST'),
            'username' => env('FTP_USERNAME'),
            'password' => env('FTP_PASSWORD'),
            'root' => 'DIR_PATH_TO_WHERE_IMAGE_STORE' // for example: /var/www/html/dev/images
        ],
    ],

];

Replace DIR_PATH_TO_WHERE_IMAGE_STORE with the actual path where you need to store images. For example, if we have folder called ‘images' and path to this folder is /var/www/html/dev/images then this path will go as the value for ‘root' in the above array.

将 DIR_PATH_TO_WHERE_IMAGE_STORE 替换为您需要存储图像的实际路径。例如,如果我们有一个名为“images”的文件夹,并且该文件夹的路径是 /var/www/html/dev/images,那么该路径将作为上述数组中“root”的值。

For uploading files through FTP we need a form where a user can submit the image. Add the below code in your view file.

为了通过 FTP 上传文件,我们需要一个用户可以提交图像的表单。在您的视图文件中添加以下代码。

<form action="{{ url('PASS_ACTION_URL_HERE') }}" method="post" enctype="multipart/form-data">
    <div class="form-group">
        <label for="exampleInputFile">File input</label>
        <input type="file" name="profile_image" id="exampleInputFile" multiple />
    </div>
    {{ csrf_field() }}
    <button type="submit" class="btn btn-default">Submit</button>
</form>

You should replace the PASS_ACTION_URL_HERE with your actual route. As we are using a Laravel Storage to file uploading user need add Facade to the controller file as follows:

您应该将 PASS_ACTION_URL_HERE 替换为您的实际路线。由于我们使用 Laravel Storage 来上传文件,因此用户需要将 Facade 添加到控制器文件中,如下所示:

public function store(Request $request)
{
    if($request->hasFile('profile_image')) {

        //get filename with extension
        $filenamewithextension = $request->file('profile_image')->getClientOriginalName();

        //get filename without extension
        $filename = pathinfo($filenamewithextension, PATHINFO_FILENAME);

        //get file extension
        $extension = $request->file('profile_image')->getClientOriginalExtension();

        //filename to store
        $filenametostore = $filename.'_'.uniqid().'.'.$extension;

        //Upload File to external server
        Storage::disk('ftp')->put($filenametostore, fopen($request->file('profile_image'), 'r+'));

        //Store $filenametostore in the database
    }

    return redirect('images')->with('status', "Image uploaded successfully.");
}

‘profile_image' is the name of our file input. We build the unique name of our file and then uploading it to an external server. Notice we have used Storage::disk(‘ftp') method. This function would store our file to the path as defined in the configuration. A user should store the value ‘$filenametostore' in your database.

'profile_image' 是我们的文件输入的名称。我们构建文件的唯一名称,然后将其上传到外部服务器。注意我们使用了 Storage::disk('ftp') 方法。此函数会将我们的文件存储到配置中定义的路径。用户应该将值 '$filenametostore' 存储在您的数据库中。

回答by Michael Law

'local' => [
    'driver' => 'local',
    'root'   => storage_path().'/app',
],

If you use the local disk (config/filesystems.php) you are looking in 'storage/app' not the public folder unless you have changed the default settings. So:

如果您使用本地磁盘 (config/filesystems.php),除非您更改了默认设置,否则您将查看“storage/app”而不是公共文件夹。所以:

$file_local = Storage::disk('local')->get('file.pdf');

will look for 'Storage/App/file.pdf'

将寻找“Storage/App/file.pdf”

Make sure your pdf is in that folder and give this a try:

确保您的 pdf 在该文件夹中并尝试一下:

$file_local = Storage::disk('local')->get('file.pdf');
$file_ftp = Storage::disk('ftp')->put('file.pdf', $file_local);

EDIT: also, I notice in your original post second line of code you have rogue ' after $file_local ;)

编辑:另外,我注意到在你原来的第二行代码中,你在 $file_local 之后有流氓 ;)