8

I want to upload a file via FTP upload in a form.

<html>
  <body>
    <form enctype="multipart/form-data" action="upload_file.php" method="POST">
      <input type="hidden" name="MAX_FILE_SIZE" value="100000" />
      Choose a file to upload: <input name="uploadedfile" type="file" /><br />
      <input type="submit" value="Upload File" />
    </form>
  </body>
</html>

Here is the PHP file:

<?php

$ftp_server = "xxx";
$ftp_username   = "xxx";
$ftp_password   =  "xxx";

// setup of connection
$conn_id = ftp_connect($ftp_server) or die("could not connect to $ftp_server");

// login
if (@ftp_login($conn_id, $ftp_username, $ftp_password))
{
  echo "conectd as $ftp_username@$ftp_server\n";
}
else
{
  echo "could not connect as $ftp_username\n";
}

$file = $_FILES["file"]["name"];
$remote_file_path = "/home/www/lifestyle69/import/".$file;
ftp_put($conn_id, $remote_file_path, $file, FTP_ASCII);
ftp_close($conn_id);
echo "\n\nconnection closed";

?>

The FTP connection connects successfully but the file is nowhere.

Can anybody help me?

Thanks!

0

3 Answers 3

7

Because you have <input name="uploadedfile" type="file" />:

$file = $_FILES["file"]["name"]; // wrong
$file = $_FILES["uploadedfile"]["name"]; // right

Because you need the filename of the temporary copy stored by PHP, which exists on the server:

ftp_put($conn_id, $remote_file_path, $file, FTP_ASCII); // wrong
ftp_put($conn_id, $remote_file_path, $_FILES["uploadedfile"]["tmp_name"],
        FTP_ASCII); // right

Refer to the PHP documentation for more information about $_FILES.

Sign up to request clarification or add additional context in comments.

Comments

1

Are you sure that the folder you are uploading to has the correct permissions? Try chmoding it to 777 and see if that works.

3 Comments

Just a suggestion: Never suggest "try chmoding to 777" without a disclaimer "do this just for testing. Revert it back to safer permissions once you found what the problem is".
@mmalmeida I wonder about that. aren't permissions on the server behind a passworded ftp connection anyway? even if I have a 777 folder on my ftp host, it can't be written from internet, you have to be logged in to the server.
Felix, setting file permissions to 777 will allow anyone to read, write, or execute that file. No FTP access is required, you can access it through http.
1

The file is stored on server with temporary name, so when you try uploading $_FILES['file']['name'], it fails, because file with such name does not exist. Instead you should call ftp_put() with $_FILES['file']['tmp_name']

It's explained a little better here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.