Curl如何处理上传文件?

时间:2019-01-29 13:27:19

标签: php file curl upload

我有上载excel文件的应用程序,然后将excel上的数据插入数据库。该应用程序将给出2个输出。第一个输出是成功插入数据时的输出,另一个输出是数据已存储在数据库中时的输出。

现在,在不同的服务器中,我想构建新的应用程序,并希望通过Curl使用该上载方法(控制器)。我已经实现了它,并且可以正常工作。但是某些excel文件无法正常工作,因为应用程序会输出已存储数据的信息。

我认为我在使用Curl时发生了一些错误,导致我两次使用Curl上传。

已经存储数据时,将给出输出“数据库中存在”或“连接失败”。我正在使用该参数作为对用户的输出进行解析,因此将数据成功插入数据库时​​也是如此。

if (isset($_POST['btnUpload'])) {
        $url = //controller to upload excel to database
        $filename = basename($_FILES['upload-file']['name']);
        $filedata = $_FILES['upload-file']['tmp_name'];
        $filesize = $_FILES['upload-file']['size'];


        if ($filedata != '') {
            $headers = array("Content-Type:multipart/form-data"); // cURL headers for file uploading
            $postfields = array(
                "upload-file" => new CURLFile($filedata, $_FILES['upload-file']['type'], $filename),    
            );
            $ch = curl_init();
            $options = array(
                CURLOPT_URL => $url,
                CURLOPT_POST => 1,
                CURLOPT_HTTPHEADER => $headers,
                CURLOPT_POSTFIELDS => $postfields,
                CURLOPT_RETURNTRANSFER => true
            ); // cURL options
            curl_setopt_array($ch, $options);
            $output = curl_exec($ch);

            curl_close($ch);

            if ((strpos($output, 'exist') !== false) or (strpos($output, 'Failed') !== false)) {
                        $this->session->set_flashdata('msg-error',$output);
                        redirect($_SERVER['HTTP_REFERER']);
            } else {
                        $this->session->set_flashdata('msg-success',"File uploaded");
                        redirect($_SERVER['HTTP_REFERER']);
            }
   }

任何反馈将不胜感激

0 个答案:

没有答案