user3150430
user3150430

Reputation: 47

Uploading an image to ftp from HTML input

I'm trying to store images on an FTP server to be used on other pages, but I'm getting multiple errors trying to get this work. Running everything on XAMPP. First I use input on an html page:

<input type="file" name="image" required>

Then I bring it over and try to upload it:

$image = $_POST["image"];
$ftpCon = ftp_connect("127.0.0.1", "21") or die("Could not connect to FTP");
ftp_fput($ftpCon, "image.png", $image, FTP_BINARY);
ftp_close($ftpCon);

With this code I get this error: " ftp_fput() expects parameter 3 to be resource, string given"

Upvotes: 0

Views: 98

Answers (2)

user3121056
user3121056

Reputation: 339

When you upload items (files) via a form it's populated in the $_FILES superglobal.

An associative array of items uploaded to the current script via the HTTP POST method.

https://www.php.net/manual/en/reserved.variables.files.php

Make sure you also set the form with enctype='multipart/form-data'

So the first line of the PHP has to be changed to:

$image = $_FILES['image']['tmp_name'];

The $_FILES is associtaive and contains the following data:

(userfile = image, in your case)

$_FILES['userfile']['name']
The original name of the file on the client machine.
$_FILES['userfile']['type']
The mime type of the file, if the browser provided this information. An example would be "image/gif". This mime type is however not checked on the PHP side and therefore don't take its value for granted.
$_FILES['userfile']['size']
The size, in bytes, of the uploaded file.
$_FILES['userfile']['tmp_name']
The temporary filename of the file in which the uploaded file was stored on the server.
$_FILES['userfile']['error']
The error code associated with this file upload. This element was added in PHP 4.2.0

Upvotes: 0

Thomas Leu
Thomas Leu

Reputation: 855

Change the line

$image = $_POST["image"];

to

$image = $_FILES['image']['tmp_name'];

Upvotes: 1

Related Questions