简体   繁体   中英

How to save multiple file name to a database?

i have a register form which user should upload their photo and other document ( for example doc-b, doc-b etc ).

in my form I have something like

<input type="file" name="doc-a" id="doc-a">
<input type="file" name="doc-b" id="doc-b">

the "name" tag indicate what type of file it is, is it a document A, document B, photo of him or etc.

in his profile page I want to link each individual file. for example link to doc-b.

so what I have in mind is create a table in DB to save each file name and their type ( form name tag ) . but I don't know how can I do that

here is the code to submit the form, save data to DB and upload the files :

    $this->member->register($this->input->post(NULL, TRUE));
$this->upload->do_upload('photo');
$this->upload->do_upload('doc-a');
$this->upload->do_upload('doc-b');

and then what I want to do is loop each files ( I don't know how should I do this )

    $member_id = $this->db->insert_id();
foreach ($_FILES as $member_files) {

if ($_FILES[] == 'photo') {
$file_type = 'photo';
}elseif ($_FILES['doc-a']) {
$file_type = 'doc-a';


$data = array(
'id_member' => $member_id,
'file_type' => $, // this should be the **name** tag 
'file_name' => $, // this should be the file name
);

$this->db->insert('member_files', $data);
}

anyway, let me know if you have better way to do this. Thanks

To check the extension of the file use this code

 foreach($files as $file){

                    $name = $file->filename; 
                    $info = new SplFileInfo($name);
                    $extension =  $info->getExtension();
   }
if($extension  == "jpg" || $extension  == "png" || $extension  == "gif" || $extension  == "jpeg") {
show your image 
}else{
show other doc file
         }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM