I want to upload files from user's machine to folder uploads
in my FTP and it doesn't work. Can you help me?
$ftp_server = "some ip";
$ftp_conn = ftp_connect($ftp_server) or die("Could not connect to $ftp_server");
$login = ftp_login($ftp_conn, "some name", "some password");
$target_dir = "uploads/";
$target_file = basename($_FILES["filename"]["name"]);
if (ftp_fput($ftp_conn, $target_dir.$target_file, FTP_ASCII))
{
echo "Successfully uploaded $target_file.";
}
else
{
echo "Error uploading $target_file.";
}
use php-ftp-client library:
You need to specify what local file (as of the webserver) you want to upload to the FTP server.
You can retrieve a name of a temporary file that contains a file uploaded via HTTP POST from user's machine to your webserver using
$_FILES["filename"]["tmp_name"]
. Read about POST method uploads in PHP.You can then pass that to
ftp_put
(no need forftp_fput
):Two other issues in your code (which are not your immediate problems, but you will face them right after you resolve it):
Absolutely do not use
FTP_ASCII
, if you are uploading binary files, like.mp3
. UseFTP_IMAGE
.You need to use
ftp_pasv
.