Troubleshooting ftp_put Failure in PHP
Problem:
When attempting to upload an XML file to an FTP server using the ftp_put function in PHP, the operation fails, returning false.
Cause:
One common cause of ftp_put failures is that PHP defaults to using the active FTP mode, which is often incompatible with many FTP servers due to firewall or network restrictions. Switching to the passive mode usually resolves this issue.
Solution:
To switch to the passive mode in PHP, use the ftp_pasv function after establishing an FTP connection:
<code class="php">$connect = ftp_connect($ftp) or die("Unable to connect to host"); ftp_login($connect, $username, $pwd) or die("Authorization failed"); // Turn passive mode on ftp_pasv($connect, true) or die("Unable to switch to passive mode");</code>
Additional Considerations:
<code class="php">ftp_set_option($connect, FTP_USEPASVADDRESS, false);</code>
The above is the detailed content of When an FTP File Transfer with ftp_put Fails in PHP, Is It Caused by FTP Mode Improperly Set?. For more information, please follow other related articles on the PHP Chinese website!