使用 PHP 将大文件上传到 FTP [英] Upload large files to FTP with PHP

查看:21
本文介绍了使用 PHP 将大文件上传到 FTP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 php 将大文件上传到 ftp 服务器.我可以上传小文件,但上传大文件时遇到问题.我查了一下,发现需要设置upload_max_filesize和post_max_size,设置ftp为被动模式,设置时间限制为never.我没有设置时间限制,我现在没有返回任何错误,但它也没有上传文件.如果您查看底部的 if (!$upload) { 行,它应该会回显某些内容,但事实并非如此.更重要的是,它只是不起作用.关于出了什么问题或我需要做些什么来完成这项工作的任何见解?谢谢!

I'm trying to upload large files with php to an ftp server. I can upload small files, but I'm having trouble uploading larger files. I have looked it up and found that I need to set upload_max_filesize and post_max_size, set ftp to passive mode, and set time limit to never. I had no luck with setting the time limit, and what I have now isn't returning any errors, but it is also not uploading the file. If you look at the if (!$upload) { line at the bottom, it should echo something, but it isn't. More importantly, it just isn't working. Any insight as to what is going wrong or what I need to do to make this work? Thank you!

ini_set('upload_max_filesize', '50M');   
ini_set('post_max_size', '50M');  

$conn_id = ftp_connect($ftp_server);

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

// check connection
if ((!$conn_id) || (!$login_result)) {
        echo "FTP connection has failed!";
        echo "Attempted to connect to $ftp_server for user $ftp_user_name";
        exit;
}

 // turn passive mode on
ftp_pasv($conn_id, true);

if($_FILES['upload_file']['name']!=''){
    $source_file = $_FILES['upload_file']['tmp_name'];
    $destination_file = $_FILES['upload_file']['name']; 

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

    // check upload status
    if (!$upload) {
            echo "FTP upload has failed!<br />";
        } else {
            echo "Uploaded $source_file to $ftp_server as $destination_file<br />";
        }       
}

更新

我发现我无法从 php 页面设置 upload_max_filesize 值;另外,我似乎无法让 .htaccess 工作:如果我有一个 .htaccess 文件,则会导致 HTTP 错误 500.此外,我无权访问 php.ini.

UPDATE

I've discovered that I cannot set the upload_max_filesize value from the php page; also, I can't seem to get .htaccess to work: if I have an .htaccess file it results in a HTTP Error 500. Also, I don't have access to php.ini.

我还能如何更改upload_max_filesize 值?

How else can I change the upload_max_filesize value?

我的网络管理员告诉我,我使用的是基于 IIS Windows 的系统,因此 .htaccess 文件将无法工作.有没有办法可以通过 web.config 影响文件上传大小?

My web administrator has told me that I am on an IIS windows-based system, so .htaccess files won't work. Is there a way that I can affect the file upload size with web.config?

推荐答案

来自这个网站:http://www.php.net/manual/en/ini.php

'upload_max_filesize' 和 'post_max_size'

'upload_max_filesize' and 'post_max_size'

属于 PHP_INI_PERDIR 类型

are of type PHP_INI_PERDIR

表示Entry可以在php.ini, .htaccess or httpd.conf"中设置.所以你不能在你的脚本中设置它.

which means Entry can be set in "php.ini, .htaccess or httpd.conf". So you can't set it in your script.

--- 编辑---

一个类似的问题:在 PHP 上更改 upload_max_filesize

--- 编辑 2 ---

--- Edit 2 ---

就个人而言,我想做同样的事情(通过 ftp 上传大文件)并最终编写一个 java 应用程序.对于处理大文件,php 并不是最好的方法.Web 浏览器并不喜欢这样.因此,对于处理大文件,我建议查看其他替代方案:(java, swf, javascript, ...)

Personally, I wanted to do the same kind of stuff (upload large files over ftp) and end up writing a java application. For handling large files, php is not really the best way. Web browsers doesn't really like that. So, for handling large files, I would suggest to look at other alternative: (java, swf, javascript, ...)

我有空想试试 http://nodejs.org/

这篇关于使用 PHP 将大文件上传到 FTP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆