Upload large files to FTP with PHP

11,407

Solution 1

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/

Solution 2

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
Share:
11,407
bozdoz
Author by

bozdoz

Updated on June 04, 2022

Comments

  • bozdoz
    bozdoz almost 2 years

    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?

  • Sauleil
    Sauleil about 12 years
  • bozdoz
    bozdoz about 12 years
    @AngeDeLaMort Wow. I'll try it and let you know.
  • Ibrahim Azhar Armar
    Ibrahim Azhar Armar about 12 years
    never knew about this. removed -1, +1 for correctly pointing it out.
  • bozdoz
    bozdoz about 12 years
    Page cannot be displayed when I create an .htaccess file: HTTP Error 500
  • Ibrahim Azhar Armar
    Ibrahim Azhar Armar about 12 years
    do you have any other rule set up in your .htaccess file?
  • bozdoz
    bozdoz about 12 years
  • Ibrahim Azhar Armar
    Ibrahim Azhar Armar about 12 years
    please make sure you place .htaccess file in the correct directory.
  • bozdoz
    bozdoz about 12 years
    I put it in the same directory as the php page. It results in an HTTP Error 500
  • Ibrahim Azhar Armar
    Ibrahim Azhar Armar about 12 years
    place the file in the root directory, i.e if you are using linux server then directly in public_html directory or in windows www directory.