How to implement php http file upload

藏色散人
Release: 2023-03-12 12:48:02
Original
2315 people have browsed it

php http file upload implementation method: 1. Initialize the cURL session; 2. Obtain the CURLFile instance; 3. Execute the given cURL session; 4. Obtain a CURLFile class instance according to the file path; 5. Save the settings folder.

How to implement php http file upload

The operating environment of this article: windows7 system, PHP7.1 version, DELL G3 computer

How to upload http file upload via php?

PHP uses CURL (HTTP) to upload files from the server to another server

The code is as follows:

// 上传端   
    /**
     * 向目标地址推送xls文件
     * @Date 2019/4/29
     */
    public function putXls()
    {
        // 目标接口
        $url = "http://xxx";

        // 初始化 cURL 会话, 如果提供url,CURLOPT_URL 选项将会被设置成这个值
        $ch = curl_init($url);

        // 获取CURLFile实例
        $xlsCurlFile = $this->makeCurlFile(base_path()."/public/tby.xls");

        $data = array('xls' => $xlsCurlFile);

        // TRUE 时会发送 POST 请求,类型为:application/x-www-form-urlencoded,是 HTML 表单提交时最常见的一种。
        curl_setopt($ch, CURLOPT_POST, 1);

        // 从 PHP 5.5.0 开始, @ 前缀已被废弃,文件可通过 CURLFile 发送。 设置 CURLOPT_SAFE_UPLOAD 为 TRUE 可禁用 @ 前缀发送文件,以增加安全性。
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

        // 执行给定的 cURL 会话
        // 成功时返回 TRUE, 或者在失败时返回 FALSE。 然而,如果 设置了 CURLOPT_RETURNTRANSFER 选项,函数执行成功时会返回执行的结果,失败时返回 FALSE 。
        $result = curl_exec($ch);

        if (curl_errno($ch)) {// 返回错误代码或在没有错误发生时返回 0 (零)。
            // 返回错误信息,或者如果没有任何错误发生就返回 '' (空字符串)。
            $result = curl_error($ch);
        }

        // 关闭 cURL 会话
        curl_close($ch);
    }
    
    /**
     * 根据文件路径获取一个CURLFile类实例
     * @param string $file 文件路径
     * @return CURLFile
     * @Date 2019/4/29
     */
    private function makeCurlFile(string $file)
    {
        /**
         * .xls mime为 application/vnd.ms-excel
         * .xlsx mime为 application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
         * 可参考 https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Complete_list_of_MIME_types
         * 
         *  注意:也可以使用 finfo类动态获取,但需要装fileinfo扩展
         *  demo:
            $result = new finfo();
            if (is_resource($result) === true) {
                return $result->file($filename, FILEINFO_MIME_TYPE);
            }
            return false;
         */
        $mime = "application/vnd.ms-excel";
        $info = pathinfo($file);
        $name = $info['basename'];
        $output = new CURLFile($file, $mime, $name);
        return $output;
    }
// 接收端
public function getFile(){    
// 保存的文件夹,需要注意所在用户组是否有写入权限    
$uploads_dir = base_path()."/public/test";    
$xlsFiles = $_FILES["xls"] ?? null;    
if($xlsFiles){        
if ($xlsFiles["error"] == UPLOAD_ERR_OK) {            
$tmp_name = $xlsFiles["tmp_name"];            
$name = $xlsFiles["name"];            
move_uploaded_file($tmp_name, "$uploads_dir/$name");        }        
// todo success    
}else{        
// todo fail    }}
Copy after login

Recommended learning: "PHP Video tutorial

The above is the detailed content of How to implement php http file upload. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template