上传多个上传文件时遇到问题

时间:2012-11-11 12:12:20

标签: php upload

所以我有一个脚本允许文件立即上传:

<input style = "width: 90px;" name="book-image" type="file" id="image" value = "Upload">
<input style = "width: 90px;" name="book-preview" type="file" id="book" value = "Upload">

这是接收脚本:如果图像存在,它会测试预览(pdf)是否存在,然后运行2个函数来上传每个图像和pdf。

if(file_exists($_FILES['book-image']['tmp_name']) || is_uploaded_file($_FILES['book-image']['tmp_name'])) {
        //If there is a preview or non-free book
        if((file_exists($_FILES['book-preview']['tmp_name']) || is_uploaded_file($_FILES['book-preview']['tmp_name']))) {
            //valid - upload image, preview and add book 
            if($image = uploadBookImage() && $pdf = uploadPreviewPdf()) {
                $newValues['imagefilename'] = $image;
                $newValues['previewfile'] = $pdf;
                if($qc->insertBook($newValues)) {
                    $message = "Added Book!";
                } else{
                    $error .= "Couldn't add book";
                }
            }
        }
} 

$ QC-&GT; insertBook($值);将值添加到数据库。

以下是上传功能:

function uploadBookImage() {
$allowedExts = array("jpg", "jpeg", "gif", "png");
$gay = explode(".", $_FILES['book-image']['name']);
$extension = end($gay);
$target_path = "../images/books/";
$filename = $_FILES["book-image"]["name"];
$target_path = $target_path . basename($_FILES['book-image']['name']); 
if ((($_FILES["book-image"]["type"] == "image/gif")
|| ($_FILES["book-image"]["type"] == "image/jpeg")
|| ($_FILES["book-image"]["type"] == "image/png")
|| ($_FILES["book-image"]["type"] == "image/pjpeg"))
&& ($_FILES["book-image"]["size"] < 2000000)
&& in_array($extension, $allowedExts)){
    if (file_exists($target_path)) {
        return false;
    } else {
        if(move_uploaded_file($_FILES['book-image']['tmp_name'], $target_path)) {
            echo $filename;
            return $filename;
        }
    }
} else {
    return false;
}
}//function

function uploadPreviewPdf() {
     $allowedExts = array("pdf");
     $gay = explode(".", $_FILES['book-preview']['name']);
     $extension = end($gay);
     $target_path = '../previews/';
     $filename = $_FILES['book-preview']['name'];
     $target_path = $target_path . basename($_FILES['book-preview']['name']);   
     if((($_FILES['book-preview']['type'] == 'application/pdf'))
         && ($_FILES['book-preview']['size'] < 6000000)
         && in_array($extension, $allowedExts)) {
         if(file_exists($target_path)) {
           return false;
         } else {
        if(move_uploaded_file($_FILES['book-preview']['tmp_name'],    $target_path)) {
            return $filename;
        }
    }
} else {
    return false;
}
}

问题:当我上传两者时,它会将图像名称的值1和pdf的文件名添加到数据库中。但是,如果我只上传图像,则会将图像名称添加到数据库中

上传图片时var转储的输出:输出图像文件名和 上传图像和pdf时输出var转储:输出图像文件名

我不知道为什么。请帮忙。

1 个答案:

答案 0 :(得分:0)

我明白了:

是:

if($image = uploadBookImage() && $pdf = uploadPreviewPdf()) {
     $newValues['imagefilename'] = $image;
     $newValues['previewfile'] = $pdf;
}

需要

if($image = uploadBookImage()) {
    $newValues['imagefilename'] = $image;
    if($pdf = uploadPreviewPdf()){         
         $newValues['previewfile'] = $pdf;
    }
}

不确定为什么。有谁能解释为什么?我唯一的猜测就是它没有时间这么快完成两项检查?