Php 用于上载多个图像的脚本:第一个图像更改其他图像的扩展名
我有这个脚本上传多个图像。 如果我同时上传相同类型的图像,例如,所有jpg、gif或png,效果会很好,但是如果我上传混合类型的图像(jpg、gif、png的混合),到DB中,我会得到具有相同扩展名的所有图像。在foreach中,加载的第一个图像将扩展更改为其他图像。这种情况仅发生在DB中,因为class.upload.php会将图像正确上传到文件夹中 有什么想法吗?谢谢Php 用于上载多个图像的脚本:第一个图像更改其他图像的扩展名,php,mysql,pdo,Php,Mysql,Pdo,我有这个脚本上传多个图像。 如果我同时上传相同类型的图像,例如,所有jpg、gif或png,效果会很好,但是如果我上传混合类型的图像(jpg、gif、png的混合),到DB中,我会得到具有相同扩展名的所有图像。在foreach中,加载的第一个图像将扩展更改为其他图像。这种情况仅发生在DB中,因为class.upload.php会将图像正确上传到文件夹中 有什么想法吗?谢谢 if($_FILES){ try{ $files = array(); foreach ($_FILE
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());
}
}
?>
if($\u文件){
试一试{
$files=array();
foreach($\u文件['group']为$k=>l){
foreach($l为$i=>$v){
如果(!array_key_存在($i,$files))
$files[$i]=array();
$files[$i][$k]=$v;
}
}
foreach($files作为$file){
$query=“插入到图库(img,alt_image,created)值(:image,:alt,:created)”;
//准备要执行的查询
$stmt=$con->prepare($query);
$name=$_文件['group']['name'][0];
$ext=pathinfo($name,pathinfo_扩展名);
$rand=md5(uniqid().rand());
$post_image=$rand.”.strtolower($ext);
$withoutExt=preg\u replace('/\..[^.\\s]{3,4}$/',''.$post\u image);
$handle=新上传($file);
如果($handle->上传){
$handle->file\u new\u name\u body=strtolower($withoutExt);
$handle->image\u resize=true;
$handle->image\u ratio\u crop=true;
$handle->image_x=720;
$handle->image_y=630;
$handle->process('../../images/gallery/');
如果($handle->processed){
$handle->clean();
}否则{
回显“错误:”。$handle->Error;
}
}
unset($handle);
//绑定参数
$stmt->bindParam(':image',$post_image);
$stmt->bindParam(':alt',$name);
$created=date('Y-m-d H:i:s');//指定将此记录插入数据库的时间
$stmt->bindParam(':created',$created);
//执行查询
如果($stmt->execute()){
呼应“成功”;
}否则{
回显“错误”;
}
}
}
//显示错误
捕获(PDO异常$exception){
die('ERROR:'。$exception->getMessage());
}
}
?>
您不应该总是在$\u FILES
数组的第一个元素上构建文件扩展名,而是自己使用$FILES
中收集的内容:
$name = $file['name'];
$ext = pathinfo($name, PATHINFO_EXTENSION);
…此外,请看一些调试教程。他们会帮助你自己发现错误;)