[thelist] FTP with PHP

Pablo Oliva poliva at cox.net
Sun Dec 15 22:14:01 CST 2002


> I have made every attempt to ftp with php, but no luck.  I
> have used the ftp functions, but php uploads zero byte files only:
>
> ------------------------ Code ----------------------
> <?php
> function ftp( $ftp_server, $ftp_user_name, $ftp_user_pass,
> $destination_file, $source_file ) {  $conn_id = ftp_connect(
> $ftp_server );  $login_result = ftp_login( $conn_id,
> $ftp_user_name, $ftp_user_pass );  ftp_pasv( $conn_id, true );
>
>  if ( (!$conn_id) || (!$login_result) ) {
>      die( "FTP connection has failed! Attempted to connect to
> $ftp_server for user $ftp_user_name." );
>  }
>  else {
>   print "Connected to $ftp_server, for user
> $ftp_user_name.<br><br>";  }  ftp_chdir( $conn_id, "files" );
>
>  $upload = ftp_put( $conn_id, $destination_file,
> $source_file, FTP_BINARY );
>
>  if ( !$upload ) {
>   print "FTP upload has failed!";
>  }
>  else {
>   print "Uploaded $source_file to $ftp_server as
> $destination_file";  }  flush();
>
>  ftp_close( $conn_id );
> }
> ?>
> ----------------------- End Code --------------------------
>
> I have also tried using ftp_fput:
>
>  $fp = fopen( $source_file, 'r' );
>  $upload = ftp_fput( $conn_id, $destination_file, $fp, FTP_BINARY );
>
> But I get the same result, zero byte file.
>
> Then I tried using fsockopen(), using sockets to emulate an
> ftp session:
>
> ----------------------- Code -----------------------------
> <?php function ftp( $ftp_server, $ftp_user_name,
> $ftp_user_pass, $destination_file, $source_file ) {  $fp =
> fsockopen( "$ftp_server", 21, $errno, $errstr, 60 );  if ( !$fp ) {
>   print "Connection failed: $errstr ($errno)";
>   exit;
>  }
>  else {
>   socket_set_blocking( $fp, true );
>   print '<b>FTP transaction:</b><br />';
>   print fgets( $fp, 255 )."<br />";
>   fputs( $fp, "USER $ftp_user_name\r\n" );
>   print fgets( $fp, 255 )."<br />";
>   fputs( $fp, "PASS $ftp_user_pass\r\n" );
>   print fgets( $fp, 255 )."<br />";
>   // works up to here
>   fputs( $fp, "CWD files\r\n" );
>   print fgets( $fp, 255 )."<br />";
>   fputs( $fp, "TYPE A\r\n" );
>   print fgets( $fp, 255 )."<br />";
>   fputs( $fp, "PASV\r\n" );
>   print fgets( $fp, 255 )."<br />";
>   fputs( $fp, "STOR $source_file\r\n" );
>   print fgets( $fp, 255 )."<br />";
>   fputs( $fp, "QUIT\r\n" );
>   print fgets( $fp, 255 )."<br />";
>   fclose( $fp );
>   print 'Success';
>   /*
>   */
>  }
> }
> ?>
> ----------------------- End Code -----------------------------
>
> With this, I get the everything working fine up until the
> moment of upload, then it fails. Script output:
>
> FTP transaction:
> 220 renown FTP server (CH/1.9) ready.
> 331 Password required for admin%financialnewsusa.com.
> 230 User admin logged in.
> 250 CWD command successful.
> 200 Type set to A.
> 227 Entering Passive Mode (207,155,248,63,195,80)
>
> Fatal error: Maximum execution time of 30 seconds exceeded in
> c:\apache\htdocs\fnusa\releases\ftp.php on line 28
>
> At my office machine, I have the timeout set to 90 seconds,
> and it still fails. Can anyone give me a clue?




More information about the thelist mailing list