3
votes

I have been googling the whole day trying everything I can find, but nothing works. I'm starting to get desperate.

I'm trying to make a script that can upload a file to my ftp account, but so far I've not been able to get it working.

I'm getting these errors:

Warning: ftp_put(): Opening data channel for file transfer. in C:\xampp\htdocs\AA\dwsite\ftptest\up.php on line 49

Fatal error: Maximum execution time of 30 seconds exceeded in C:\xampp\htdocs\AA\dwsite\ftptest\up.php on line 49

This is the code I'm using:

<?php
if (isset ( $_FILES ['be_file'] )) {

    $file_size = $_FILES ['be_file'] ['be_file'];
    $file_type = $_FILES ['be_file'] ['type'];
    $source_file = $_FILES ['be_file'] ['tmp_name'];
    $destination_file = $_FILES ['be_file'] ['name'];
//ftp details   
    $ftp_server = 'ip of ftp';
    $ftp_port = 'port number';
    $ftp_user_name = 'username';
    $ftp_user_pass = 'pass';

    // set up basic connection
    $conn_id = ftp_connect ( $ftp_server, $ftp_port );
    ftp_pasv ( $conn_id, true );
    // login with username and password

    $login_result = ftp_login ( $conn_id, $ftp_user_name, $ftp_user_pass );

    // upload a file
    if (ftp_put ( $conn_id, $destination_file, $source_file, FTP_BINARY )) {
        echo "successfully uploaded $source_file\n";
        exit ();
    } else {
        echo "There was a problem while uploading $source_file\n";
        exit ();
    }
    // close the connection
    ftp_close ( $conn_id );
    echo "Success";
}

?>
<html>
<body>

    <form action="" method="POST" enctype="multipart/form-data">
        <input type="file" name="file" /> <input type="submit" />

        <ul>
            <li>Sent file: <?php echo $_FILES['be_file']['name'];  ?>

            <li>File size: <?php echo $_FILES['be_file']['size'];  ?>

            <li>File type: <?php echo $_FILES['be_file']['type']?>

        </ul>

    </form>

</body>
</html>
2

2 Answers

2
votes

max_execution_time integer

  • Add max_execution_time = 300 into your php.ini file server. Then restart your php-fpm service and restart your server.
  • Or add set_time_limit(0); in your code.
  • Or If you havn't access to your php.ini file, you could extend the maximum execution time like this: ini_set('max_execution_time', 300); //300 seconds = 5 minutes

This sets the maximum time in seconds a script is allowed to run before it is terminated by the parser. This helps prevent poorly written scripts from tying up the server. The default setting is 30. When running PHP from the command line the default setting is 0.

The maximum execution time is not affected by system calls, stream operations etc. Please see the set_time_limit() function for more details.

You can not change this setting with ini_set() when running in safe mode. The only workaround is to turn off safe mode or by changing the time limit in the php.ini.

Php doc -> Max_execution_time

0
votes

I think you had this error because you called the ftp_pasv function before the login:

ftp_pasv ( $conn_id, true );
$login_result = ftp_login ( $conn_id, $ftp_user_name, $ftp_user_pass );

Should be:

$login_result = ftp_login ( $conn_id, $ftp_user_name, $ftp_user_pass );
ftp_pasv ( $conn_id, true );

If it can help someone. ;)