简体   繁体   English

如何将多个图像路径存储在一个数组中以将值存储在数据库中的一行中,以便为php上传多个图像

[英]how to store multiple image path in one array to store in database the value in a single row for multiple images uploads for php

how to store multiple image path in one array to store in database the value in a single row for multiple images uploads for php I want to store the paths inside an array and that array will stores different paths in a single row 如何在一个数组中存储多个图像路径以在数据库中为php上传多个图像上传的值存储在一行中,我想将路径存储在数组中,并且该数组将在单个行中存储不同的路径

<html xmlns="http://www.w3.org/1999/xhtml">
    <body>

    <?php

class Upload_Rename
{
    const ALLOWED_TYPES = "jpg,gif,png";
    public static function generate_new_name($extension, $uppercase = true, $prefix = '', $sufix = '')
    {
        $new_name = $prefix . uniqid() . '_' . time() . $sufix;
        return ($uppercase ? strtoupper($new_name) : $new_name) . '.' . $extension;
    }

    public static function check_and_get_extension($file)
    {
        $file_part     = pathinfo($file);
        $allowed_types = explode(",", Upload_Rename::ALLOWED_TYPES);
        if (!in_array($file_part['extension'], $allowed_types)) {
            throw new Exception('Not ok.. bad bad file type.');
        }
        return $file_part['extension'];
    }

    public function upload($file, $target_destination)
    {
        if (!isset($file['tmp_name'])) {
            throw new Exception('Whaaaat?');
        }
        $_name          = $file['name'];
        $_tmp           = $file['tmp_name'];
        $_type          = $file['type'];
        $_size          = $file['size'];
        $file_extension = '';
        try {
            $file_extension = Upload_Rename::check_and_get_extension($_name);
        }
        catch (Exception $e) {
            throw new Exception('Ops.. file extension? what? ' . $e->getMessage());
        }
        $new_name    = Upload_Rename::generate_new_name($file_extension, true, 'whaat_', '_okey');
        $destination = $target_destination . DIRECTORY_SEPARATOR . $new_name;
        move_uploaded_file($_tmp, $destination);
        return $destination;
    }

    public function multiple_files($files, $destination)
    {
        $number_of_files = isset($files['tmp_name']) ? sizeof($files['tmp_name']) : 0;
        $errors          = array();
        $paths           = array();
        for ($i = 0; $i < $number_of_files; $i++) {
            if (isset($files['tmp_name'][$i]) && !empty($files['tmp_name'][$i])) {
                try {
                    $path    = $this->upload(array(
                        'name' => $files['name'][$i],
                        'tmp_name' => $files['tmp_name'][$i],
                        'size' => $files['size'][$i],
                        'type' => $files['type'][$i]
                    ), $destination);
                    $paths[] = $path;
                }
                catch (Exception $e) {
                    array_push($errors, array(
                        'file' => $files['name'][$i],
                        'error' => $e->getMessage()
                    ));
                }
            }
        }
        return $paths;
    }

}

if ($_FILES) {
    $upload      = new Upload_Rename();
    $destination = 'upload';
    $paths       = $upload->multiple_files($_FILES['myfile'], $destination);

    //Fill this with correct information
    $mysql_hostname = "";
    $mysql_user     = "";
    $mysql_password = "";
    $mysql_database = "";
    $tbl_name       = "test";
    $pathfield_name = 'path';
    //
    $con            = mysql_connect("localhost", "root", "");
    myesql_select_db("db1", $con);
    foreach ($paths as $path) {
        $query = 'INSERT INTO `' . $tbl_name . '` (`' . $pathfield_name . '`) VALUES ("' . $mysql->escape_string($path) . '");';
        mysql_query($query);
    }
    $mysql_close($con);
}
?>

    <form  method="post" enctype="multipart/form-data">
    <?php
for ($i = 0; $i < 10; $i++):
?>
   file: <input type="file" name="myfile[]"><br>
    <?php
endfor;
?>
   <input type="submit">
    </form>
    </body>
    </html>
    Hope this one will help you.


if (isset($_FILES['filetoupload']['name'])) {
        $file_name_all = "";
        $randomString = "";

        for ($i = 0; $i < count($_FILES['filetoupload']['name']); $i++) {

            $characters = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
            for ($j = 0; $j < 7; $j++) {
                $randomString .= $characters[rand(0, strlen($characters) - 1)];
            }

            $tmpFilePath = $_FILES['filetoupload']['tmp_name'][$i];
            if ($tmpFilePath != "") {
                $path = "../views/uploads/";
                $name = $_FILES['filetoupload']['name'][$i];
                // $size = $_FILES['filetoupload']['size'][$i];

                list($txt, $ext) = explode(".", $name);
                //$file= time().substr(str_replace(" ", "_", $txt), 0);
                $file = $randomString . substr(str_replace(" ", "_", $txt), 0);
                $info = pathinfo($file);
                $filename = $file . "." . $ext;
                if (move_uploaded_file($_`if (isset($_FILES['filetoupload']['name'])) {
        $file_name_all = "";
        $randomString = "";

        for ($i = 0; $i < count($_FILES['filetoupload']['name']); $i++) {

            $characters = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
            for ($j = 0; $j < 7; $j++) {
                $randomString .= $characters[rand(0, strlen($characters) - 1)];
            }

            $tmpFilePath = $_FILES['filetoupload']['tmp_name'][$i];
            if ($tmpFilePath != "") {
                $path = "../views/uploads/";
                $name = $_FILES['filetoupload']['name'][$i];
                // $size = $_FILES['filetoupload']['size'][$i];

                list($txt, $ext) = explode(".", $name);
                //$file= time().substr(str_replace(" ", "_", $txt), 0);
                $file = $randomString . substr(str_replace(" ", "_", $txt), 0);
                $info = pathinfo($file);
                $filename = $file . "." . $ext;
                if (move_uploaded_file($_FILES['filetoupload']['tmp_name'][$i], $path . $filename)) {
                    $file_name_all.=$filename . "*";
                }
            }
        }

        $finalFileNames = rtrim($file_name_all, '*'); //imagepath if it is present  
        $insertMsgImages = mysql_query("insert into conversation_messages_tbl(postedby_userid,topic_id,message,image,posted_timestamp,upload_img,is_active,member_id) values('$user_id','$topic_id','$message','$finalFileNames','$posted_timestamp','$finalFileNames','1','$member_id')");
    }`FILES['filetoupload']['tmp_name'][$i], $path . $filename)) {
                    $file_name_all.=$filename . "*";
                }
            }
        }

        $finalFileNames = rtrim($file_name_all, '*'); //imagepath if it is present  
        $insertMsgImages = mysql_query("insert into conversation_messages_tbl(postedby_userid,topic_id,message,image,posted_timestamp,upload_img,is_active,member_id) values('$user_id','$topic_id','$message','$finalFileNames','$posted_timestamp','$finalFileNames','1','$member_id')");
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在单行中将多个图像存储到SQL数据库 - store multiple image to SQL database in single row 如何将多个图像上传到一个文件夹并将它们的路径存储到一个数据库列中 - How to upload Multiple Images to a folder and store their path into one database column 使用php将多个值存储在单个数组中 - store multiple value in a single array using php 如何使用 php mysqli 从存储在数据库单列中的图像数组中删除和更新单个图像? - how to delete and update a single image from array of images store in a single column of database using php mysqli? 在多个文件上传(PHP / Mysql)中,图像存储在单列中。 想要将每个图像存储在具有相同ID的单行中 - In multiple files upload(PHP/Mysql), the images are stored in single column. Want to store each image in single row with same id 如何在 php 中的 mysql 数据库中上传/存储多个图像 - How to upload/store multiple images in mysql database in php 如何在 Laravel 数据库中存储多张图片 - How to store multiple images in the database Laravel 如何将多个复选框值存储到一个数据库中 - How to store multiple checkbox value into one database filed 如何在PHP中将多个图像拼接成一个单独的图像? - How to stitch multiple images into one single image in php? 如何从单个文本框中获取多个值并将其存储在数组中以及如何将其存储在数据库中 - how to get multiple values from single text box and store it in array and also store it in database
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM