只有一个文件数据进入数据库

时间:2015-01-12 09:01:16

标签: php mysql cakephp

您好我想将上传的文件数据导入数据库,但我只获得了一个文件数据。所有这些文件数据都试图进入数据库,但是它们使用了所有相同的ID,并且由于这一点,我们写了这一行。我如何将这些数据存入数据库?

我想做什么:

            if (!empty($this->request->data) && is_uploaded_file($this->request->data['UploadFile']['file_upload']['0']['tmp_name'])) {
            $fileData = fread(fopen($this->request->data['UploadFile']['file_upload']['0']['tmp_name'], "r"), $this->request->data['UploadFile']['file_upload']['0']['size']);
            $this->request->data['UploadFile']['name'] = $this->request->data['UploadFile']['file_upload']['0']['name'];
            $this->request->data['UploadFile']['size'] = $this->request->data['UploadFile']['file_upload']['0']['size'];
            $this->request->data['UploadFile']['URL'] = $this->request->data['UploadFile']['file_upload']['0']['tmp_name'];
            $this->request->data['UploadFile']['data'] = $fileData;
            $this->UploadFile->save($this->request->data);
            }
            if (!empty($this->request->data) && is_uploaded_file($this->request->data['UploadFile']['file_upload']['1']['tmp_name'])) {
            $fileData1 = fread(fopen($this->request->data['UploadFile']['file_upload']['1']['tmp_name'], "r"), $this->request->data['UploadFile']['file_upload']['1']['size']);
            $this->request->data['UploadFile']['name'] = $this->request->data['UploadFile']['file_upload']['1']['name'];
            $this->request->data['UploadFile']['size'] = $this->request->data['UploadFile']['file_upload']['1']['size'];
            $this->request->data['UploadFile']['URL'] = $this->request->data['UploadFile']['file_upload']['1']['tmp_name'];
            $this->request->data['UploadFile']['data'] = $fileData1;
            $this->UploadFile->save($this->request->data);
            }
            if (!empty($this->request->data) && is_uploaded_file($this->request->data['UploadFile']['file_upload']['2']['tmp_name'])) {
            $fileData2 = fread(fopen($this->request->data['UploadFile']['file_upload']['2']['tmp_name'], "r"), $this->request->data['UploadFile']['file_upload']['2']['size']);
            $this->request->data['UploadFile']['name'] = $this->request->data['UploadFile']['file_upload']['2']['name'];
            $this->request->data['UploadFile']['size'] = $this->request->data['UploadFile']['file_upload']['2']['size'];
            $this->request->data['UploadFile']['URL'] = $this->request->data['UploadFile']['file_upload']['2']['tmp_name'];
            $this->request->data['UploadFile']['data'] = $fileData2;
            $this->UploadFile->save($this->request->data);
            }

我想将3个文件数据插入数据库,但我只获得一个文件数据。

和结果:

1   INSERT INTO `kontaktid`.`upload_files` (`name`, `size`, `URL`) VALUES ('add.JPG', 61020, 'C:\\wamp\\tmp\\php75DF.tmp')
2   SELECT COUNT(*) AS `count` FROM `kontaktid`.`upload_files` AS `UploadFile` WHERE `UploadFile`.`id` = 34
3   SELECT COUNT(*) AS `count` FROM `kontaktid`.`upload_files` AS `UploadFile` WHERE `UploadFile`.`id` = 34
4   SELECT COUNT(*) AS `count` FROM `kontaktid`.`upload_files` AS `UploadFile` WHERE `UploadFile`.`id` = 34
5   UPDATE `kontaktid`.`upload_files` SET `name` = 'empty rows.JPG', `size` = 73534, `URL` = 'C:\\wamp\\tmp\\php75F0.tmp' WHERE `kontaktid`.`upload_files`.`id` = '34'
6   SELECT COUNT(*) AS `count` FROM `kontaktid`.`upload_files` AS `UploadFile` WHERE `UploadFile`.`id` = 34
7   SELECT COUNT(*) AS `count` FROM `kontaktid`.`upload_files` AS `UploadFile` WHERE `UploadFile`.`id` = 34 
8   SELECT COUNT(*) AS `count` FROM `kontaktid`.`upload_files` AS `UploadFile` WHERE `UploadFile`.`id` = 34
9   UPDATE `kontaktid`.`upload_files` SET `name` = 'vardump.JPG', `size` = 79107, `URL` = 'C:\\wamp\\tmp\\php75F1.tmp' WHERE `kontaktid`.`upload_files`.`id` = '34'

感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

对于每次保存,您应该使用$this->UploadFile->create();,否则它只会保存一个。所以它是这样的:

        if (!empty($this->request->data) && is_uploaded_file($this->request->data['UploadFile']['file_upload']['0']['tmp_name'])) {
        $fileData = fread(fopen($this->request->data['UploadFile']['file_upload']['0']['tmp_name'], "r"), $this->request->data['UploadFile']['file_upload']['0']['size']);
        $this->request->data['UploadFile']['name'] = $this->request->data['UploadFile']['file_upload']['0']['name'];
        $this->request->data['UploadFile']['size'] = $this->request->data['UploadFile']['file_upload']['0']['size'];
        $this->request->data['UploadFile']['URL'] = $this->request->data['UploadFile']['file_upload']['0']['tmp_name'];
        $this->request->data['UploadFile']['data'] = $fileData;
        $this->UploadFile->create();
        $this->UploadFile->save($this->request->data);
        }
        if (!empty($this->request->data) && is_uploaded_file($this->request->data['UploadFile']['file_upload']['1']['tmp_name'])) {
        $fileData1 = fread(fopen($this->request->data['UploadFile']['file_upload']['1']['tmp_name'], "r"), $this->request->data['UploadFile']['file_upload']['1']['size']);
        $this->request->data['UploadFile']['name'] = $this->request->data['UploadFile']['file_upload']['1']['name'];
        $this->request->data['UploadFile']['size'] = $this->request->data['UploadFile']['file_upload']['1']['size'];
        $this->request->data['UploadFile']['URL'] = $this->request->data['UploadFile']['file_upload']['1']['tmp_name'];
        $this->request->data['UploadFile']['data'] = $fileData1;
        $this->UploadFile->create();
        $this->UploadFile->save($this->request->data);
        }
        if (!empty($this->request->data) && is_uploaded_file($this->request->data['UploadFile']['file_upload']['2']['tmp_name'])) {
        $fileData2 = fread(fopen($this->request->data['UploadFile']['file_upload']['2']['tmp_name'], "r"), $this->request->data['UploadFile']['file_upload']['2']['size']);
        $this->request->data['UploadFile']['name'] = $this->request->data['UploadFile']['file_upload']['2']['name'];
        $this->request->data['UploadFile']['size'] = $this->request->data['UploadFile']['file_upload']['2']['size'];
        $this->request->data['UploadFile']['URL'] = $this->request->data['UploadFile']['file_upload']['2']['tmp_name'];
        $this->request->data['UploadFile']['data'] = $fileData2;
        $this->UploadFile->create();
        $this->UploadFile->save($this->request->data);
        }

希望它有所帮助!