1. 程式人生 > >關於curl模擬上傳檔案

關於curl模擬上傳檔案

在用curl模擬上傳的時候遇到一個小問題,所以記錄下來:

  • 在 curl 中設定表單,包括檔案上傳網上很多都是這樣的
 $post_data = array(
        "file" => "@" . "檔案所在路徑"
    );

 $ch = curl_init();
 curl_setopt($ch, CURLOPT_HTTPHEADER, explode("\r\n", $header));//模擬的header頭 
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
 curl_setopt($ch, CURLOPT_URL, $url
); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_TIMEOUT, $time_out); curl_setopt($ch, CURLOPT_COOKIE, $cookie); //post data curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data); $result = curl_exec($ch); $info = curl_getinfo($ch); curl_close($ch);

但是我發現根本就接受不到檔案 $_FILES 這個引數陣列:
後來百度查看了一下發現原來 php5.5 之前可以用這樣的用法,PHP5.5 之後就用一個

new CURLFile(realpath($file_path), $mime_type, $file_name); 

來進行傳引數了:

$post_data['file'] = new CURLFile(realpath($file_path), $mime_type, $file_name);

檔案的 mime 型別可以根據下面的函式求得:

function _getMimeDetect() {
        if (class_exists('finfo')) {
            return 'finfo';
        } else if (function_exists('mime_content_type'
)) { return 'mime_content_type'; } else if ( function_exists('exec')) { $result = exec('file -ib '.escapeshellarg(__FILE__)); if ( 0 === strpos($result, 'text/x-php') || 0 === strpos($result, 'text/x-c++')) { return 'linux'; } $result = exec('file -Ib '.escapeshellarg(__FILE__)); if ( 0 === strpos($result, 'text/x-php') || 0 === strpos($result, 'text/x-c++')) { return 'bsd'; } } return 'internal'; } function _getMimeType($path) { $mime = array( //applications 'ai' => 'application/postscript', 'eps' => 'application/postscript', 'exe' => 'application/octet-stream', 'doc' => 'application/vnd.ms-word', 'xls' => 'application/vnd.ms-excel', 'ppt' => 'application/vnd.ms-powerpoint', 'pps' => 'application/vnd.ms-powerpoint', 'pdf' => 'application/pdf', 'xml' => 'application/xml', 'odt' => 'application/vnd.oasis.opendocument.text', 'swf' => 'application/x-shockwave-flash', // archives 'gz' => 'application/x-gzip', 'tgz' => 'application/x-gzip', 'bz' => 'application/x-bzip2', 'bz2' => 'application/x-bzip2', 'tbz' => 'application/x-bzip2', 'zip' => 'application/zip', 'rar' => 'application/x-rar', 'tar' => 'application/x-tar', '7z' => 'application/x-7z-compressed', // texts 'txt' => 'text/plain', 'php' => 'text/x-php', 'html' => 'text/html', 'htm' => 'text/html', 'js' => 'text/javascript', 'css' => 'text/css', 'rtf' => 'text/rtf', 'rtfd' => 'text/rtfd', 'py' => 'text/x-python', 'java' => 'text/x-java-source', 'rb' => 'text/x-ruby', 'sh' => 'text/x-shellscript', 'pl' => 'text/x-perl', 'sql' => 'text/x-sql', // images 'bmp' => 'image/x-ms-bmp', 'jpg' => 'image/jpeg', 'jpeg' => 'image/jpeg', 'gif' => 'image/gif', 'png' => 'image/png', 'tif' => 'image/tiff', 'tiff' => 'image/tiff', 'tga' => 'image/x-targa', 'psd' => 'image/vnd.adobe.photoshop', //audio 'mp3' => 'audio/mpeg', 'mid' => 'audio/midi', 'ogg' => 'audio/ogg', 'mp4a' => 'audio/mp4', 'wav' => 'audio/wav', 'wma' => 'audio/x-ms-wma', // video 'avi' => 'video/x-msvideo', 'dv' => 'video/x-dv', 'mp4' => 'video/mp4', 'mpeg' => 'video/mpeg', 'mpg' => 'video/mpeg', 'mov' => 'video/quicktime', 'wm' => 'video/x-ms-wmv', 'flv' => 'video/x-flv', 'mkv' => 'video/x-matroska', ); $fmime = _getMimeDetect(); switch($fmime) { case 'finfo': $finfo = finfo_open(FILEINFO_MIME); if ($finfo) $type = @finfo_file($finfo, $path); break; case 'mime_content_type': $type = mime_content_type($path); break; case 'linux': $type = exec('file -ib '.escapeshellarg($path)); break; case 'bsd': $type = exec('file -Ib '.escapeshellarg($path)); break; default: $pinfo = pathinfo($path); $ext = isset($pinfo['extension']) ? strtolower($pinfo['extension']) : ''; $type = isset($mime[$ext]) ? $mime[$ext] : 'unkown'; break; } $type = explode(';', $type); if ($fmime != 'internal' && $type[0] == 'application/octet-stream') { $pinfo = pathinfo($path); $ext = isset($pinfo['extension']) ? strtolower($pinfo['extension']) : ''; if (!empty($ext) && !empty($mime[$ext])) { $type[0] = $mime[$ext]; } } return $type[0]; }

相關推薦

關於curl模擬檔案

在用curl模擬上傳的時候遇到一個小問題,所以記錄下來: 在 curl 中設定表單,包括檔案上傳網上很多都是這樣的 $post_data = array( "file" => "@" . "檔案所在路徑" ); $

Idea使用HttpClicnt模擬檔案

模擬前提是已經搭建好SpringMVC環境配置: 1、建一個maven專案,屬於jar包然後建一個UploadFileDemo類,裡面寫: @Test public void uploadFile() throws IOException { //1、得到Cl

curl post檔案的“failed creating formpost data“錯誤

之前寫過一個php使用curl上傳檔案的程式碼,今天暴出了一個bug,curl錯誤資訊為"failed creating formpost data" 先說一下curl上傳檔案的過程吧 $file =array("upimg"=>"@/tmp/tmp.jpg");

PHP:curl模擬form表單檔案

<form action="" method="post" enctype="multipart/form-data"> <input type="file" name="upload"> <buttion>submit</button> </f

Shell 程式設計用CURL 模擬POST 檔案 解決417 Expectation

話不多說直接拋程式碼 curl -H "Expect:" -F "passport=monitor_134" -F "[email protected]/root/monitor/result/mid_1_rule_1.txt" http://192.168.1

使用CURL模擬表單檔案

有時需要使用PHP程式上傳檔案,其實使用PHP上傳檔案有多種方式,如果能完全模擬出HTML表單的效果這樣伺服器端就不用做任何程式碼的改動了,使用CURL完全可以實現,在PHP5.5之前使用的方式和PHP7.x、PHP5.6x中使用了新的方式,下面是程式碼示例:

Curl 模擬get、 post、put 請求以及檔案

1,Curl 模擬GET請求 //初始化 $ch = curl_init(); //設定請求地址L curl_setopt($ch, CURLOPT_URL, "http://www.nettuts.com"); //設定響應不直接輸出到頁面,1為不

PHP CURL 模擬POST請求 提交資料或檔案

$file = '/doucment/Readme.txt';$ch = curl_init();$post_data = array(     'loginfield' => 'username',     'username' => 'ybb',     'password' => '1

CURL模擬表單post提交及相關常用引數的使用(包括提交表單同時檔案

<form action="doLogin" method="post"> <input type="text" name="username" value="admin"/> <input type="password" nam

curl傳送請求檔案(multipart file upload)

折騰一下午的問題 第三方介面需要我們傳multipart 上傳檔案 curl一直各種試不成功,用Restlet Client工具總是能成功! 對比傳送的頭,發現工具在Content-Type: multipart/form-data;後面多了個這個boundary 然後去查了下

curl post表單檔案(C++)

最近測試如何上傳檔案到伺服器。原來傳照片一致通過binary 形式傳檔案,或者把圖片base64編碼傳圖片。一致沒有用form-data 表單形式傳送資料,今天嘗試下如何使用libcurl提供的API上傳檔案。 Sample code: #include <

PHP使用CURL向Python,Golang傳送檔案表單檔案[HTTP協議下Api]

PHP傳送方程式碼段: <?php /** * htppCurl表單上傳檔案 * @param $file FILE_ADDR * @param string $url uri * @param string $key key * @return bool|mixed *

Python模擬瀏覽器檔案指令碼(Multipart/form-data格式)

http協議本身的原始方法不支援multipart/form-data請求,這個請求由原始方法演變而來的。 multipart/form-data的基礎方法是post,也就是說是由post方法來組合實現的,與post方法的不同之處:請求頭,請求體。 mult

使用httpclient模擬表單檔案,後臺用struts2接收

本人是使用java,開發android後臺的,公司要求使用SSM框架,有一個功能要求是實現android大檔案的上傳。開發人員都是新手,以前沒有開發經驗,鼓搗了好久,也嘗試了兩個android框架,Xutils貌似跟struts2不太好整合,而AsyncHttpClient

java、 http模擬post檔案到服務端 模擬form檔案

需求是這樣的: **1,前後端分離,前端對接pc軟體進行檔案同步的介面,後的springboot微服務進行檔案接收和處理。 2,軟體不能直接呼叫微服務的介面進行上傳,只能先走一下前端controller進行轉發過來()。 3,這樣就只能用httpclien

QT模擬表單檔案到微信伺服器

最近做微信開發,需要上傳素材到微信伺服器,我就用qt寫了一個介面上傳素材,首先我們來看下最終的介面: 然後將裡面的access_token後面的lineEdit命名為:accessMediaEdit,type後面的ComboBox命名為typeComboB

c# 模擬表單提交,post form 檔案、大資料內容

表單提交協議規定: 要先將 HTTP 要求的 Content-Type 設為 multipart/form-data,而且要設定一個 boundary 引數, 這個引數是由應用程式自行產生,它會用來識別每一份資料的邊界 (boundary), 用以產生多重資訊部份 (me

.net模擬http post請求檔案

http://blog.csdn.net/dreamparks/article/details/43149845 public void SendByApi(string url, Stream postedStream, string fileName, string

PHP7 cURL檔案

在做微信上傳素材檔案時出了點問題,伺服器提示media缺失,原上傳程式碼如下:function https_request($url,array $data = null) { $curl = curl_init(); curl_setopt(

java模擬表單檔案,java通過模擬post方式提交表單實現圖片功能例項

package com.zdz.httpclient;import java.io.BufferedReader;import java.io.DataInputStream;import java.io.DataOutputStream;import java.io.File;import java.io.