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

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

问题描述

我试图用php上传大文件到一个ftp服务器。我可以上传小文件,但在上传大文件时遇到问题。我查了一下,发现我需要设置upload_max_filesize和post_max_size,将ftp设置为被动模式,并将时间限制设置为从不。我没有设置时间限制的运气,我现在有什么不返回任何错误,但它也没有上传文件。如果你看看 if(!$ upload){ line在底部,它应该回显一些东西,但是不是。更重要的是,它只是不工作。对于发生了什么问题或者我需要做什么来做这个工作有什么见解?谢谢!

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

$ conn_id = ftp_connect($ ftp_server);

//使用用户名和密码登录
$ login_result = ftp_login($ conn_id,$ ftp_user_name,$ ftp_user_pass);
$ b $ //检查连接
if((!!conn_id)||(!$ login_result)){
echoFTP连接失败!
echo试图连接到$ ftp_server用户$ ftp_user_name;
出口;
}

//在
上启用被动模式ftp_pasv($ conn_id,true);

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

//上传文件
$ upload = ftp_put($ conn_id,$ destination_file,$ source_file,FTP_BINARY);

//检查上传状态
if(!$ upload){
echoFTP upload has failed!< br />;
} else {
echo上传$ source_file到$ ftp_server作为$ destination_file< br />;


code
$ b $ h2 UPDATE

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

如何更改upload_max_filesize值?



更多信息



我的网页管理员告诉我,我在一个IIS基于Windows的系统,所以.htaccess文件将无法正常工作。有没有办法,我可以影响文件上传大小与web.config?

解决方案

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


'upload_max_filesize'和'post_max_size'


是类型PHP_INI_PERDIR



这意味着Entry可以在php.ini,.htaccess或httpd.conf中设置。所以你不能在你的脚本中设置。

编辑---

类似问题:
更改PHP上的upload_max_filesize



---编辑2 ---

个人而言,我想做同样的东西(通过ftp上传大文件)并最终编写一个Java应用程序。为了处理大文件,php并不是最好的方法。网页浏览器并不真的喜欢这个。所以,为了处理大文件,我建议看看其他的选择:(java,swf,javascript,...)



我想试一试当我有一段时间是 http://nodejs.org/


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 />";
        }       
}

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.

How else can I change the upload_max_filesize value?

More Information

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?

解决方案

From this site: http://www.php.net/manual/en/ini.php

'upload_max_filesize' and 'post_max_size'

are of type PHP_INI_PERDIR

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

--- Edit ---

A similar question: Changing upload_max_filesize on PHP

--- Edit 2 ---

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, ...)

Something I would like to give a try when I have some time is http://nodejs.org/

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

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