У меня есть этот скрипт для загрузки нескольких изображений.Если я загружаю в одно и то же время один и тот же тип изображений, например, все jpg, gif или png, это работает хорошо, но если я загружаю смесь типов изображений (смесь jpg, gif, png), в БД яесть все изображения с одинаковыми расширениями.В foreach первое загруженное изображение меняет расширения на другие.Это происходит только для БД, потому что класс class.upload.php правильно загружает изображения в папку.
Есть идеи?Спасибо
if($_FILES){
try{
$files = array();
foreach ($_FILES['group'] as $k => $l) {
foreach ($l as $i => $v) {
if (!array_key_exists($i, $files))
$files[$i] = array();
$files[$i][$k] = $v;
}
}
foreach ($files as $file) {
$query = "INSERT INTO gallery (img, alt_image, created) VALUES (:image, :alt, :created)";
// prepare query for execution
$stmt = $con->prepare($query);
$name = $_FILES['group']['name'][0];
$ext = pathinfo($name, PATHINFO_EXTENSION);
$rand = md5(uniqid().rand());
$post_image = $rand.".".strtolower($ext);
$withoutExt = preg_replace('/\\.[^.\\s]{3,4}$/', '', $post_image);
$handle = new upload($file);
if ($handle->uploaded) {
$handle->file_new_name_body = strtolower($withoutExt);
$handle->image_resize = true;
$handle->image_ratio_crop = true;
$handle->image_x = 720;
$handle->image_y = 630;
$handle->process('../../images/gallery/');
if ($handle->processed) {
$handle->clean();
} else {
echo 'Error: ' . $handle->error;
}
}
unset($handle);
// bind the parameters
$stmt->bindParam(':image', $post_image);
$stmt->bindParam(':alt', $name);
$created=date('Y-m-d H:i:s'); // specify when this record was inserted to the database
$stmt->bindParam(':created', $created);
// Execute the query
if($stmt->execute()){
echo "<div class='alert alert-success'>Success.</div>";
}else{
echo "<div class='alert alert-danger'>Error.</div>";
}
}
}
// show error
catch(PDOException $exception){
die('ERROR: ' . $exception->getMessage());
}
}
?>