2

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?

bozdoz
  • 12,550
  • 7
  • 67
  • 96
  • Where did you set the timelimit? – fhugas May 01 '12 at 14:48
  • some hosting companies will only let you set that so high. Also, rather than setting it everytime the script runs, why not just put it in the php.ini file? – Rooster May 01 '12 at 14:49
  • Is the page loading for ever? If not, the reason that it doesn't echo something must be because `$_FILES['upload_file']['name']` is `''`. And make sure error reporting is on. – Sietse May 01 '12 at 14:49
  • @fhugas I set it at the very top: set_time_limit(0) – bozdoz May 01 '12 at 14:49
  • @Sietse How do I turn error reporting on? It will upload small files, but not large files. – bozdoz May 01 '12 at 14:50
  • You can turn it on with `error_reporting(-1);`. – Sietse May 01 '12 at 14:52
  • You sure set_time_limit() is working? Some hosts require you to make a php.ini file in the directory where your php file resides. – fhugas May 01 '12 at 14:57
  • @Sietse Error reporting didn't seem to do anything. – bozdoz May 01 '12 at 15:00
  • for the web config, maybe you can have a look at the http runtime section: http://msdn.microsoft.com/en-us/library/e1f13641.aspx – Sauleil May 04 '12 at 14:53

2 Answers2

2

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/

Community
  • 1
  • 1
Sauleil
  • 2,573
  • 1
  • 24
  • 27
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
Community
  • 1
  • 1
Ibrahim Azhar Armar
  • 25,288
  • 35
  • 131
  • 207