我完全坚持这种情况下, 问题1: 我想发送文件从一个服务器到其他,发送文件我使用卷曲,标准或更常见通过curl发送文件的代码 `$ url = $ callthis; $ filename = $ _FILES ['orig_file_name'] ['name']; $ filedata = $ _FILES ['orig_file_name'] ['tmp_name']; $ filesize = $ _FILES ['orig_file_name'] ['size']; 如果($ FILEDATA!=“”) {发送文件使用PHP CURL IIS服务器不工作
$headers = array("Content-Type:multipart/form-data"); // cURL headers for file uploading
$postfields = array("orig_file_name" => "@$filedata", "filename" => $filename, 'user_id' => $this->Session->read('userid'),
'artwork_type'=>$this->request->data['artwork_type'],
'description'=>$this->request->data['description'],
'itemstable_id'=>$this->request->data['itemstable_id'],
'upload_date'=>$this->request->data['upload_date'],
'brand_Approved'=>'New');
$ch = curl_init();
$options = array(
CURLOPT_URL => $url,
CURLOPT_HEADER => true,
CURLOPT_POST => 1,
CURLOPT_HTTPHEADER => $headers,
CURLOPT_POSTFIELDS => $postfields,
CURLOPT_INFILESIZE => $filesize,
CURLOPT_RETURNTRANSFER => true
); // cURL options
curl_setopt_array($ch, $options);
curl_exec($ch);
if(!curl_errno($ch))
{
$info = curl_getinfo($ch);
debug($info);
if ($info['http_code'] == 200)
return $this->redirect(array('action' => 'uploadedart'));
}
else
{
$errmsg = curl_error($ch);
$this->Session->setFlash(__($errmsg));
}
curl_close($ch);
}`
仅供参考,卷曲安装在服务器上,并启用 此代码的工作完美我的本地PC上,我期待在Linux系统上相同的,但我的代码将在amazone IIS服务器上实现,所以当我上传代码,然后我测试它给我0状态代码,当我谷歌,他们说你的网址是不正确的,在我的情况是正确的,所以问题是'@'符号在文件名和php curl文档之前放在curl请求中,他们说@ sign是为了告诉接收者服务器它是一个非纯文本的物理文件,当我删除这个@符号时,我得到了200个响应代码从接收端,但文件没有得到保存在t中他想要的目录.....
问题2:我实现了另一个代码从一个人在stackoverflow `$ url = $ callthis; $ filename = $ _FILES ['orig_file_name'] ['name']; $ filedata = $ _FILES ['orig_file_name'] ['tmp_name']; $ filesize = $ _FILES ['orig_file_name'] ['size']; // debug($ this-> request-> data); exit(); 如果($ FILEDATA!= ''){
move_uploaded_file($filedata, APP."webroot".DS."upload".DS.$filename);
$newfile=APP."webroot".DS."upload".DS.$filename;
//exit;
/* begin stuff */
$postfields = array('user_id' => $this->Session->read('userid'),
'artwork_type'=>$this->request->data['artwork_type'],
'description'=>$this->request->data['description'],
'itemstable_id'=>$this->request->data['itemstable_id'],
'upload_date'=>$this->request->data['upload_date'],
'brand_Approved'=>'New');
$file_url = $newfile; //here is the file route, in this case is on same directory but you can set URL too like "http://examplewebsite.com/test.txt"
$eol = "\r\n"; //default line-break for mime type
$BOUNDARY = md5(time()); //random boundaryid, is a separator for each param on my post curl function
$BODY=""; //init my curl body
$BODY.= '--'.$BOUNDARY. $eol; //start param header
$BODY .= 'Content-Disposition: form-data; name="otherfields"' . $eol . $eol; // last Content with 2 $eol, in this case is only 1 content.
$BODY .= serialize($postfields) . $eol;//param data in this case is a simple post data and 1 $eol for the end of the data
$BODY.= '--'.$BOUNDARY. $eol; // start 2nd param,
$BODY.= 'Content-Disposition: form-data; name="orig_file_name"; filename="'.$filename.'"'. $eol ; //first Content data for post file, remember you only put 1 when you are going to add more Contents, and 2 on the last, to close the Content Instance
$BODY.= 'Content-Type: application/octet-stream' . $eol; //Same before row
$BODY.= 'Content-Transfer-Encoding: base64' . $eol . $eol; // we put the last Content and 2 $eol,
$BODY.= chunk_split(base64_encode(file_get_contents($file_url))) . $eol; // we write the Base64 File Content and the $eol to finish the data,
$BODY.= '--'.$BOUNDARY .'--' . $eol. $eol; // we close the param and the post width "--" and 2 $eol at the end of our boundary header.
$ch = curl_init(); //init curl
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'X_PARAM_TOKEN : 71e2cb8b-42b7-4bf0-b2e8-53fbd2f578f9' //custom header for my api validation you can get it from $_SERVER["HTTP_X_PARAM_TOKEN"] variable
,"Content-Type: multipart/form-data; boundary=".$BOUNDARY) //setting our mime type for make it work on $_FILE variable
);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/1.0 (Windows NT 6.1; WOW64; rv:28.0) Gecko/20100101 Firefox/28.0'); //setting our user agent
curl_setopt($ch, CURLOPT_URL, $url); //setting our api post url
//curl_setopt($ch, CURLOPT_COOKIEJAR, $BOUNDARY.'.txt'); //saving cookies just in case we want
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); // call return content
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1); //navigate the endpoint
curl_setopt($ch, CURLOPT_POST, true); //set as post
curl_setopt($ch, CURLOPT_POSTFIELDS, $BODY); // set our $BODY
$response = curl_exec($ch); // start curl navigation
print_r($response); //print response
this works fine but only with text files because it does
chunk_split(BASE64_ENCODE(的file_get_contents($ FILE_URL)))`我的意思是读取文件发送,这在文本文件,但在我的情况下工作的罚款我想发送在问题2不工作,因为它修改图像内容,因此无法打开图像的图像....
fyi,我不能做jquery/ajax文件发送,我需要做它通过php,所以答案应该是有效可行的curl请求代码发送文件或cakephp httpsocket发送文件
我的php版本是5.5 – ahsanali 2015-02-24 05:52:28