Timeout to Upload FTP file in PHP

Asked

Viewed 224 times

2

Friends, I am trying to develop the upload of files to FTP by PHP. When running it it successfully connects to the FTP server. However, the page keeps loading until a timeout and does not finish sending the whole file.

Follow code for used. Any ideas on how to solve this case ?

<?php
    $file = 'teste.txt';
    $remote_file = '/teste.txt';
    $ftp_server = "IPSERVER";
    $ftp_user_name = "user_name";
    $ftp_user_pass = "user_pass";

    // set up basic connection
    $conn_id = ftp_connect($ftp_server);

    // login with username and password
    $login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
    ftp_pasv($conn_id, true);

    // upload a file
    if (ftp_put($conn_id, $remote_file, $file, FTP_ASCII)) {
     echo "successfully uploaded $file\n";
    } else {
     echo "There was a problem while uploading $file\n";
    }

    // close the connection
    ftp_close($conn_id);
 ?>

1 answer

0


Vinicius, we have two situations. The first is the question of the run time set in your php.ini in the max_execution_time directive. Because this if not changed is with default 30 seconds, which is relatively low depending on the file to be sent.

Soon, you can start your script with this code: ini_set("max_execution_time",3000);

Another observation, too, that you can change is in ftp_connect($ftp_server); The way you set it comes as default 90 seconds of connection. So consider increasing the time on the connection itself like this: ftp_connect($ftp_server,21,10); the second parameter is the port, obviously, and the third the connection attempt time. Consider changing the latter as needed.

Anyway, there are some alternatives. But I believe that the first solution will resolve your request. Hug, I hope I helped. Good morning

If your server is on Windows you will need to set up an FTP server on your computer to run your script. As it was not mentioned for which operating system, just as we do not have version. I will put below how to set up an FTP server for Windows Server 2003. If your version is different you can search on, but will have a north:

NOTE: On Windows Server 2003, the FTP Service is not installed by default when installing IIS. If you have already installed IIS on your computer, you should use the Add or Remove Programs tool in the Control Panel to install the FTP Service.
Click Start, point to Control Panel and click Add or Remove Programs.
Click Add or Remove Windows Components.
In the Components list, click Application Server,
click on Internet Information Services (IIS) (but do not check or uncheck the check box) and click on Details.
Click to mark the following checkboxes (if not already selected):
Common files
FTP (File Transfer Protocol)
Internet Information Services Manager
Click to mark the checkboxes next to any other IIS-related service or subcomponent you want to install and click OK.
Click on Next.
Upon request, insert the Windows Server 2003 CD-ROM into the computer’s CD-ROM or DVD-ROM drive or provide the path to the file location and
click OK.
Click on Finish.
IIS and the FTP service are now installed. You must configure the FTP Service before you can use it.

Follow an example, basic but practical to send the file via ftp:

<?php
if( $_SERVER['REQUEST_METHOD']=='POST' )
{
        //var_dump( $_FILES );//apenas para debug
        $servidor = '192.168.0.25';
        $caminho_absoluto = '';
        $arquivo = $_FILES['arquivo'];

        $con_id = ftp_connect($servidor) or die( 'Não conectou em: '.$servidor );

        ftp_login( $con_id, 'Leonardo', '1111' );

        echo 'Nome '.$arquivo['name'].'<br>';
        echo 'temp: '.$arquivo['tmp_name'];

        ftp_put( $con_id, $caminho_absoluto.$arquivo['name'], $arquivo['tmp_name'], FTP_BINARY );
}
?>
        <form action="" method="post" enctype="multipart/form-data">
            <input type="file" name="arquivo" />
            <input type="submit" name="enviar" value="Enviar" />
        </form>
  • Leandro, thanks for the support. With this solution, I can now upload to a linux FTP. However, I have created an FTP on Windows server and the Timeout problem persists. Is there any parameter for FTP created in Windows environment?

  • I will edit the answer and add the solution to windows

  • edited, it is always important to add in the question as much information as possible so that we can help you accurately

  • Leandro, thank you again, the FTP server in windows is already created and we were able to access it via CMD or even browser with credentials. I believe the problem may be in the Script. I would like to know if there is any other parameter to be sent ?

  • I put a practical example, see if it helps you solve your situation

Browser other questions tagged

You are not signed in. Login or sign up in order to post.