Ad

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:

<?php


//FTP variabelen met de values

$host = "radioprogrammabank.nl";

$user = "***";  

$pass = "***";

//location I want to send the uploaded file to (it is remote)
$destDir = "/domains/radioprogrammabank.nl/public_html/wp/wp-content/uploads";


$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

ftp_close($conn);

// 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:


<html>
<body>
<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" />
</form>

</body>
[xyz-ips snippet="verbindftp"]
</html>

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.

Greetings,

Parsa_237

Ad

Answer

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.

e.g.

if ( empty( $_FILES['file'] ) ) {
    return;
}

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

Ad
source: stackoverflow.com
Ad