php 在MySQL数据库表的一列中存储多个图像路径

k97glaaz  于 2023-06-28  发布在  PHP
关注(0)|答案(2)|浏览(99)

我能够上传多个图像到上传文件夹和它的路径到数据库,但我的代码是创建新的行在数据库中的每个图像与新的id(我不想)。我想将所有图像的路径存储到数据库中具有相同ID的单个列中,并用逗号分隔。
下面是我的PHP代码:

<?php
    include('connection.php');
    foreach($_FILES['files']['name'] as $i => $name) {

    $name = $_FILES['files']['name'][$i];
    $size = $_FILES['files']['size'][$i];
    $type = $_FILES['files']['type'][$i];
    $tmp = $_FILES['files']['tmp_name'][$i];

    $explode = explode('.', $name);

    $ext = end($explode);

    $path = 'uploads/';
    $path = $path . basename( $explode[0] . time() .'.'. $ext);

     $sql="INSERT into pictures (FILE_NAME, FILE_SIZE, FILE_TYPE ) 
     VALUES('$name','$size','$type'); ";

    mysqli_query($conn, $sql);
    $errors = array();

    if(empty($_FILES['files']['tmp_name'][$i])) {
        $errors[] = 'Please choose at least 1 file to be uploaded.';
    }else {

        $allowed = array('jpg','jpeg','gif','bmp','png');

        $max_size = 2000000; // 2MB

        if(in_array($ext, $allowed) === false) {
            $errors[] = 'The file <b>'.$name.'</b> extension is not allowed.';
        }

        if($size > $max_size) {
            $errors[] = 'The file <b>'.$name.'</b> size is too hight.';
        }

    }

    if(empty($errors)) {

        if(!file_exists('uploads')) {
            mkdir('uploads', 0777);
        }

        if(move_uploaded_file($tmp, $path)) {
            echo '<p>The file <b>'.$name.'</b> successful upload</p>';
        }else {
            echo 'Something went wrong while uploading 
     <b>'.$name.'</b>';
        }

    }else {
        foreach($errors as $error) {
            echo '<p>'.$error.'<p>';
        }
    }

}

    ?>

数据库截图如下

h7appiyu

h7appiyu1#

只需改进代码,并将插入查询放在foreach循环之外即可

<?php
include('connection.php');
$files = [];
foreach($_FILES['files']['name'] as $i => $name) {

    $name = $_FILES['files']['name'][$i];
    $size = $_FILES['files']['size'][$i];
    $type = $_FILES['files']['type'][$i];
    $tmp = $_FILES['files']['tmp_name'][$i];

    $explode = explode('.', $name);

    $ext = end($explode);

    $updatdName = $explode[0] . time() .'.'. $ext;
    $path = 'uploads/';
    $path = $path . basename( $updatdName );

    if(empty($_FILES['files']['tmp_name'][$i])) {
        $errors[] = 'Please choose at least 1 file to be uploaded.';
    }else {

        $allowed = array('jpg','jpeg','gif','bmp','png');

        $max_size = 2000000; // 2MB

        if(in_array($ext, $allowed) === false) {
            $errors[] = 'The file <b>'.$name.'</b> extension is not allowed.';
        }

        if($size > $max_size) {
            $errors[] = 'The file <b>'.$name.'</b> size is too hight.';
        }

    }

    if(empty($errors)) {

        // if there is no error then set values
        $files['file_name'][] = $updatdName;
        $files['size'][] = $size;
        $files['type'][] = $type;
        $errors = array();
        if(!file_exists('uploads')) {
            mkdir('uploads', 0777);
        }

        if(move_uploaded_file($tmp, $path)) {
            echo '<p>The file <b>'.$name.'</b> successful upload</p>';
        }else {
            echo 'Something went wrong while uploading 
     <b>'.$name.'</b>';
        }

    }else {
        foreach($errors as $error) {
            echo '<p>'.$error.'<p>';
        }
    }

}

if(!empty($files)) {

    $files['file_name'][] = $updatdName;
    $files['size'][] = $size;
    $files['type'][] = $type;
    $names = implode(',', $files['file_name']);
    $sizes = implode(',', $files['size']);
    $types = implode(',', $files['type']);
    $sql="INSERT into pictures (FILE_NAME, FILE_SIZE, FILE_TYPE ) 
         VALUES('$names','$sizes','$types'); ";
    mysqli_query($conn, $sql);
}

我想这会对你有帮助。

ttygqcqt

ttygqcqt2#

// Assuming you have a form with an input field named 'product_images[]' for multiple file uploads

// Get the uploaded files
$uploadedFiles = $_FILES['images'];

// Array to store the uploaded file names
$imageNames = [];

// Loop through each uploaded file
foreach ($uploadedFiles['tmp_name'] as $key => $tmpName) {
    // Generate a unique file name or use the original file name
    $imageName = uniqid() . '_' . $uploadedFiles['name'][$key];

    // Move the uploaded file to a directory
    $destination = 'path/to/your/upload/directory/' . $imageName;
    move_uploaded_file($tmpName, $destination);

    // Add the file name to the array
    $imageNames[] = $imageName;
}

// Save the image names in the database
$commaSeparatedImageNames = implode(',', $imageNames);

// Assuming you have a database connection established
$query = "INSERT INTO products (image_names) VALUES ( '$commaSeparatedImageNames')";
// Execute the query to insert the product details into the database
// Remember to use prepared statements or sanitize the input to prevent SQL injection

相关问题