【问题标题】:Trouble with uploading multiple uploaded files上传多个上传文件的问题
【发布时间】:2012-11-11 12:12:19
【问题描述】:

所以我有一个脚本可以让文件一次上传:

<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->insertBook($values);将值添加到数据库中。

上传功能如下:

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 dump的输出:输出图片文件名和 上传图片和pdf时var dump的输出:输出图片文件名

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

【问题讨论】:

  • 在这两种情况下var_dump($_FILES) 是什么?
  • 现在检查。敬请关注。绝望的大声笑。
  • 是的,调试很糟糕。只需稍事休息,用新的眼光和逐步的故障排除方法,通常很快就可以了。
  • 好的,这是测试结果!仅上传图像: var dump 仅返回图像名称(如我所料)。上传图像和 pdf,var dump 再次只返回图像。但是我查看了两个上传目录,两个文件都在那里。很奇怪吧。
  • 您应该将 var_dumps 的输出添加到您的问题中。否则很难说这是否很奇怪;)

标签: php upload


【解决方案1】:

我想通了:

原来是:

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

应该是

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

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

【讨论】:

    猜你喜欢
    • 2013-07-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-30
    • 2012-05-13
    • 2017-09-29
    • 1970-01-01
    • 2013-03-26
    相关资源
    最近更新 更多