douzhao7634 2013-12-13 20:18
浏览 58
已采纳

PHP - 检查文件上传时的不同文件名

I'm pretty new to PHP. I hate to have to ask questions, because I'm sure this is documented somewhere, but after looking for a while, I just cannot seem to put two and two together.

I have a program that will allow multiple images to be uploaded to an item within the database. Basically, for each item in the database, there might be multiple image uploads.

Example:

  • Item: Xbox 360


Images:

  • Xbox360.jpg
  • side.jpg
  • front.jpg

The image and item information is all stored in the database (MySQL), but the images are stored within the filesystem, and the database points to the URL of the image(s) in the filesystem.

The problem I'm having is that everything works as expected, except it allows duplicate image names to be written to the database. It doesn't allow duplicate images to be written to the filesystem, which I'm happy with. I want to make sure that the name of an image is only added once to the database. If the image name is a duplicate to another, it needs to not write to the database.

add_db.php:

$uniqueDir = uniqid();
$directory = "img/$uniqueDir/";

db_addItem($id_category, $name, $cost, $description, $qty, $directory); //Adds to the `items` table

foreach ($_FILES['i_file']['name'] as $filename) { 
    if ($filename != '' && $filename != 'No file chosen') {
        //I think above is where I check for unique image names
        $url = "img/$uniqueDir/$filename";
        db_addImg($url, $filename); //Adds to the `img` table
        $item_picsID = get_item_PicsID($filename, $url);
        $itemID = get_itemID($directory);
        db_insertImg($itemID, $item_picsID);
    }
}
addFilesystem($directory); //Writes image(s) to filesystem

function db_addImg($url, $filename) {
    include 'mysql_login_pdo.php';

    // Create the SQL query
    $query = "INSERT INTO img (`name`, `url`) VALUES(:filename, :url)";

    $stmt = $db->prepare($query);
    $stmt->execute(array(
        ':filename' => $filename,
        ':url' => $url
    ));
}

function db_insertImg($itemID, $item_picsID) {
    include 'mysql_login_pdo.php';

    // Create the SQL query
    $query = "INSERT INTO `item_pics` (`item_id`, `img_id`) VALUES(:itemID, :item_picsID)";

    $stmt = $db->prepare($query);
    $stmt->execute(array(
        ':itemID' => $itemID,
        ':item_picsID' => $item_picsID
    ));
    $db = null;
    return;
}

Everything works, except it will write duplicate image names to the database. I want the image names to be distinct. I also don't want to rename the images (if possible).

  • 写回答

3条回答 默认 最新

  • dongtangxi1584 2013-12-13 20:27
    关注

    You can define an UNIQUE index on the name column in img table, and then use slightly modified INSERT statement in your db_addImg function:

    function db_addImg($url, $filename) {
        //...
        $query = "INSERT INGORE INTO img (`name`, `url`) VALUES(:filename, :url)";
        //...
    }
    

    It will quietly cancel any insert where the is a UNIQUE key collision, which will end up in distinct filenames across your img table.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么