如何在不使用HTML文件输入字段的情况下批量上传文件

时间:2015-05-06 03:50:12

标签: php

我目前正在开展一个项目,我需要批量上传包含文件的记录。数据将来自MS Excel文件,列将是记录附加文件的文件路径。我设法使用本地网络,但是,我在每台计算机上都使用了共享文件夹,我只获取用户的IP地址转到该共享文件夹。如果系统将与内联网一起使用,那就没问题了。问题是,系统应该通过互联网运行。有可能这样做吗?

P.S。我使用了PHP的copy()函数。

编辑:这是我将文件列的值放入循环

的代码
for($i=5; $i<=9; $i++){ // GET ONLY 5 ROWS OF DATA (FOR TESTING)
        $series_number = $objWorksheet->getCellByColumnAndRow(0,$i)->getValue();
        if($series_number!=''){
            $date = $objWorksheet->getCellByColumnAndRow(1,$i)->getValue();
            $date = PHPExcel_Shared_Date::ExcelToPHP($date);
            $date = date('Y-m-d', $date);
            $issuedto = explode('/', $objWorksheet->getCellByColumnAndRow(2,$i)->getValue());
            $issuedby = $objWorksheet->getCellByColumnAndRow(3,$i)->getValue();
            $q = $this->db->query('SELECT id FROM tbl_employees WHERE name="'.$issuedby.'"');
            if($q->num_rows()>0){
                $issuedbyid = $q->row()->id;
            }else{
                $issuedbyinsert =  array('name' => $issuedby);
                $this->db->insert('tbl_employees', $issuedbyinsert);
                $issuedbyid = $this->db->insert_id();
            }
            $subject = $objWorksheet->getCellByColumnAndRow(4,$i)->getValue();
            $original_file_name = $objWorksheet->getCellByColumnAndRow(5,$i)->getValue();
            $ext = pathinfo($original_file_name, PATHINFO_EXTENSION);
            $classification = $objWorksheet->getCellByColumnAndRow(6,$i)->getValue();
            $restriction = $objWorksheet->getCellByColumnAndRow(7,$i)->getValue();
            $file = explode('\\', $original_file_name);
            $file = end($file);
            // UPLOAD THE FILE DECLARED IN EXCEL
            if (copy('\\\\'.$_SERVER['REMOTE_ADDR']."\\".$original_file_name, 'uploads/docs/'.$file)) {
                // NOTE: PLEASE INSTALL IMAGEMAGICK
                $this->load->helper('file');
                $check = explode('/', get_mime_by_extension('uploads/docs/'.$file));
                if($check[0]=='image'){
                    exec('convert uploads/docs/'.$file.' uploads/docs/'.preg_replace('"\.'.$ext.'"', '.pdf', $file));
                    unlink('uploads/docs/'.$file);
                    $file = preg_replace('"\.'.$ext.'"', '.pdf', $file);
                }

                // Insert to tbl_issuances
                if($this->session->userdata['usertype']==1){
                    $status2 = 'Approved';
                }else{
                    $status2 = 'Pending';
                }
                $insert = array(
                        'series_number' => $series_number,
                        'date_created' => $date,
                        'issued_by' => $issuedbyid,
                        'subject' => $subject,
                        'file' => $file,
                        'original_file_name' => $file,
                        'status' => $status2,
                        'restriction' => $restriction,
                        'classification' => $classification,
                        'record_type' => 1,
                        'batch_id' => $batch_id
                    );
                $this->db->insert('tbl_issuances',$insert);
                $issuance_id = $this->db->insert_id();

                // Insert to tbl_emp_issuances
                foreach ($issuedto as $val) {
                    if(strpos($val, 'Staff')==false){
                        $q = $this->db->query('SELECT id FROM tbl_employees WHERE name ="'.$val.'"');
                        if($q->num_rows()>0){
                            $issuedtoid = $q->row()->id;
                        }else{
                            $this->db->insert('tbl_employees', array('name' => $val));
                            $issuedtoid = $this->db->insert_id();
                        }
                    }
                    $issuedtoinsert = array(
                            'emp_id' => $issuedtoid,
                            'issuance_id' => $issuance_id
                        );
                    $this->db->insert('tbl_emp_issuances',$issuedtoinsert);
                }       
            } else {
                $error .= "Failed to add <span style='color:red'>'".$subject."'</span> on <span style='color:blue'>row ".$i."</span> because the file you're trying to upload is not found.<br>";
            }
        }

此方法适用于共享文件夹方法。它在网络中工作。但是当我尝试使用像'C:/Users/etc/file.jpg'这样的路径的copy()函数时,会出现错误'无法添加...'。有没有办法用这个用户的IP地址做这个,但你会从他的电脑的不同位置得到它?

1 个答案:

答案 0 :(得分:1)

根据php手册中的示例,它应该可以工作。

http://php.net/manual/en/function.copy.php

但是,我建议您使用$FILESmove_uploaded_file

http://php.net/manual/en/features.file-upload.php