php - 如何使用 mysql 刚刚分配的自动增量 id 重命名文件?

标签 php mysql image file-upload rename

我正在开发一个表单,该表单应该将图像上传到我的服务器,同时在数据库中给它一个 ID,以便我稍后可以将其显示给正确的用户。我的问题是如何将文件重命名为我的 mysql 给它的 id?在我的数据库中,自动增量字段的名称是“pictureID”。我想在将其传输到我的服务器时获取其中的值并以此命名我的图片:)

这是我的代码,大部分来自w3school呵呵(mysql和php不是我的强项)

$_POST 变量来自已提交的图片..

<?php
if(isset($_POST["goOnGallery"]) OR isset($_POST["goOnCamera"])){

        // Create connection
        $mysqliConnection = mysqli_connect($mysqliHost, $mysqliUser, $mysqliPassword, $mysqliDatabase);
        // Check connection
        if (!$mysqliConnection) {die("Connection failed: " . mysqli_connect_error());}

        $insertOfData = "INSERT INTO styles (userID,pictureLikes,pictureDislikes,pictureGender) VALUES ('$userID', '0', '0', '$userGender')";

        if (mysqli_query($mysqliConnection, $insertOfData)) {
            $target_dir = "userarchive/".$userID."/pictures/mystyles/";
            $target_file = $target_dir . basename($_FILES["imageUpload"]["name"]);
            $uploadOk = 1;
            $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
            // Check file size
            if ($_FILES["imageUpload"]["size"] > 500000) {
                echo "Sorry, your file is too large.";
                $uploadOk = 0;
            }
            // Allow certain file formats
            if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" && $imageFileType != "gif" ) {
                echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
                $uploadOk = 0;
            }
            // Check if $uploadOk is set to 0 by an error
            if ($uploadOk == 0) {
                echo "Sorry, your file was not uploaded.";
            // if everything is ok, try to upload file
            } else {
                if (move_uploaded_file($_FILES["imageUpload"]["tmp_name"], $target_file)) {
                    echo "The file ". basename( $_FILES["imageUpload"]["name"]). " has been uploaded.";
                } else {
                    echo "Sorry, there was an error uploading your file.";
                }
            }
        } else {
            echo "Error: " . $insertOfData . "<br>" . mysqli_error($mysqliConnection);
        }
        mysqli_close($mysqliConnection);
}
?>

最佳答案

你已经很接近了!只需使用这些行:

   if (mysqli_query($mysqliConnection, $insertOfData)) {
        $pictureID = mysqli_insert_id($mysqliConnection);
        $fe = substr($_FILES["imageUpload"]["name"], strrpos($_FILES["imageUpload"]["name"], '.'));
        $target_dir = "userarchive/".$userID."/pictures/mystyles/";
        $target_file = $target_dir . $pictureID.$fe);

关于php - 如何使用 mysql 刚刚分配的自动增量 id 重命名文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40273289/

相关文章:

php - 按下后退按钮时如何使 Firefox 重新加载页面?

java - 当我尝试从 play 框架访问我​​的数据库(MySQL)时,我在编码 :"The import play.db cannot be resolved"时收到此错误

mysql - 查询搜索网址匹配

android - 如何在警告对话框中缩放图像

php - 如何在Php项目中集成iiif图片查看器?

php - 显示记录的性能问题

php - Doctrine 冲洗单个实体

javascript - 通过 jQuery AJAX for MYSQL 传递多维数组

image - JasperReports 中当其值为 NULL 或空时隐藏图像

php - Windows 上的多个 Web 开发环境