我想在我的数据库中存储2张图片。当我上传2张图片时,两者都成功存储,但在尝试上传单张图片时,则为未上传的图片提供未定义的错误。我哪里错了?
我的代码是:
<label for="certificate">Upload Scaned Document:</label>
<input type="file" id="uploadImage" name="image" />
<label for="certificate">Upload Scaned QR Code</label>
<input type="file" name="QRimage" id="File2" />
php代码是
if((!empty($_FILES["image"])) && ($_FILES['image']['error'] == 0)) {
$imageName = mysql_real_escape_string($_FILES["image"]["name"]);
$imageData = mysql_real_escape_string(file_get_contents($_FILES["image"]["tmp_name"]));
$imageType = mysql_real_escape_string($_FILES["image"]["type"]);
}
if((!empty($_FILES["QRimage"])) && ($_FILES['QRimage']['error'] == 0)) {
$QRimageName = mysql_real_escape_string($_FILES["QRimage"]["name"]);
$QRimageData = mysql_real_escape_string(file_get_contents($_FILES["QRimage"]["tmp_name"]));
}
答案 0 :(得分:1)
为 if 条件添加 isset 检查。因为当您上传两张图片时,$ _FILES获取图片,如果条件有效,则在上传时如果没有在$ _FILES中获取图像,那么图像然后是其他图像所以它的给定错误undefined ...变量应该在任何操作之前设置..
if(isset($_FILES["image"]) && (!empty($_FILES["image"])) && ($_FILES['image']['error'] == 0)) {
if(isset($_FILES["QRimage"]) && (!empty($_FILES[" QRimage "])) && ($_FILES[' QRimage ']['error'] == 0)) {
答案 1 :(得分:1)
使用isset()查看
if((isset($_FILES["image"]["size"]) &&
($_FILES["image"]["size"] > 0))
{
}
if((isset($_FILES["QRimage"]["size"]) &&
($_FILES["QRimage"]["size"] > 0))
{
$QRimageName = mysql_real_escape_string($_FILES["QRimage"]["name"]);
$QRimageData = mysql_real_escape_string(file_get_contents($_FILES["QRimage"]["tmp_name"]));
}
答案 2 :(得分:1)
试试这个
$imageName = "";
$imageData = "";
$QRimageName = "";
$QRimageData = "";
if(!empty($_FILES["image"]["name"])){
$imageName = mysql_real_escape_string($_FILES["image"]["name"]);
$imageData = mysql_real_escape_string(file_get_contents($_FILES["image"]["tmp_name"]));
}
if(!empty($_FILES["QRimage"]["name"])){
$QRimageName = mysql_real_escape_string($_FILES["QRimage"]["name"]);
$QRimageData = mysql_real_escape_string(file_get_contents($_FILES["QRimage"]["tmp_name"]));
}