2
votes

Below code takes file from html form and saves temp file to local temp folder but cannot upload to server. Server will take upload from ftp client, but php code below returns "Cannot upload". Problem with $destDir? Directory exists on server... any help appreciated.

<?php
// set up basic connection
$ftp_server = "ftp.rf.gd";
$ftp_user_name = "rfgd_19026557";
$conn_id = ftp_connect($ftp_server);
$pass="fakepass";
// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $pass);
//test if the connection is successful
echo '<center>';
$conn_id = ftp_connect($ftp_server);
if (!$conn_id)
    echo '<div style="background-color:red;padding:10px;color:#fff;font-size:16px;">
    Couldn\'t connect to <b>' . $ftp_server . '</b></div>';
else
    echo '<div style="background-color:green;padding:10px;color:#fff;font-size:16px;">
    Connected to <b>' . $ftp_server . '</b></div>';
ftp_pasv($conn_id, true);  // turns on passive mode
// upload a file
$destDir = "Business/pics";
$workDir = "C:\Apache24\htdocs\Images"; // define this as per local system
// get temporary file name for the uploaded file
$tmpName = basename($_FILES["fileToUpload"]['tmp_name']);
// copy uploaded file into current directory
move_uploaded_file($_FILES['fileToUpload']['tmp_name'], $workDir."/".$tmpName) or die("Cannot move uploaded file to working directory");

//Code above this point works

// perform file upload
$upload = ftp_put($conn_id, $destDir."/".$_FILES['fileToUpload']['name'], $workDir."/".$tmpName, FTP_BINARY);
// check upload status
// display message
if (!$upload) {
    echo "Cannot upload";
} else {
    echo "Upload complete";
}
// close the connection
ftp_close($conn_id);
// delete local copy of uploaded file
//unlink($workDir."/".$tmpName) or die("Cannot delete uploaded file from working directory — manual deletion recommended");
?>
1
Do you have the permissions to initiate outgoing traffic? - Quasimodo's clone
I am running from browser address localhost/upload.html which asks for file input and calls the below php file. same folder (htdocs). What would I do to check for permission to go out? - Jeff Turcotte
Oops, found my mistake. After connecting, I redefine $conn_id = ftp_connect($ftp_server) again, which essentially disconnects. Sorry for the bother and thanks for looking to help. Jeff - Jeff Turcotte

1 Answers

2
votes
$trackErrors = ini_get('track_errors');
ini_set('track_errors', 1);
if (!@ftp_put($conn_id, $destDir."/".$_FILES['fileToUpload']['name'], $workDir."/".$tmpName, FTP_BINARY)) {
   // error message is now in $php_errormsg
   $msg = $php_errormsg;
   ini_set('track_errors', $trackErrors);
   throw new Exception($msg);
}