I'm trying to upload a file from the local machine drive to a server.
I uploaded the file with the following code but it uploads the file with 0 byte size or a blank file.
Also, I did not get all the file path of the local file. It shows the server path. For example, if my local file is on D:/abc/abc.txt, it shows /public_html/abc.txt
Please help.
<?php
echo "hi";
echo "\n";
$host = '000.000.00.000';
$usr = 'userName';
$pwd = 'password';
$temp = $_FILES["file"];
$local_file = realpath($_FILES["file"]["name"]["r"]);
$ftp_path = "/public_html/Download/".$_POST['file'];
echo "This is file to Upload :";
echo "\n";
print($local_file);
// connect to FTP server (port 21)
$conn_id = ftp_connect($host, 21) or die ("Cannot connect to host");
// send access parameters
ftp_login($conn_id, $usr, $pwd) or die("Cannot login");
// perform file upload
print ("This is ftp path : ");
print($ftp_path);
print("New line : ");
$upload = ftp_put($conn_id, $ftp_path, $local_file , FTP_ASCII , 0);
print (!$upload) ? 'Cannot upload' : 'Upload complete';
print ("\n") ;
ftp_close($conn_id);
?>
$_FILES['file']['name']
is the name of the file on the client machine, not the server. The upload is put into $_FILES['file']['tmp_name']
on the server, so this is the file that you should send with FTP.
You can use $_FILES['file']['name']
when setting $ftp_path
, so that the destination file will have the same name as the user's original file. $_POST['file']
doesn't exist -- file uploads are only put in $_FILES
, not $_POST
.