2012-04-05 70 views
0

我做了一个REST服务,它主要基于this tutorial。我还制作了一个基于this tutorial的REST请求库。 (基本上,switch上的一堆$_SERVER['REQUEST_METHOD'])。 api也用cURL提出请求。

protected function executePost ($ch) 
{ 
    if (!is_string($this->requestBody)) 
    { 
     $this->buildPostBody(); 
    } 

    curl_setopt($ch, CURLOPT_POSTFIELDS, $this->requestBody); 
    curl_setopt($ch, CURLOPT_POST, 1); 

    $this->doExecute($ch); 
} 

protected function doExecute (&$curlHandle) 
{ 
    $this->setCurlOpts($curlHandle); 
    $this->responseBody = curl_exec($curlHandle); 
    $this->responseInfo = curl_getinfo($curlHandle); 

    curl_close($curlHandle); 
} 

我有2个简单的HTML表单,一个用get方法,另一个用于post方法。当我使用简单的输入文本中的一个时,工作正常。我得到/返回服务中的值没有问题。

但我需要从HTML表单发送图像,在我的服务中接收它,然后将其保存在服务器上。

下面是我午餐查询服务的部分。

print_r($_FILES); 
//move_uploaded_file($_FILES["image1"]["tmp_name"], "Images/" . $_FILES["image1"]["name"]); when uncommented, This line actually works and save the image in my folder. 

include("RestUtils.php"); 

$request = new RestRequestOperator('http://localhost:8080/REST/RestControler.php/user/1', 'POST', $_FILES); 
$request->execute(); 

在我的服务中,我收到图像信息,它们是tmp_name和名称。当我尝试使用move_uploaded_file保存图像(以及正确的参数,它不起作用)。

我意识到有一种魔法会在图像文件中保存一段“短时间” 。TMP文件夹时,我打电话给我的服务,图像已被删除

结束语:?我不知道是否可以将图像发送到PHP REST API,他们保存他们的服务器上

编辑:我在服务中加入了

if(file_exists($_POST["image1"]["tmp_name"])){ 
    echo "file EXISTS<br>"; 
}else echo "NOPE.<br>"; 

if(is_uploaded_file($_POST["image1"]["tmp_name"])){ 
    echo "is_uploaded_file TRUE<br>"; 
}else echo "is_uploaded_file FALSE<br> ."; 

if(move_uploaded_file($_POST["image1"]["tmp_name"], "Images/" .$_POST["image1"]["name"])){ 
    echo "move_uploaded_file SUCCESS "; 
}else echo "NOT move_uploaded_file"; 

输出s:file EXISTS,is_uploaded_file FALSE,NOT move_uploaded_file 这意味着该文件实际上仍然存在于服务中,但上传的文件返回False,可能是因为我在服务中使用POST数组而不是$ _FILES数组,这在我的空服务.......

回答

1

发现它,在本地wamp PHP 5.3.4上运行!

public static function processRequest() 
{ 
    case 'post': 
     if(move_uploaded_file($_FILES["uploaded_file"]["tmp_name"] , "Images/" . $_FILES["uploaded_file"]["name"])){ 
        echo "move_uploaded_file SUCCESS "; 
    ......................................     
} 
......................................   
protected function executePost ($ch) 
{ 
    $tmpfile = $_FILES['image1']['tmp_name']; 
    $filename = basename($_FILES['image1']['name']); 

    $data = array(
     'uploaded_file' => '@' . $tmpfile . ';filename='.$filename, 
    ); 
    curl_setopt($ch, CURLOPT_POST, 1);    
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data); 
    //no need httpheaders 
    $this->doExecute($ch); 
} 

谢谢。戴夫

0

检查您的POST的编码类型。在一个表单中它需要是“multipart/form-data”,所以我假设你的POST REST调用需要有一个类似的编码类型才能使文件上传正常工作。