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

时间:2022-12-02 17:55:17

I'm currently working on a project wherein I need to batch upload records with files. The data will be coming from an MS Excel file, and a column will be the file path of the record's attached file. I managed to do it with local network, however, I used a shared folder for it in every computer and I am only getting the IP address of the user to go to that shared folder. It would be just fine, if the system will be used with intranet. The problem is, the system should run through internet. Is it possible to do this?

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

P.S. I used the copy() function of PHP.

附:我使用了PHP的copy()函数。

Edit: Here's the code where I put the values of the file column into loop

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

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>";
            }
        }

This method is for the shared folder approach. It works within the network. But when I try the copy() function with the path like 'C:/Users/etc/file.jpg', the error 'Failed to add...' appears. Is there any way to do this with the IP address of the user but you'll get it from different locations in his computer?

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

1 个解决方案

#1


According to the examples in the php manual it should work.

根据php手册中的例子,它应该工作。

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

However, I would encourage you to use $FILES and move_uploaded_file

但是,我建议您使用$ FILES和move_uploaded_file

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

#1


According to the examples in the php manual it should work.

根据php手册中的例子,它应该工作。

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

However, I would encourage you to use $FILES and move_uploaded_file

但是,我建议您使用$ FILES和move_uploaded_file

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