2016-06-08 54 views
0

所以我試圖使用cURL將文件發送到另一個頁面以及其他POST變量。除文件發送外,其中大部分都有效。但它只在我的本地主機上不起作用。當它上傳到託管的Web服務器時,它的工作原理與它應該完全相同。使用cURL發佈文件在路徑的開始處無法使用@

我也不想使用CURLFile,因爲Web服務器不支持它。

下面是代碼:

 // Output the image 
     imagejpeg($fileData['imageBackground'], $newFileName, 75); 

     // Get Old Background 
     $query['getBackground'] = $this->PDO->prepare("SELECT backgroundImage FROM accounts WHERE token = :token"); 
     $query['getBackground']->execute(array(':token' => $token)); 

     $queryData = $query['getBackground']->fetch(PDO::FETCH_ASSOC); 

     $verificationKey = self::newVerificationKey($token); 
     // Send the file to the remote 
     $ch = curl_init(); 
     curl_setopt($ch, CURLOPT_URL, $uploadURL); 
     curl_setopt($ch, CURLOPT_POST, true); 
     $postArgs = array(
       'action' => 'updateBackground', 
       'verificationKey' => $verificationKey, 
       'file' => '@' . realpath($newFileName), 
       'oldBackground' => $queryData['backgroundImage'] 
      ); 
     curl_setopt($ch, CURLOPT_POSTFIELDS, $postArgs); 
     curl_setopt($ch, CURLOPT_SAFE_UPLOAD, 0); 
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
     $result = curl_exec($ch); 
     curl_close($ch); 
     unlink($newFileName); 

提前感謝!

回答

0

最有可能您的網絡服務器運行支持「@」的舊版本 - 但不是curlfile。本地機器支持curlfile - 而不是「@」(默認配置)...

您可以使用

if (class_exists("CurlFile")){ 
    $postArgs = array(
       'action' => 'updateBackground', 
       'verificationKey' => $verificationKey, 
       'file' => new CurlFile($newFileName), 
       'oldBackground' => $queryData['backgroundImage'] 
      ); 
}else{ 
$postArgs = array(
       'action' => 'updateBackground', 
       'verificationKey' => $verificationKey, 
       'file' => '@' . realpath($newFileName), 
       'oldBackground' => $queryData['backgroundImage'] 
      ); 
} 

這是推薦的,因爲@三通被認爲是不安全的,所以在使用CurlFile,當可用時。

然而,有@三通lokaly工作,以及,你可以使用

curl_setopt($ch, CURLOPT_SAFE_UPLOAD, false); 

此默認爲前PHP 5.5 false,但默認爲true更高版本。

請注意,玩「訂單」的周圍。似乎有一個問題,CURLOPT_POSTFIELDS beeing對現有的選項有些敏感。所以

curl_setopt($ch, CURLOPT_POSTFIELDS, $postArgs); 
curl_setopt($ch, CURLOPT_SAFE_UPLOAD, false); 

可能無法正常工作,而

curl_setopt($ch, CURLOPT_SAFE_UPLOAD, false); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $postArgs); 

威力。

+0

工作就像一個魅力。我只是完成了整個class_exists的方式,並且它完美地工作。謝謝! – Joaquim