I have a fairly simple website where I am intending to allow the user to upload an image file to an FTP server. Here is the HTML/PHP code:
<form action="success.php" method="post" enctype="multipart/form-data">
<div id="image-box" class="input-item">
<label for="img">Select image:</label>
<input type="file" id="img" name="img" accept="image/*">
</div>
</form>
and here is the relevant PHP in success.php:
$ftpHost = "hostsite.com";
$ftpUsername = "username";
$ftpPassword = "password";
$ftpDir = "/host-directory/Orders/images/";
$srcFile = $_FILES["img"]["name"];
$dstFile = $ftpDir . $srcFile;
$ftpcon = ftp_connect($ftpHost) or die('Error connecting to ftp server...');
$ftplogin = ftp_login($ftpcon, $ftpUsername, $ftpPassword);
ftp_pasv($ftpcon, true) or die("Unable to switch to passive mode");
if ((!$ftpcon) || (!$ftplogin)) {
echo "FTP connection has failed!<br>";
exit;
} else {
echo "Connected successfully<br>";
}
if (ftp_put($ftpcon, $dstFile, $srcFile, FTP_ASCII))
echo $dstFile . ' uploaded successfully to FTP server!';
else
echo 'Error uploading ' . $dstFile . '<br>Please try again later.';
ftp_close($ftpcon);
This results in the following output:
Connected successfully
Error uploading /host-directory/Orders/images/image.png
Please try again later.
Clearly ftp_put is returning false, but I can't seem to figure out why.
Something to note is that if I use the following fpt_put command:
ftp_put($ftpcon, $dstFile, $_FILES["img"]["tmp_name"], FTP_ASCII)
Then the file actually uploads successfully to the FTP (and ftp_put() returns true), however it uploads essentially a blank file with size of 1KB. The files I have tried to upload are around 300-400KB (Currently all files fail and I have tried many types).
I have confirmed that my php ini allows file uploads and has a max file upload size of 5MB.
I can't confirm this but based on how $_FILES["img"]["tmp_name"] at least uploads, I believe $_FILES["img"]["name"] is returning some file location that does not actually exist. Is this possible? Note that when I echo $dstFile it is correctly outputting $_FILES["img"]["name"] as 'image.png'.
Any help would be greatly appreciated.