问题描述
使用PHP完成我的FTP文件上传器时遇到了一些麻烦。我使用php.net中的示例在此处找到:并稍微修改了代码。
name 可能不会在发送文件时发生错误。这会导致您尝试上传到 members / userUploads / ,这会导致 ftp_upload 正确地抱怨空文件名。
错误的常见原因是超过了允许的最大文件大小。至少,在尝试FTP上传之前,检查文件的 $ _ FILES 条目中的错误: p>
if($ _FILES ['userfile'] ['error']!= UPLOAD_ERR_OK){
//处理错误而不是上传,例如给用户一个消息
}
你可以找到。
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:
解决方案The name might not be set if there was an error when sending the file. This would cause you to try to upload to members/userUploads/, which would cause ftp_upload to rightly complain about an empty filename.
A common reason for the error is exceeding the maximum allowed filesize. At the very least, check the error in the $_FILES entry for your file before attempting the FTP upload:
if ($_FILES['userfile']['error'] != UPLOAD_ERR_OK) { // handle the error instead of uploading, e.g. give a message to the user }You can find the description of the possible error codes in the PHP manual.
这篇关于PHP FTP上传错误:警告:ftp_put()[function.ftp-put]:文件名不能为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!