Upload File To Remote FTP Server Errors

- 1 answer

Lately, I have been working on an upload form. The idea is that users can upload their files to a remote FTP server. However, it does not work as expected.

Before I even start uploading the file, I get the following error: "Cannot move uploaded file to working directory". Again, I have not yet started uploading a file.

Here is my PHP code:


//FTP variabelen met de values

$host = "";

$user = "***";  

$pass = "***";

//location I want to send the uploaded file to (it is remote)
$destDir = "/domains/";

$dehost = $_POST[$host];

$deuser = $_POST[$user];

$depass = $_POST[$pass];

$dedestDir = $_POST[$destDir];

$workDir = "\Users\stagiaire01\Uploads"; // definieer het lokale systeem

// get temporary file name for the uploaded file

$tmpName = basename($_FILES['file']['tmp_name']);

// copy uploaded file into the current directory

move_uploaded_file($_FILES['file']['tmp_name'], $workDir."/".$tmpName) or die('Cannot move uploaded file to working directory');

// maak connectie, als het niet werkt. Die en geef een melding

$conn = ftp_connect($host) or die ("Cannot initiate connection to host");

// send access parameters

ftp_login($conn, $user, $pass) or die("Cannot login");

// Voer de file upload uit

$upload = ftp_put($conn, $destDir."/".$_FILES['file']['name'], $workDir."/".$tmpName, FTP_BINARY);

// check upload status

// display message

if (!$upload) {

    echo "Upload mislukt";

} else {

    echo "Upload geslaagd";


// sluit de FTP connectie


// verwijder de lokale kopie van het bestand

unlink($workDir."/".$tmpName) or die("Cannot delete uploaded file from working directory -- manual deletion recommended");


My HTML code:

<h2>U kunt hier uw album uploaden</h2>

<form enctype="multipart/form-data" method="post" action="upload.php">
<input type="hidden" name="MAX_FILE_SIZE" value="5000000" />

File <br />
<input type="file" name="file" /><p />

<input type="submit" name="submit" value="Upload Album" />

[xyz-ips snippet="verbindftp"]

You may wonder why I have a shortcode in my HTML. The code is written in Wordpress. I use a plugin in which I can write PHP. The code works when writing this shortcode.

I have also tried doing a var_dump of $_FILES which tells me the following:

"array(0) { } Upload misluktCannot delete uploaded file from working directory -- manual deletion recommended"

I do not know why I get this message when doing a var_dump. I have set my host, username, password, and direction in my values above. The password and username are not shown because of security reasons.

I could not find any answers to this question on StackOverflow. However, I do hope I provided you with enough information to help me out. I expect to be able to upload a file to a remote FTP server.





You need to check if an upload is occurring, otherwise, it will try to connect to the FTP server when the page is loaded, even if the user hasn't started uploading yet.


if ( empty( $_FILES['file'] ) ) {

As a side note, I notice you're using the PHP Snippets plugin, this plugin and others like it are incredibly dangerous. Instead, use add_shortcode in a PHP file to embed snippets of PHP inside of pages.

This is why you get this problem, the original tutorial assumed you would put the PHP code in a PHP file named upload.php, so it would only run when the form got submitted. But that's not the case with this plugin