2015-02-23 65 views
0

我完全堅持這種情況下, 問題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發送文件

回答

0

請注意,PHP> = 5 .6默認禁用curl的@file約定,所以如果你有不同的版本,這可能是你的問題。

要使用現有的代碼PHP> = 5.6,你可以添加

CURLOPT_SAFE_UPLOAD => false 

到你的選擇陣列。

如果這是你的問題,請參閱更改日誌的詳細信息: http://us3.php.net/manual/en/migration56.changed-functions.php

否則,僅僅是明確的:這不應該,除非服務器是直接關係到你的Web服務器(IIS與Apache的)使用不同的PHP配置,或者運行Web服務器服務的用戶有權限連接到影響腳本的用戶。

所以你一定的PHP /捲曲庫在您的環境中正常運行?: 有時候,我只是做了一個「Hello字」測試在終端命令提示符下,如:

php -r "$ch = curl_init('http://www.google.com'); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $output = curl_exec($ch); die($output);" 

這正好應轉儲谷歌。com的代碼進入你的終端。這有效,對嗎?

對你的網址做同樣的事......特別是如果你使用的是SSL!你可能有SSL問題,如同行驗證等!另外請注意,如果上述工作與https://your.url.com,它可能不能保證它在您的web應用程序中工作,如果SSL庫依賴項(在IIS情況下libeay32.dll & ssleay32.dll)可能無法訪問。

狀態0碼,似乎預示着什麼基於上述測試將彈出,但它可以放棄鬼其他一些原因:

例如,你在確保$ FILEDATA您代碼是一個可訪問的路徑?也許嘗試設置這些帖子字段之前:

if(!is_file($ filedata))die('where is'。$ filedata。'?');

+0

我的php版本是5.5 – ahsanali 2015-02-24 05:52:28

0

@filename已被棄用,在PHP> = 5.5.0:

使用新CurlFile代替,不需要在接收端進行任何更改。

相關問題