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 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题