dry18813
dry18813
2012-05-01 14:44

使用PHP将大文件上传到FTP

已采纳

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?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

2条回答

  • dongzengzai4567 dongzengzai4567 9年前

    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/

    点赞 评论 复制链接分享
  • duanaoshu1989 duanaoshu1989 9年前

    as rightly pointed out by @AngeDeLaMort, you cannot use shorthand notation to set configuration values outside of PHP.ini.

    reference : Changing upload_max_filesize on PHP

    try doing it this way.

    create .htaccess file in your root directory and add the following.

    php_value upload_max_filesize 50M
    php_value post_max_size 50M
    php_value max_execution_time 200
    php_value max_input_time 200
    
    点赞 评论 复制链接分享

相关推荐