我想做的是在文件移动到上传文件夹之前重命名图像的文件名,并将链接保存到数据库。
我的问题是在我当前的代码中,文件的原始名称在移动到上传文件夹后仍然存在,甚至在保存链接到数据库时,原始图像名称仍然存在。
在将图像文件的原始名称移动到上传文件夹之前以及将链接保存到数据库之前,如何重命名图像文件的原始名称?
示例:原始名称Oppa/upload/default.png
我想将其重命名为Oppa/upload/1.png
php代码:
<?php
include_once('../dbc/database.php');
$db = new Connection();
$db = $db->dbConnect();
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$imageLink = isset($_POST['imageLink']) ? $_POST['imageLink'] : "";
$email = isset($_POST['email']) ? $_POST['email'] : "";
$image = addslashes(file_get_contents($_FILES['imageInput']['tmp_name']));
$image_name = addslashes($_FILES['imageInput']['name']);
$image_size = getimagesize($_FILES['imageInput']['tmp_name']);
move_uploaded_file($_FILES["imageInput"]["tmp_name"], "../upload/" . $_FILES["imageInput"]["name"]);
$location = "Oppa/upload/" . $_FILES["imageInput"]["name"];
if(!empty($_POST['imageLink'])) {
$q = "UPDATE tbl_user SET user_image = '$location' WHERE user_email= :email ";
$query = $db->prepare($q);
$query->bindParam(':email', $email);
$results = $query->execute();
echo "1";
}
?>
答案 0 :(得分:2)
尝试如下:
$type = $_FILES["imageInput"]["type"];
$ext = end(explode('/', $type));
$filename = uniqid() . '.' . $ext; // you can set name here whatever you want
move_uploaded_file($_FILES["imageInput"]["tmp_name"], "../upload/" . $filename);
$location = "Oppa/upload/" . $filename;
答案 1 :(得分:0)
$image_name = "my_img.jpeg";
move_uploaded_file($_FILES["imageInput"]["tmp_name"], "../upload/" . $image_name);
$location = "Oppa/upload/" . $image_name;
这将使用image_name变量
中定义的新名称保存文件