简体   繁体   English

使用PHP将大文件上传到FTP

[英]Upload large files to FTP with PHP

I'm trying to upload large files with php to an ftp server. 我正在尝试使用php将大文件上传到ftp服务器。 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. 我查了一下,发现需要设置upload_max_filesize和post_max_size,将ftp设置为被动模式,并将时间限制设置为从不。 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. 如果查看底部的if (!$upload) {行,它应该回显某些内容,但事实并非如此。 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 />";
        }       
}

UPDATE UPDATE

I've discovered that I cannot set the upload_max_filesize value from the php page; 我发现我无法从php页面设置upload_max_filesize值; 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. 另外,我似乎无法使.htaccess正常工作:如果我有.htaccess文件,它将导致HTTP错误500。而且,我无权访问php.ini。

How else can I change the upload_max_filesize value? 我还能如何更改upload_max_filesize值?

More Information 更多信息

My web administrator has told me that I am on an IIS windows-based system, so .htaccess files won't work. 我的网络管理员告诉我,我在基于IIS Windows的系统上,所以.htaccess文件不起作用。 Is there a way that I can affect the file upload size with web.config? 有没有一种方法可以影响web.config的文件上传大小?

From this site: http://www.php.net/manual/en/ini.php 从此站点: http : //www.php.net/manual/zh/ini.php

'upload_max_filesize' and 'post_max_size' 'upload_max_filesize'和'post_max_size'

are of type PHP_INI_PERDIR 属于PHP_INI_PERDIR类型

which means Entry can be set in "php.ini, .htaccess or httpd.conf". 这意味着可以在“ php.ini,.htaccess或httpd.conf”中设置条目。 So you can't set it in your script. 因此,您无法在脚本中进行设置。

--- Edit --- -编辑-

A similar question: Changing upload_max_filesize on PHP 一个类似的问题: 在PHP上更改upload_max_filesize

--- Edit 2 --- -编辑2-

Personally, I wanted to do the same kind of stuff (upload large files over ftp) and end up writing a java application. 就个人而言,我想做同样的事情(通过ftp上传大文件)并最终编写一个Java应用程序。 For handling large files, php is not really the best way. 对于处理大文件,php并不是真正的最佳方法。 Web browsers doesn't really like that. Web浏览器并非如此。 So, for handling large files, I would suggest to look at other alternative: (java, swf, javascript, ...) 因此,对于处理大文件,我建议考虑其他方法:(java,swf,javascript,...)

Something I would like to give a try when I have some time is http://nodejs.org/ 有空的时候我想尝试一下的东西是http://nodejs.org/

as rightly pointed out by @AngeDeLaMort, you cannot use shorthand notation to set configuration values outside of PHP.ini. 正如@AngeDeLaMort正确指出的那样,您不能使用速记符号在PHP.ini之外设置配置值。

reference : Changing upload_max_filesize on PHP 参考: 在PHP上更改upload_max_filesize

try doing it this way. 尝试这样做。

create .htaccess file in your root directory and add the following. 在您的根目录中创建.htaccess文件,并添加以下内容。

php_value upload_max_filesize 50M
php_value post_max_size 50M
php_value max_execution_time 200
php_value max_input_time 200

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

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