PHP connection FTP to achieve file upload download delete file instance code

  • 2020-03-31 20:48:18
  • OfStack

PHP FTP files to the server
 
<?php 
//start
$ret = ftp_nb_get ($my_connection, "test", "README", FTP_BINARY, 
filesize("test")); 
//$ret = ftp_nb_get ($my_connection, "test", "README",
// FTP_BINARY, FTP_AUTORESUME); 
while ($ret == FTP_MOREDATA) { 

//You can insert other code
echo "."; 
//Continue to transmit...
$ret = ftp_nb_continue ($my_connection); 
} 
if ($ret != FTP_FINISHED) { 
echo " Download error ..."; 
exit(1); 
} 
?> 

PHP FTP deletes files
 
<?php 
$file = 'public_html/old.txt'; 
//Connect to FTP server
$conn_id = ftp_connect('www.jb51.net'); 
//Verify the username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass); 
//Delete specified file
if (ftp_delete($conn_id, $file)) { 
echo "$file  File deleted successfully  n"; 
} else { 
echo " delete  $file  The file failed n"; 
} 
//Close FTP connection
ftp_close($conn_id); 
?> 

Download files for PHP FTP
 
<?php 
$file = 'somefile.txt'; 
//Connect to FTP server
$conn_id = ftp_connect($ftp_server); 
//Verify username and password www.jb51.net
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass); 
//Gets the size of the specified file
$res = ftp_size($conn_id, $file); 
if ($res != -1) { 
echo " $file  The file size is  $res byte "; 
} else { 
echo " Failed to get remote file size "; 
} 
//Close FTP connection
ftp_close($conn_id); 
?> 

Related articles: