美好的一天,这里我有一个存储数据的页面,其中包含一些图像,确切地说是 3 个图像,
<?php
$brg = $_POST['id'];
$nama = $_POST['nm'];
$img = $_FILES['img']['name'];
$tmp = $_FILES['img']['tmp_name'];
$img1 = $_FILES['img1']['name'];
$tmp1 = $_FILES['img1']['tmp_name'];
$img2 = $_FILES['img2']['name'];
$tmp2 = $_FILES['img2']['tmp_name'];
$temp = explode(".", $img);
$temp1 = explode(".", $img1);
$temp2 = explode(".", $img2);
$new = round(microtime(true)) . '.' . end($temp);
$new1 = round(microtime(true)) . '.' . end($temp1);
$new2 = round(microtime(true)) . '.' . end($temp2);
$path = "img/photo/".$new;
$path1 = "img/photo/".$new1;
$path2 = "img/photo/".$new2;
move_uploaded_file($tmp, $path);
move_uploaded_file($tmp1, $path1);
move_uploaded_file($tmp2, $path2);
$c = "insert into imgstuff values('$brg','$nama','$new','$new1','$new2');";
$ins=mysqli_query($con,$c);
if($ins){
header('location: test.php?success='.base64_encode('success'));
} else {
header('location: test.php?error='.base64_encode('failed'));
}
?>
上面的代码工作完美,但正如你所看到的,它最终变得非常丑陋,并且它为三张图片生成相同的文件名,
我的问题是,有什么方法可以使我的代码更干净且易于维护我的命名文件方法有什么问题吗?
最佳答案
也许
round(microtime(true)) . '.' . end($temp);
返回相同的内容,因为它几乎是在同一时间完成的。你可以尝试像 uuid 这样的东西,甚至是一个简单的函数,例如
function getUniqFileName($end) {
$token = md5(sha1(rand()));
return sprintf($token%s, $end);
}
然后在你的代码中
$new = getUniqFileName(end($temp));
这会很快给你一个更独特的名字?
(编辑:删除了在这种情况下毫无用处的回合)
关于php - 多个图像上传到不同的列php mysql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47669949/