I'm having some trouble finishing my FTP file uploader using PHP. I'm using the example from php.net found here: http://php.net/manual/en/ftp.examples-basic.php and have modified the code slightly.
<?php
//Start session();
session_start();
// Checking the users logged in
require_once('config.php');
//Connect to mysql server
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if(!$link) {
die('Failed to connect to server: ' . mysql_error());
}
//Select database
$db = mysql_select_db(DB_DATABASE);
if(!$db) {
die("Unable to select database");
}
$ftp_server="*";
$ftp_user_name="*";
$ftp_user_pass="*";
$paths="members/userUploads";
$name=$_FILES['userfile']['name'];
$source_file=$_FILES['userfile']['tmp_name'];
// set up basic connection
$conn_id = ftp_connect($ftp_server);
// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
// check connection
if ((!$conn_id) || (!$login_result)) {
echo "FTP connection has failed!";
echo "Attempted to connect to $ftp_server for user $ftp_user_name";
exit;
} else {
echo "Connected to $ftp_server, for user $ftp_user_name";
}
// upload the file
$upload = ftp_put($conn_id, $paths.'/'.$name, $source_file, FTP_BINARY);
// check upload status
if (!$upload) {
echo "FTP upload has failed!";
} else {
$CurrentUser = $_SESSION['CurrentUser'];
$qry = "SELECT * FROM members WHERE username='$CurrentUser'";
$result = mysql_query($qry);
$result = mysql_fetch_array($result);
$CurrentUser = $result[memberID];
$qry = "INSERT into uploads (UploadPath, UploadUser) VALUES('$file_name', '$CurrentUser')";
echo "Uploaded $source_file to $ftp_server as $paths.'/'.$name";
}
// close the FTP stream
ftp_close($conn_id);
?>
However, the code will work for some files but not others. When it doesn't work it gives the error:
Warning: ftp_put() [function.ftp-put]: Filename cannot be empty in ... on line 48.