This example will inform you about uploading the file on the server.
This example will inform you about uploading the file on the server.Uploading the file on Server
This example will explain you how to upload the file on FTP server. ftp_put() command allows to upload an existing file on the server. For uploading a file onto the FTP server, first you have to login on the FTP server, and searching the source file to be uploaded. Define the destination path of the source file.
Then check the basic connection. If it is not connected well, set up a connection using ftp_connect($ftp_server).
Check the connection of FTP server for user name.
upload the file using ftp_put() function in which you will have to define connection id, destination file, source file and FTP_Binary.
Check upload status, and close the FTP stream.
End the program.
<?php
$ftp_server = "<IP>";
$ftp_user_name = "<username>";
$ftp_user_pass = "<password>";
$destination_file = "C:\\wamp\\www\\projects\\public_html\\upload_file\\".$_FILES['image']['name'];
$sourcefile = $_FILES['image']['name'];
// 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);
// check connection
if ((!$conn_id) || (!$login_result)) {
echo "FTP connection has failed!";
echo "Attempted to connect to $ftp_server for user $ftp_user_name";
exit;
} else {
echo "Connected to $ftp_server, for user $ftp_user_name";
}
// upload the file
$upload = ftp_put($conn_id, $destination_file, $source_file, FTP_BINARY); // line 30
// check upload status
if (!$upload) {
echo "FTP upload has failed!";
} else {
echo "Uploaded $source_file to $ftp_server as $destination_file";
}
// close the FTP stream
ftp_close($conn_id);
?>