1

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

Mujahid Bhoraniya
  • 1,518
  • 10
  • 22
  • Did you check [Why would $_FILES be empty when uploading files to PHP?](https://stackoverflow.com/q/3586919/850848) – Martin Prikryl Feb 27 '20 at 14:19
  • No I have not. I will read it now. However, I feel that I am limited and have to do more work on this project since I am working in Wordpress (not my personal choice haha) –  Feb 27 '20 at 14:23
  • have you tried it without unlink() – Ess Kay Feb 27 '20 at 14:32
  • No, I will delete that line and give you the results. Though it should not even give this error. Since I have not uploaded a file yet. And when I add a if(isset($_POST["button"])) around the code I get no results at all. –  Feb 27 '20 at 14:33
  • i think its an issue with $workDir try using / – Ess Kay Feb 27 '20 at 14:37
  • Neither `unlink` nor `$workDir` are not the primary problem. The primary problem is that the uploaded file(s) do not even make it to the script. And as the script does not do any error checking, it will make it to `unlink` with empty `$tmpName`. – Though the `$workDir` is possibly wrong too, as I have suspicion that OP tries to delete a file on the user system (`$workDir`), what is obviously not possible. But that's secondary problem. – Martin Prikryl Feb 27 '20 at 14:38
  • this article deals with locations for the uploads https://www.geeksforgeeks.org/how-to-move-a-file-into-a-different-folder-on-the-server-using-php/ – Ess Kay Feb 27 '20 at 14:40
  • hi @MartinPrikryl. You mentioned the uploaded files. The problem is that I have not uploaded files. These errors pop up as soon as I run the code. –  Feb 27 '20 at 14:49
  • How do you *"run the code"*? You do not run your PHP code. You should open the HTML page in your browser and submit the (HTML) form. – Martin Prikryl Feb 27 '20 at 14:49
  • In the plugn (PHPCode Snippets) I change the code and update it. I get a new shortcode after the update. I paste the short code in the page and the code is "run" then. I work in Wordpress. No form is shown. –  Feb 27 '20 at 14:52
  • Also the code is based on this article: https://www.techrepublic.com/article/keep-your-options-open-with-ftp-file-uploads-using-php/ –  Feb 27 '20 at 14:57
  • That sounds very wrong. I do not think this can ever work. I'm afraid that you are on a wrong track. Btw, I'm sure there's wordpress plugin that does what you need out of the box. – Martin Prikryl Feb 27 '20 at 14:57
  • How would you approach this @MartinPrikryl? Do you know any plugins that allow me to write php code? I do not know any good ones. –  Feb 27 '20 at 14:59
  • I suggest you go to [wordpress.se]. – Martin Prikryl Feb 27 '20 at 15:00
  • My goal is to create a form that uploads a files of users to a remote ftp server. –  Feb 27 '20 at 15:01
  • Does anyone have another idea on how I can solve this. –  Feb 27 '20 at 15:38
  • @MartinPrikryl this Q does not need WordPress knowledge to be answered, and doesn't make any use of WordPress APIs. It can be answered using pure PHP knowledge. The OP asked the Q on WPSE and it was closed as offtopic – Tom J Nowell Feb 27 '20 at 15:53
  • @MartinPrikryl this was not a Wordpress related question –  Feb 27 '20 at 16:04
  • @Parsa_237 if you're ever asking for help with WP and using the PHP Snippets plugin, make sure you mention it clearly, otherwise answers you get are unlikely to work, or won't behave the way they're supposed to – Tom J Nowell Feb 27 '20 at 16:08
  • Thanks Tom. I will look out for that in the future. –  Feb 27 '20 at 16:11
  • The problem is that this is WordPress related question, but you write the code as if it were not. – Martin Prikryl Feb 28 '20 at 06:55

1 Answers1

0

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

Tom J Nowell
  • 9,588
  • 17
  • 63
  • 91
  • I will look into the add_shortcode. Just copy paste this code? Thanks for the answer. –  Feb 27 '20 at 16:09
  • That if statement should go somewhere near the top of the PHP code – Tom J Nowell Feb 27 '20 at 16:12
  • My upload form is not shown at all. I want it to return "hello" yet it does not. Here is the link to my website: https://radioprogrammabank.nl/wp/upload-album/. As you can see. No upload form is shown. –  Feb 27 '20 at 16:14
  • did you put it at the top of the PHP code as I said? or did you put it in the section you described as HTML in your question? That page never finishes rendering so there's a PHP fatal error happening, you will need the error message. Note that there's lots of other issues with your upload form other than the issue you asked about here, for example, the form tag references a file that doesn't exist, and there are other issues resulting from taking code designed for one context and putting it somewhere completely different. My answer only addresses the question you asked, it won't fix everything – Tom J Nowell Feb 27 '20 at 16:45
  • Sorry for the late response Tom. I think I will just start over again with other code. My code has too much faults. I will accept your answer to myy question since it did help me out with my first error. –  Feb 28 '20 at 08:14