2012-07-20 39 views
0

我有一個圖像的網址: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控制器如同一個普通用戶來處理上傳?

謝謝

+0

http://stackoverflow.com/questions/1692186/image-upload-using-php-curl可能會幫助你。 – 2012-07-20 11:34:56

回答

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(); 



} 

如果有人知道如何發佈該文件,但不保存將是巨大的。