0

我正在尝试上传 3 个单独的图像以上传到服务器上使用。该代码不会引发任何错误,但只会上传第一张图片。其他两个图像没有上传到服务器上,但它的名称被插入到数据库中。

这是我上传图片的代码:


    <?php
include("common_code.php");
include("database_connection.php");

$property_id = $_POST['id'];
if (!$_FILES['file1']['size'] == 0) {
    $temp = explode(".", $_FILES["file1"]["name"]);
    $extension = end($temp);
    $fileName1= mt_rand(). "_". time(). "." .$extension;
    $image_tmp_name = $_FILES['file1']['tmp_name'];
    $folder = "images/property_images/";
    $folder = $folder . $fileName1;
    move_uploaded_file($image_tmp_name, $folder);
    $uploadQuery = "INSERT INTO property_photos (filenames,property_id) VALUES ('$folder','$property_id')";
    $run = mysqli_query($connection, $uploadQuery);
}

if (!$_FILES['file2']['size'] == 0) {
    $temp2 = explode(".", $_FILES["file2"]["name"]);
    $extension2 = end($temp2);
    $fileName2= mt_rand(). "_". time(). "." .$extension2;
    $image_tmp_name = $_FILES['file2']['tmp_name'];
    $folder2 = "images/property_images/";
    $folder2 = $folder2 . $fileName2;
    move_uploaded_file($image_tmp_name2, $folder2);
    $uploadQuery2 = "INSERT INTO property_photos (filenames,property_id) VALUES ('$folder2','$property_id')";
    $run2 = mysqli_query($connection, $uploadQuery2);
}

if (!$_FILES['file3']['size'] == 0) {
    $temp3 = explode(".", $_FILES["file3"]["name"]);
    $extension3 = end($temp3);
    $fileName3= mt_rand(). "_". time(). "." .$extension3;
    $image_tmp_name = $_FILES['file3']['tmp_name'];
    $folder3 = "images/property_images/";
    $folder3 = $folder3 . $fileName3;
    move_uploaded_file($image_tmp_name3, $folder3);
    $uploadQuery3 = "INSERT INTO property_photos (filenames,property_id) VALUES ('$folder3','$property_id')";
    $run3 = mysqli_query($connection, $uploadQuery3);
}

header("Location:property_reg_successful.php?id=$property_id");

只有file1被上传到服务器!文件 2 和文件 3 未上传。

4

1 回答 1

0

您刚刚遇到了一个简单的变量命名错误:

$image_tmp_name = $_FILES['file2']['tmp_name'];

[...]

move_uploaded_file($image_tmp_name2, $folder2);

请注意不同的变量名称“$image_tmp_name”和“$image_tmp_name2”。

于 2021-05-16T15:48:48.663 回答