note 39448 modified in function.fopen by danbrown

From: Date: Mon, 08 Jun 2009 23:44:08 +0000
Subject: note 39448 modified in function.fopen by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-156568@lists.php.net to get a copy of this message
If you are connecting to your ftp server through a router doing NAT (such as the Zyxel 128L prestige router/bridge we are using) by doing say an <?php fopen("ftp://ftpusername:ftppassword@example.com/".$file_name, "w") ?>, then this could fail. You will get php_hostconnect connection failed error. This is because fopen() function uses a passive ftp connection which the Zyxel router does not support, even though your ftp server may be configured to allow the passive connections which php fopen() function is using. Note that $file_name is the file we want to ftp to the remote server eg could be file.txt. Thus an alternative would be to do create the file you want in a local directory of your machine or in the webserver where your php files reside eg use fwrite() as documented in the manual. Once you have the $file_name you want to ftp created, do the following: <?php ftp_connect($ftp_server); //which can connect to the ftp server using an active connection. ftp_login ($conn_id, $ftp_username."@".$ftp_server, $ftp_password); $fp = fopen($PATH."".$file_name, 'r'); // eg. $fp = fopen("http://www.yourwebsite.com/".$file_name, 'r'); //turn off passive mode transfers ftp_pasv ($conn_id, false); //now upload $file_name ftp_fput($conn_id, $file_name, $fp, FTP_ASCII)); ?> Your file should now be in your ftp server, having used an active connection. --was-- If you are connecting to your ftp server through a router doing NAT (such as the Zyxel 128L prestige router/bridge we are using) by doing say an <? fopen("ftp://ftpusername:ftppassword@ftpserver/".$file_name, "w") ?>, then this could fail. You will get php_hostconnect connection failed error. This is because <?fopen() ?>function uses a passive ftp connection which the Zyxel router does not support, even though your ftp server may be configured to allow the passive connections which php <?fopen?> function is using. Note that $file_name is the file we want to ftp to the remote server eg could be file.txt. Thus an alternative would be to do create the file you want in a local directory of your machine or in the webserver where your php files reside eg use <?fwrite()?> as documented in the manual. Once you have the $file_name you want to ftp created, do the following: <? ftp_connect($ftp_server); //which can connect to the ftp server using an active connection. ftp_login ($conn_id, $ftp_username."@".$ftp_server, $ftp_password); $fp = fopen($PATH."".$file_name, 'r'); // eg. $fp = fopen("http://www.yourwebsite.com/".$file_name, 'r'); //turn off passive mode transfers ftp_pasv ($conn_id, false); //now upload $file_name ftp_fput($conn_id, $file_name, $fp, FTP_ASCII)); ?> Your file should now be in your ftp server, having used an active connection. http://php.net/manual/en/function.fopen.php

« previous php.notes (#156568) next »