0

I have tried files upload to server using ftp connection in php and its not working, its connecting but getting Error like "Connected to XXXXXXXXXXX, for user XXXXXXXXXXXXX FTP upload has failed!" I have tried following code please help by correcting it,..

image.html

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Welcome</title>
</head>

<body>
<form action="upload.php" enctype="multipart/form-data" method="post">
<input name="file" type="file" />
<input name="submit" type="submit" value="Upload File" />
</form>
</body>
</html>

upload.php

<?php
$ftp_server = "XXXXXX"; 
$ftp_user_name = "XXXXXXX"; 
$ftp_user_pass = "XXXXXXXX";  
$destination_file = "imagetest/123/".$_FILES['file']['name'];
$source_file = $_FILES['file']['tmp_name'];  

// set up basic connection
$conn_id = ftp_connect($ftp_server);
ftp_pasv($conn_id, true); 

// 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); 

// 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);
?>
4
  • $destination_file specify your directory properly... and make sure you have the directory in your root folder... according to your code the directory is dynamic as you had appended the file name to directory path... Oct 13, 2014 at 4:41
  • Warning: ftp_put(): Could not create file. in /homeb/home/XXXXXXXXXX/www/upload.php @Fred
    – Redsun
    Oct 13, 2014 at 4:54
  • remove .$_FILES['file']['name']; at end of $destination_file = "imagetest/123/".$_FILES['file']['name']; and try... i think the problem is with directory... Oct 13, 2014 at 4:58
  • @Redsun See my answer below. I too had a hard time, but figured out why it wasn't working. We seem to have the same problem, which I hope my answer will work for you also. Oct 13, 2014 at 5:12

1 Answer 1

2

I've tested your code and had a bit of a hard time to get it working, but what worked for me was to use something like /http_docs, or /public_html as the base/root folder.

  • Root folder names will vary from hosting services, so modify accordingly.

I.e. and with a few modifications:

<?php
$ftp_server = "XXXXXX"; 
$ftp_user_name = "XXXXXXX"; 
$ftp_user_pass = "XXXXXXXX"; 

$folder = "/http_docs/imagetest/123/";

$destination_file = $folder . $_FILES['file']['name'];

$source_file = $_FILES['file']['tmp_name'];

// rest of code

Sidenote:

Do not use a full path name.

I.e.: /var/user/you/public_html/ it won't work.

0

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.