我在使用PHP完成我的FTP文件上傳器時遇到了一些麻煩。我使用php.net中的示例在這裏找到:http://php.net/manual/en/ftp.examples-basic.php並稍微修改了代碼。PHP FTP上傳錯誤:警告:ftp_put()[function.ftp-put]:文件名不能爲空
<?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);
?>
然而,代碼將一些文件,但不是別人的工作。當它不工作,它給人的錯誤:
Warning: ftp_put() [function.ftp-put]: Filename cannot be empty in ... on line 48.
什麼文件這不起作用?檢查$ _FILES ['userfile'] ['name']','$ _FILES ['userfile'] ['tmp_name']'總是有值。 – pankar