`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 = "ansxxxx@94.xx.1.xxx/JustForTest".$file; // This is the Folder which I've created inside the FTP 
$remote_file_path2 = "ansxxxx@94.xx.1.xxx/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 anshu9453@94.23.1.139
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), 'anshu9453@94.23...', '', 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...!`

Dani AI

Generated

The error you saw — "ValueError: Path cannot be empty" — means ftp_put was given an empty local path. In your stack trace the third argument is '' so PHP never created a tmp file for the upload (or you referenced the wrong field). Common causes: the upload hit a size limit (MAX_FILE_SIZE or php.ini settings), the file input name doesn't match, or $_FILES contains an error code.

Short troubleshooting and fixes (try these in order):

  • Inspect what PHP actually received: print_r($_FILES); and check $_FILES['uploadedfile_1']['error'] and ['tmp_name'].
  • Remove or increase the <input name="MAX_FILE_SIZE"> while testing — that field is advisory and can make uploads fail silently if the file is bigger than the value. Also verify upload_max_filesize and post_max_size in php.ini.
  • Don’t pass the username@host as the remote file path. ftp_put expects a remote path on the FTP server (for example /JustForTest/filename.ext), not an FTP URL.

Example of the minimal safe flow (simplified):

// validate upload
if (!isset($_FILES['uploadedfile_1']) || $_FILES['uploadedfile_1']['error'] !== UPLOAD_ERR_OK) {
    var_dump($_FILES['uploadedfile_1']['error']); exit;
}
$local = $_FILES['uploadedfile_1']['tmp_name'];
$remote = '/JustForTest/' . basename($_FILES['uploadedfile_1']['name']);

ftp_pasv($conn, true);
$ok = ftp_put($conn, $remote, $local, FTP_BINARY);
if (!$ok) { /* check remote dir exists, perms, use ftp_nlist/ftp_chdir/ftp_mkdir */ }

Also heed ’s point: if you only need the file on the web server, use move_uploaded_file() instead of FTP. If you must move to a separate host, check remote folder permissions, test with a very small file, and consider SFTP (ssh2) for security. If these checks still fail, paste the output of print_r($_FILES) and the exact php.ini upload limits for further diagnosis.

Recommended Answers

All 2 Replies

What exactly does upload.php look like? It looks like there is an error on line 22 of upload.php. It looks as if the path is invalid in your ftp_put() function. Check your values for $remote_file_path and $remote_file_path2.

Also, this is a very, very insecure way of doing file uploads. Why are you doing it this way instead of as a POST forum with PHP uploads?

Non logic operation to upload file. file transfer protocol(ftp) can be operated after connecting two or more devices to obtain ftp IP to perform a specific task of transferring a file from one device to another. eg ftp://10.165.1:2541

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.