php多个文件上传有两个输入

时间:2016-02-13 13:03:06

标签: php html

我正在尝试使用图片和文件上传文件。 我一直试图修复它多年,并没有设法让它工作。 (我是新手) PHP:

$target_dir1 = "/Files/images";
$target_dir2 = "/Files/files";
$target_file1 = $target_dir1 . basename($_FILES["file"]["name"][0]);
$target_file2 = $target_dir2 . basename($_FILES["file"]["name"][1]);
$uploadOk = 1;
$fileType1 = pathinfo($target_file1,PATHINFO_EXTENSION);
$fileType2 = pathinfo($target_file2,PATHINFO_EXTENSION);
// Check if files already exists
if (file_exists($target_file1)) {
    $msg .= "<script>document.write( \"<div class='alert alert-info alert-css'><a class='close' data-dismiss='alert' aria-label='close'>&times;</a><strong>Info!</strong> Sorry, file already exists.</div>\");</script>";
    $uploadOk = 0;
}
if (file_exists($target_file2)) {
    $msg .= "<script>document.write( \"<div class='alert alert-info alert-css'><a class='close' data-dismiss='alert' aria-label='close'>&times;</a><strong>Info!</strong> Sorry, file already exists.</div>\");</script>";
    $uploadOk = 0;
}
// Check files size
if ($_FILES["file"]["size"][0] > 1500000) {
    $msg .= "<script>document.write( \"<div class='alert alert-info alert-css'><a class='close' data-dismiss='alert' aria-label='close'>&times;</a><strong>Info!</strong> Sorry, your file is too large.</div>\");</script>";
    $uploadOk= 0;
}
if ($_FILES["file"]["size"][1] > 1500000) {
    $msg .= "<script>document.write( \"<div class='alert alert-info alert-css'><a class='close' data-dismiss='alert' aria-label='close'>&times;</a><strong>Info!</strong> Sorry, your file is too large.</div>\");</script>";
    $uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
    echo "error";
} else {
    if (move_uploaded_file($_FILES["file"]["tmp_name"][0], $target_file1)) {
        $msg .= "<script>document.write( \"<div class='alert alert-info alert-css'><a class='close' data-dismiss='alert' aria-label='close'>&times;</a><strong>Info!</strong> The file ". basename( $_FILES["file"]["name"][0]). " has been uploaded.</div>\");</script>";
    }
}
if ($uploadOk == 0) {
    echo "error1";
} else {
    if (move_uploaded_file($_FILES["file"]["tmp_name"][1], $target_file2)) {
        $msg .= "<script>document.write( \"<div class='alert alert-info alert-css'><a class='close' data-dismiss='alert' aria-label='close'>&times;</a><strong>Info!</strong> The file ". basename( $_FILES["file"]["name"][1]). " has been uploaded.</div>\");</script>";
    }

}

HTML:

    <form class="col-lg-3" role="form" method='post' action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]."?page=Upload");?>" enctype="multipart/form-data">
  <div class="form-group">
    <label for="image">Image</label>
    <input type="file" name="file[]" id="image">
  </div>
  <div class="form-group">
    <label for="file">File</label>
    <input type="file" name="file[]" id="file">
  </div>
  </form>

由于某种原因它不起作用。 我想要它做的是将两个文件上传到目录。

更新

我后来认为我只需删除$ target_dir前面的斜杠然后就可以了。

1 个答案:

答案 0 :(得分:0)

首先,你为什么要迷惑自己。对于image,请将name保留为image[]。并且,在下面进行必要的更改。

<form class="col-lg-3" role="form" method='post' action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]."?page=Upload");?>" enctype="multipart/form-data">
  <div class="form-group">
    <label for="image">Image</label>
    <input type="file" name="image[]" id="image">
  </div>
  <div class="form-group">
    <label for="file">File</label>
    <input type="file" name="file[]" id="file">
  </div>
</form>

<?php

$target_dir1 = "/Files/images";
$target_dir2 = "/Files/files";
$target_file1 = $target_dir1 . basename($_FILES["image"]["name"]);
$target_file2 = $target_dir2 . basename($_FILES["file"]["name"]);
$uploadOkImage = 1;
$uploadOkFile = 1;
$fileType1 = pathinfo($target_file1,PATHINFO_EXTENSION);
$fileType2 = pathinfo($target_file2,PATHINFO_EXTENSION);

// Check if files already exists
if (file_exists($target_file1)) {
    $msg .= "<script>document.write( \"<div class='alert alert-info alert-css'><a class='close' data-dismiss='alert' aria-label='close'>&times;</a><strong>Info!</strong> Sorry, file already exists.</div>\");</script>";
    $uploadOkImage = 0;
}
if (file_exists($target_file2)) {
    $msg .= "<script>document.write( \"<div class='alert alert-info alert-css'><a class='close' data-dismiss='alert' aria-label='close'>&times;</a><strong>Info!</strong> Sorry, file already exists.</div>\");</script>";
    $uploadOkFile = 0;
}

// Check files size
if ($_FILES["image"]["size"] > 1500000) {
    $msg .= "<script>document.write( \"<div class='alert alert-info alert-css'><a class='close' data-dismiss='alert' aria-label='close'>&times;</a><strong>Info!</strong> Sorry, your file is too large.</div>\");</script>";
    $uploadOkImage= 0;
}
if ($_FILES["file"]["size"] > 1500000) {
    $msg .= "<script>document.write( \"<div class='alert alert-info alert-css'><a class='close' data-dismiss='alert' aria-label='close'>&times;</a><strong>Info!</strong> Sorry, your file is too large.</div>\");</script>";
    $uploadOkFile = 0;
}

// Check if $uploadOk is set to 0 by an error
if ($uploadOkImage == 0) {
    echo "error";
} else {
    if (move_uploaded_file($_FILES["image"]["tmp_name"], $target_file1)) {
        $msg .= "<script>document.write( \"<div class='alert alert-info alert-css'><a class='close' data-dismiss='alert' aria-label='close'>&times;</a><strong>Info!</strong> The file ". basename( $_FILES["image"]["name"]). " has been uploaded.</div>\");</script>";
    }
}

if ($uploadOkFile == 0) {
    echo "error1";
} else {
    if (move_uploaded_file($_FILES["file"]["tmp_name"], $target_file2)) {
        $msg .= "<script>document.write( \"<div class='alert alert-info alert-css'><a class='close' data-dismiss='alert' aria-label='close'>&times;</a><strong>Info!</strong> The file ". basename( $_FILES["file"]["name"]). " has been uploaded.</div>\");</script>";
    }
}