我想将上传的图片文件名更改为某个名称,例如:
Original name:city.jpg -> D0000_04042018094730121.jpg
(D0000是kodeDosen,另一个是microtime时间戳。)
这是我的php代码:
uploadDosen.php
<?php
include 'connectdb.php';
if (isset($_POST['upload_Btn'])) {
$target = "uploaddosen/".basename($_FILES['gambar']['name']);
$kodeDosen = $_POST['kodeDosen'];
$namaJurnal = $_POST['namaJurnal'];
$tipePublikasi = $_POST['tipePublikasi'];
$status= $_POST['status'];
$gambar = $_FILES['gambar']['name'];
$sql = "INSERT INTO tbl_publikasi (kodeDosen,gambar,namaJurnal,tipePublikasi,status) VALUES ('$kodeDosen','$gambar','$namaJurnal',
'$tipePublikasi','$status')";
// execute query
mysqli_query($conn, $sql);
if (move_uploaded_file($_FILES['gambar']['tmp_name'],$target)) {
$msg = "Image uploaded successfully";
}else{
$msg = "Failed to upload image";
}
header("location:uploadTest.php");
}
?>
答案 0 :(得分:0)
只需将 $ target 的值更改为首选文件名即可。
您可以尝试:
$extention = explode(".", $_FILES['gambar']['name']);
$extention = end($extention);
$target = $kodeDosen."_".str_replace(array(".", " "), "", microtime() ).".".$extention;
答案 1 :(得分:0)
而不是使用
$target = "uploaddosen/".basename($_FILES['gambar']['name']);
将您想要的名字放在
中$ext = end((explode(".", $_FILES['gambar']['name'])));
$target = "uploaddosen/MYNEWNAME." . $ext
$ ext正在获取上传的文件名并获取文件扩展名。然后将它与您的新名称一起添加。