【发布时间】:2014-09-12 00:59:48
【问题描述】:
我使用以下代码将多个图像存储到数据库中,一切正常存储和显示上传我需要的是,当我一次上传 3 张照片时,为单次上传创建了三行我如何将多个图像的路径存储在一个单排
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>
我用这段代码在sql中创建表
CREATE TABLE IF NOT EXISTS `test` (
`path` text(50) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
【问题讨论】:
-
为什么要它们都在同一行?
-
为什么要将多个图像的路径存储在一行中?这似乎不是一个好主意。最好将每条路径分别存储为自己的记录。
-
是的,所有图像路径都存储在单行中
-
如果你应该在一行中存储多个值,也许你应该重新考虑你的数据库架构。
-
如果我存储在多行中,我无法在单个 id 中检索多个图像
标签: php sql database image upload