duanlan7903 2015-08-10 18:18
浏览 38
已采纳

使用数据库中的id重命名文件

I am uploading image files using their original name. I upload the file path etc. to a database and every image gets a unique id in the database. Until here everything works fine.

Now, I would like to give the uploaded image files their unique id from the database table as a name. (e.g. if the image id is 1, I would like the file to be called 1.jpg and so on.) I cannot get it to work.

My not working approach:

The database table

 TABLE `images`
 `image_id` INT(11) unsigned NOT NULL AUTO_INCREMENT,
 `user_id` INT(11) unsigned NOT NULL COMMENT 'FOREIGN KEY referencing the user_id of the user, who uploaded the images',
 `image_name` VARCHAR(64) NOT NULL,
 `image_path` VARCHAR(64) NOT NULL,
 `image_upload_timestamp` TIMESTAMP NOT NULL ON UPDATE CURRENT_TIMESTAMP,
 PRIMARY KEY (`image_id`);

The script to write the image to the database using its original name (working):

$database = DatabaseFactory::getFactory()->getConnection();

$sql = "INSERT INTO images VALUES ('', :user_id, :image_name, :image_path, :image_upload_timestamp)";
$query = $database->prepare($sql);

$query->execute(array(':user_id' => Session::get('user_id'), 
                      ':image_name' => $file_name, 
                      ':image_path' => $file_destination, 
                      ':image_upload_timestamp' => time()));

ImageModel::renameImage($image_id);

The renameImage method (not working!)

public static function renameImage($image_id)
    {   
        $database = DatabaseFactory::getFactory()->getConnection();

        $query = $database->prepare("SELECT * FROM images WHERE image_id = :image_id AND user_id = :user_id");
        $query->execute(array(':image_id' => $image_id, ':user_id' => Session::get('user_id')));

        $img_id = $query->fetch();

        // Convert array to string
        $file_name = $img_id->image_id;

        $image_name = $file_name . '.jpg';


        $sql = "UPDATE images SET image_name = :image_name WHERE :image_id = $image_id";
        $sth = $database->prepare($sql);

        $sth->execute(array(':image_name' => $image_name));
    }



I am really new to this and would highly appreciate any kind of help! Thank you very much!

  • 写回答

2条回答 默认 最新

  • doudou20145 2015-08-10 18:30
    关注

    Ok, there in the code that you show aren't any related with files, assuming that you are doing the file upload in other place, you just need to use http://php.net/manual/en/function.rename.php

    rename($img_id->image_path.$img_id->image_path, $img_id->image_path.$image_name);
    
    $sql = "UPDATE images SET image_name = :image_name WHERE image_id = :image_id";
    $sth = $database->prepare($sql);
    
    $sth->execute(array(':image_name' => $image_name,
                        ':image_id' =>  $img_id ));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用