【问题标题】:Upload files directly to the FTP server by using "Jquery file upload plugin"使用“Jquery 文件上传插件”将文件直接上传到 FTP 服务器
【发布时间】:2012-10-04 20:26:51
【问题描述】:

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

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

所以,我想知道是否有一种解决方法可以用来避免这个 HostGator 限制, 或者如果有办法允许用户通过 FTP 将文件直接上传到服务器,请抛出这个 jquery 插件“网络应用程序”。

【问题讨论】:

  • 否;你不能通过 Javascript 使用 FTP。

标签: php javascript jquery host


【解决方案1】:

绝对可以更改 hostgator 的最大文件大小设置,就像我之前做过几次一样。

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

php_value upload_max_filesize 200M
php_value post_max_size 200M

除非最近几个月发生了一些变化,否则这种解决方法一直对我有用。

【讨论】:

  • 感谢您的及时回复,但是这个解决方案对我不起作用,我只是在上传文件夹中的.htaccess文件中直接添加了这段代码“我不知道是否正确”,我还想问一下如何包含php覆盖设置“我看不懂”??请解释更多:D
【解决方案2】:

jupload可以通过ftp上传

http://jupload.sourceforge.net/

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

或切换虚拟主机 - 另一个简单有效的解决方案。

【讨论】:

    【解决方案3】:

    可以将 FTP 与“Blueimp Jquery 文件上传插件”一起使用,但它需要一些 PHP 编码。不要破坏UploadHandler 类(file-uploads/server/php/UploadHandler.php),而是应该扩展它并根据需要覆盖函数。

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

    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 操作。

    您将需要此并且您将需要 Ohgodwhy 所述的 .htaccess apache 设置

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-03-14
      • 2016-01-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多