douyeke2695 2014-07-21 13:37
浏览 50
已采纳

将多个图像存储在单行的SQL数据库中

i used the following code to store multiple images to database every thing works fine storing and displaying uploading what i need is when i upload 3 photo at once three rows are created for single uploading how could i store path of multiple images in a one single row

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';
//
$mysql= new mysqli($mysql_hostname,$mysql_user,$mysql_password,$mysql_database);
foreach($paths as $path){
$query='INSERT INTO `'.$tbl_name.'` (`'.$pathfield_name.'`) VALUES ("'.$mysql->escape_string($path).'");';
$mysql->query($query);}
$mysql->close();
}
?>

<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>

i used this code to create table in sql

CREATE TABLE IF NOT EXISTS `test` (
`path` text(50) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
  • 写回答

1条回答 默认 最新

  • dtja73027 2014-07-21 13:43
    关注

    Fetch path feild first and store in one variable then insert

    foreach($paths as $path){  $pathfield_name.=$pathfield_name."/";   }
    
    
    $query='INSERT INTO `'.$tbl_name.'` (`'.$pathfield_name.'`) VALUES ("'.$mysql->escape_string($path).'");';
    $mysql->query($query);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘