只是简要介绍/背景信息:
这可以看作part 2 of my question from yesterday,其中@ Fred-ii非常有帮助。我已设法纠正文件路径错误等。
我的问题
如上所述,我成功地将文件上传到服务器(或至少获得成功消息),但是当我检查文件夹中的文件时,它不存在..?事实上,我已经很好地梳理了整个服务器上传的文件,但没有看到上传的文件。
所有文件都有适当的权限,是否有任何可能导致此问题的文件权限除外?
也许我必须在php.ini中改变...但是我再也不会收到上传成功消息..?
过去有没有发生过这种情况?
$file_upload="true";
$file_up_size=$_FILES['file_up']['size'];
echo $_FILES['file_up']['name'];
if ($_FILES['file_up']['size']>250000){
$msg=$msg."Your uploaded file size is more than 250KB
so please reduce the file size and then upload.<BR>";
$file_upload="false";
}
if (!($_FILES['file_up']['type'] =="image/jpeg" OR $_FILES['file_up']['type'] =="image/gif"))
{
$msg=$msg."Your uploaded file must be of JPG or GIF. Other file types are not allowed<BR>";
$file_upload="false";
}
$file_name=$_FILES['file_up']['name'];
if($file_upload=="true"){
$ftp_server = "xxxxxxx";
$ftp_conn = ftp_connect($ftp_server) or die("Could not connect to $ftp_server");
$ftp_username = "xxxxxx";
$ftp_userpass = "xxxxxx";
$login = ftp_login($ftp_conn, $ftp_username, $ftp_userpass);
if($login){
echo '<h1>CONNECTED TO FTP</h1>';
$file = $file_name;
// upload file
if (ftp_put($ftp_conn, $file, 'img/userPics/', FTP_BINARY))
{
echo "Successfully uploaded $file.";
}
else
{
echo "Error uploading $file.";
}
}//if file upload == true
}//if result
// close connection
ftp_close($ftp_conn);
答案 0 :(得分:1)
上传文件或多个文件时,您应该做的第一件事就是检查上传错误。
// Check $_FILES['file_up']['error'] value.
$file_upload=true;
switch ($_FILES['file_up'']['error']) {
case UPLOAD_ERR_INI_SIZE:
echo 'The uploaded file exceeds the upload_max_filesize directive in php.ini.';
$file_upload=false;
break;
case UPLOAD_ERR_FORM_SIZE:
echo 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form.';
$file_upload=false;
break;
case UPLOAD_ERR_PARTIAL
echo 'The uploaded file was only partially uploaded.';
$file_upload=false;
break;
case UPLOAD_ERR_NO_FILE:
echo 'No file was uploaded.';
$file_upload=false;
break;
case UPLOAD_ERR_NO_TMP_DIR:
echo 'Missing a temporary folder.';
$file_upload=false;
break;
case UPLOAD_ERR_CANT_WRITE:
echo 'Failed to write file to disk.';
$file_upload=false;
break;
case UPLOAD_ERR_EXTENSION:
echo 'A PHP extension stopped the file upload.';
$file_upload=false;
break;
}
if ( ! $file_upload ) {
// no file so do whatever is appropriate on upload fail
exit;
}
// then the rest of your code
另请注意,我已将$file_upload="false";
更改为$file_upload=false;
使用实际布尔值=true
和=false
而不是字符串更好,然后您可以使用它来测试它们if ( $file_upload )
要求true
和if ( ! $file_upload )
询问是false
。
希望这能够识别上传过程中实际出现的问题。
现在解决代码中的其他错误!
最明显的是,您从$file_name=$_FILES['file_up']['name'];
获取用户上传的文件的文件名,但这只是名称,而不是上传文件实际存在的位置。
PHP上传过程是一个两阶段过程,浏览器发送文件,PHP将其存储在临时文件中。然后运行代码,验证并检查大小和文件类型等。然后,您应该使用move_file_upload()
现在,由于您实际上并未在系统中存储此文件,因此您可能会忘记此步骤,但这意味着您要从$_FILES['file_up']['tmp'];
所以改变这一行
$file_name=$_FILES['file_up']['name'];
到
$file_name=$_FILES['file_up']['tmp'];
然后至少你将通过FTP连接发送一个真实的文件。