0

次のhtmlコードを含むファイルをphpファイルに送信しています。私がやりたいのは、phpでそのファイルを取得し、そのファイルをポストフィールドとしてのcURLのパラメーター値として設定し、URLを実行することです。どうすればよいですか?

これがhtmlです:

   <form name="frm" id="frm" method="post" action="fileSubmit.php" enctype="multipart/form-data">
      <input type="file" name="file" id="file"/>
      <input type="submit" name="submit" value="submit" />
   </form>

これがphpです

<?php 
if(isset($_REQUEST['submit'])) { 
  $curl = curl_init("myDomain/submitFile";); 
  $file = "file=".file_get_contents($_FILES["file"]["name"]);
  curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); 
  curl_setopt($curl, CURLOPT_HEADER, 0); 
  curl_setopt($curl, CURLOPT_TIMEOUT, 60); 
  curl_setopt($curl, CURLOPT_POST, 0);
  curl_setopt($curl, CURLOPT_POSTFIELDS, $file); 
  $resp = curl_exec($curl); 
  curl_close($curl); 
} 
?>

前もって感謝します!

4

2 に答える 2

1

試す

$_FILES["file"]["tmp_name"]

代わりに、アップロードしたファイルを移動するまで、一時的な場所に保存されます

于 2012-08-23T14:20:33.337 に答える
1

ここで修正が必要なことがいくつかあります...コードを次のように変更します。

$postParams = array(
    "@file" => $_FILES["file"]["tmp_name"],
    "name"  => $_FILES["file"]["name"]
);
$curl = curl_init("http://remote-site.com/upload-script.php"); 
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($curl, CURLOPT_HEADER, 0); 
// curl_setopt($curl, CURLOPT_TIMEOUT, 60); // yes, this must be commented out. It will stop the upload otherwise.
curl_setopt($curl, CURLOPT_POST, 1); // post must be enabled.
curl_setopt($curl, CURLOPT_POSTFIELDS, $postParams); 
$resp = curl_exec($curl); 
curl_close($curl); 

ここでテストされ、完全に機能します。

于 2014-01-30T02:06:24.850 に答える