Marty1452
Marty1452

Reputation: 405

PHP uploading file from browser to FTP

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.";
  }

Upvotes: 1

Views: 1535

Answers (1)

Martin Prikryl
Martin Prikryl

Reputation: 202272

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 for ftp_fput):

$remote_filename = $target_dir . $target_file;
$local_filename = $_FILES["filename"]["tmp_name"];
ftp_put($ftp_conn, $remote_filename, $local_filename, FTP_BINARY)

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. Use FTP_BINARY.

  • You need to use ftp_pasv.

Upvotes: 1

Related Questions