首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用"Jquery文件上传插件“将文件直接上传到FTP服务器

问使用"Jquery文件上传插件“将文件直接上传到FTP服务器
EN

Stack Overflow用户
提问于 2012-10-15 03:46:33
回答 3查看 7.1K关注 0票数 1

我在我的网站上使用"Blueimp Jquery file upload plugin“,我允许用户在我的服务器上上传文件。我在HostGator上托管了我的网站。问题是用户无法上传超过64MB/文件的文件,搜索后我发现HostGator在"php.ini“文件中将上传文件的最大限制设置为64MB/文件,而我无法更改此大小限制。

但实际上,我可以上传任何大小的文件没有任何限制,直接通过FTP。

所以,我想知道有没有办法避免这个HostGator限制,或者有没有办法允许用户通过FTP直接上传文件到服务器,把这个jquery插件扔给"web应用程序“。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2012-10-15 03:55:58

你当然可以改变的最大文件大小设置,就像我以前做过的几次一样。

在您选择的目录中创建一个.htaccess文件。在此文件中包含php覆盖设置。

代码语言:javascript
复制
php_value upload_max_filesize 200M
php_value post_max_size 200M

除非在过去的几个月里发生了什么变化,否则这个变通方法对我来说总是有效的。

票数 2
EN

Stack Overflow用户

发布于 2012-10-15 03:56:59

jupload可以通过ftp上传

http://jupload.sourceforge.net/

这是一个好的解决方案。虽然不是所有用户都启用java,但您可以使html页面优雅地降级为http上传,在这种情况下,唯一失败的用户是那些想要上传大文件的用户。

或者切换网络主机--另一个简单有效的解决方案。

票数 0
EN

Stack Overflow用户

发布于 2015-08-18 05:03:34

您可以将FTP与"Blueimp Jquery文件上传插件“一起使用,但它需要一些PHP代码。不要修改UploadHandler类(文件上传/服务器/php/UploadHandler.php),相反,您应该扩展它并根据需要覆盖函数。

将file-uploads/server/php/index.php的内容替换为:

代码语言:javascript
复制
require('UploadHandler.php');

/*
 *  custom class for uploading files, simply extends the UploadHander class
 */
class CustomUploadHandler extends UploadHandler {


    /*
     * rewreite the handle_file_upload() function to use FTP to send the files to the FTP server
     */
    protected function handle_file_upload($uploaded_file, $name, $size, $type, $error, $index = null, $content_range = null) {
        $file = new stdClass();
        $file->name = $this->get_file_name($uploaded_file, $name, $size, $type, $error,
            $index, $content_range);
        $file->size = $this->fix_integer_overflow((int)$size);
        $file->type = $type;
        if ($this->validate($uploaded_file, $file, $error, $index)) {
            $this->handle_form_data($file, $index);
            $upload_dir = $this->get_upload_path();
            if (!is_dir($upload_dir)) {
                mkdir($upload_dir, $this->options['mkdir_mode'], true);
            }
            $file_path = $this->get_upload_path($file->name);
            $append_file = $content_range && is_file($file_path) &&
                $file->size > $this->get_file_size($file_path);
            if ($uploaded_file && is_uploaded_file($uploaded_file)) {
                // multipart/formdata uploads (POST method uploads)
                if ($append_file) {
                    file_put_contents(
                        $file_path,
                        fopen($uploaded_file, 'r'),
                        FILE_APPEND
                    );
                } else {
                    move_uploaded_file($uploaded_file, $file_path);
                }
            } else {
                // Non-multipart uploads (PUT method support)
                file_put_contents(
                    $file_path,
                    fopen('php://input', 'r'),
                    $append_file ? FILE_APPEND : 0
                );
            }
            $file_size = $this->get_file_size($file_path, $append_file);
            if ($file_size === $file->size) {
                $file->url = $this->get_download_url($file->name);
                if ($this->is_valid_image_file($file_path)) {
                    $this->handle_image_file($file_path, $file);
                }
            } else {
                $file->size = $file_size;
                if (!$content_range && $this->options['discard_aborted_uploads']) {
                    unlink($file_path);
                    $file->error = $this->get_error_message('abort');
                }
            }
            $this->set_additional_file_properties($file);
        }

        /* 
         * now send it to the FTPserver
         */
        $source_file = $file_path; // the path of the file to upload
        $destination_file = '/your/path/' . $file->name; // the file path to upload to (must have trailing / in the path before the $file->name is appended)

        $ftp_error = $this->ftp_upload($source_file, $destination_file);

        if ($ftp_error != '') {
          $file->error = $ftp_error;
        }

        return $file;
    }


    /*
     * new function for uploading using FTP
     */
    protected function ftp_upload($source_file, $destination_file) {

        $ftp_server = 'YOUR FTP HOST NAME';
        $ftp_user_name = 'YOUR USER NAME';
        $ftp_user_pass = 'YOUR PASSWORD';
        $port_number = YOUR FTP PORT NUMBER;

        $error = '';

        // set up basic connection
        $conn_id = ftp_connect($ftp_server, $port_number); 
        if (!$conn_id) {
            $error = "FTP connection has failed! Connection attempt was blocked." ;
            return $error;
        }

        // login with username and password
        $login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass); 

        // check connection
        if (!$login_result) { 
            $error = "FTP connection has failed! ";
            $error .= "Could not log into fpt://$ftp_server for user $ftp_user_name"; 
            return $error;
        }

        // upload the file
        $upload = ftp_put($conn_id, $destination_file, $source_file, FTP_BINARY); 

        // check upload status
        if (!$upload) { 
            $error = "FTP upload has failed!";
        } else {
            $error =  "Uploaded $source_file to $ftp_server as $destination_file";
        }

        // close the FTP stream 
        ftp_close($conn_id); 

        return $error;
    }

}

// options to pass to the upload handler object
$options = [
   // ANY OPTIONS, SUCH AS FILE UPLOAD LOCATION
   'upload_url' => 'fpt://YOUR-FTP-SERVER-NAME/files/',
];

// finally, instantiate the new class   
$upload_handler = new CustomUploadHandler($options);

请注意,这只是在文件上传到上传服务器之后,通过ftp将文件复制到ftp服务器。这意味着您将拥有同一文件的两个副本。您可以在不做太多额外工作的情况下删除最初上传的文件,但请确保ftp操作已先完成。

您将需要此和您将需要.htaccess apache设置,如下所述

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12885883

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档