PHP File Upload to FTP (Here Using Uploadify as FTP) -- Unable to perform upload

359 Views Asked by At
Here is my HTML Code :

<html>
<head>
<title>Welcome</title>
</head>

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

And Below is PHP :

<?php
$ftp_server = "94.xx.1.xxx";
$ftp_username   = "anxxxxxx";
$ftp_password   =  "xxxxxxxxx";

$conn_id = ftp_connect($ftp_server) or die("could not connect to $ftp_server");

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

$file = $_FILES["uploadedfile_1"]["name"];
$file2 = $_FILES["uploadedfile_2"]["name"];

$remote_file_path = "[email protected]/JustForTest".$file; // This is the Folder which I've created inside the FTP 
$remote_file_path2 = "[email protected]/JustForTest".$file2; // This is the Folder which I've created inside the FTP 

ftp_put($conn_id, $remote_file_path, $_FILES["uploadedfile_1"]["tmp_name"],FTP_ASCII);
ftp_put($conn_id, $remote_file_path2, $_FILES["uploadedfile_2"]["tmp_name"],FTP_ASCII);
ftp_close($conn_id);
echo "\n\nconnection closed";
?>

Error :

connected as [email protected] Fatal error: Uncaught ValueError: Path cannot be empty in C:\xampp\htdocs\upload.php:22 Stack trace: #0 C:\xampp\htdocs\upload.php(22): ftp_put(Object(FTP\Connection), '[email protected]...', '', 1) #1 {main} thrown in C:\xampp\htdocs\upload.php on line 22

It connects perfectly...but no files gets uploaded, throws the above error. I am new to php. PLEASE HELP...!

I would prefer to share the code, if anyone had done such kind of requirement.

Thanks a ton in Advance...!`

1

There are 1 best solutions below

7
Selim Acar On

Try following code. Please redefine your upload directory in the code. I wrote a comment to your question, that you should use safe directory names. I would not use @ and . in directory names. Especially no dots.

<?php

// Build FTP connection and login
function getFtpConnection()
{
    $ftp_server = "94.xx.1.xxx";
    $ftp_username   = "anxxxxxx";
    $ftp_password   =  "xxxxxxxxx";
    
    $conn_id = ftp_connect($ftp_server) or die("could not connect to $ftp_server");
    
    if(@ftp_login($conn_id, $ftp_username, $ftp_password))
    {
      echo "connected as $ftp_username@$ftp_server\n";
      return $conn_id;
    }
    else {
      echo "could not connect as $ftp_username\n";
      return false;
    }
}

$messages = [];
if(gettype($conn) == "resource")
{
    $remoteDirectory = "/html/myuploadDir/";//Absoulte path
    foreach($_FILES as $k => $upload_file)
    {
        if(!empty($upload_file["name"]) && filesize($upload_file["tmp_name"]) > 0)
        {
            $fname = $upload_file["name"];
            $remote_filepath = $remoteDirectory . $fname;
            if(ftp_put($conn, $remote_filepath, $upload_file["tmp_name"]))
                $messages[] = "File $fname uploaded successfully";
            else
                $messages[] = "Upload for file $fname failed";
        }
    }
    ftp_close($conn);
    $messages[] = "Connection closed";
}
echo implode("<br>", $messages);