0

画像付きの URL があります: http://www.example.com/image.jpg
と、ユーザー入力によってファイルをサーバーにアップロードするアクションがコントローラーにあります。これと同じアクションを使用して、URL でファイルをアップロードしたいと考えています。画像からデータを取得し始めました:

$url = "http://www.example.com/image.jpg";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
$rawdata=curl_exec($ch);
curl_close ($ch);

$rawdata をコントローラーに送信して、通常のユーザーアップロードのように処理するにはどうすればよいですか?

ありがとう

4

1 に答える 1

0

私は自分でそれを理解しました。

$url = "http://www.example.com/image.jpg";
$filename = basename($url, rand_string(7));
$path = realpath("../webroot/uploads/")."/".$filename;


$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
$rawdata=curl_exec($ch);
curl_close ($ch);

if(file_exists($path)){
    unlink($path);
}

$file = fopen($path, 'x');

fwrite($file, $rawdata);
fclose($file);


$post_data['Filedata'] = '@'.$path;
$url = "http://localhost/controller/action";
$ch = curl_init();
// Set URL on which you want to post the Form and/or data
curl_setopt($ch, CURLOPT_URL, $url);
// Data+Files to be posted
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
// Pass TRUE or 1 if you want to wait for and catch the response against the request made
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// For Debug mode; shows up any error encountered during the operation
curl_setopt($ch, CURLOPT_VERBOSE, 1);
// Execute the request
$response = curl_exec($ch);

echo $response;

コントローラーで:

function action(){

    //get properties of the file
    //$_FILES['Filedata']['name'];
    //$_FILES['Filedata']['size'];
    //$_FILES['Filedata']['tmp_name'];
    //savefile();



}

ファイルを保存せずに投稿する方法を誰かが知っていれば、それは素晴らしいことです。

于 2012-07-20T13:31:45.420 に答える