【问题标题】:How do you rename a file with the just assigned auto increment id from mysql?如何使用刚刚从 mysql 分配的自动增量 ID 重命名文件?
【发布时间】:2017-03-09 11:16:22
【问题描述】:

我正在制作一个表单,该表单应该将图像上传到我的服务器,同时在数据库中给它一个 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);
}
?>

【问题讨论】:

    标签: php mysql image file-upload rename


    【解决方案1】:

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

       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);
    

    【讨论】:

    • 非常感谢 :) 你知道我怎么能在成功传输图像后,破坏“已发布”表单的内容,以便在我刷新时不会再次发布任何内容?这种情况现在一直在发生,如果填充重复项,我的数据库:/ @webjuju
    • 这当然是一个新问题,但是看看 php 函数 header();作为一个开始(并确保你在使用它之前没有允许任何输出......这是最大的陷阱)。
    【解决方案2】:

    代码:

    //Replace this with $target_file = $target_dir . basename($_FILES["imageUpload"]["name"]);
    //mysql_query to find last most id in table : 
    
    $res = mysqli_query($mysqliConnection, 'SELECT MAX(file_id) FROM table_name')
    
    //Get Its result into some $fid variable.
    $fid = while ($result1 = mysqli_fetch_array($res)){
    result1['file_id']
    } ;
    $fid= $fid+1;
    //now to name your file as FILE_ID
    $target_file = $fid ;
    

    这就是您要插入代码的全部内容。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-10
      • 1970-01-01
      • 2011-05-20
      • 2011-01-18
      • 2013-08-02
      • 1970-01-01
      相关资源
      最近更新 更多