2011-10-07 38 views
0

我正在使用Luracast的Restler框架,它非常棒。但我想知道如果有人能告诉我如何通过HTTP上传文件。使用HTTP来添加文件

我正在使用一个简单的HTML表单将数据发布到API,并试图从$ _FILES中获取文件信息,但我没有收到任何东西。

这里是我的超级简单的形式

<form method="post" action="index.php/product"> 
    <p> 
     <label>Product name</label> 
     <input name="product_name" /> 
    </p> 

    <p> 
     <label>MSRP Price</label> 
     <input name="msrp_price" /> 

    </p> 


    <p> 
     <label>Category</label> 
     <input name="category_name" /> 
    </p> 


    <p>Teir Pricing</p> 


    <p> 
    <label>Price</label> 
    <input name="price[]" /> 
    </p> 


    <p> 
    <label>Buy Range Min</label> 
    <input name="buy_range_min[]" /> 
    </p> 

    <p> 
    <label>Buy Range Max</label> 
    <input name="buy_range_max[]" /> 

    <p> 
    <label>Price</label> 
    <input name="price[]" /> 
    </p> 

    <p> 
    <label>Buy Range Min</label> 
    <input name="buy_range_min[]" /> 
    </p> 

    <p> 
    <label>Buy Range Max</label> 
    <input name="buy_range_max[]" /> 
    </p> 


    <p> 
    <label>Image</label> 
    <input type="file" name="image" /> 
    </p> 

    <input type="submit" /> 

</form> 

这里是我的课,与Restler工作

<? 



class Product { 
     public $dp; 
     private $DBH; 
     public $highest_max = 0; 


     function __construct() { 

      $host = 'localhost'; 
      $db_name = ''; 
      $db_user = ''; 
      $db_password = ''; 

       try { 
        $this ->DBH = new  PDO('mysql:host='.$host.';dbname='.$db_name, $db_user, $db_password); 

        // Line takes care of error reporting. 
        $this->DBH->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 
       } 
       catch(PDOException $e) { 
        // return $e->getMessage(); 

        return 'Sorry there was an issue'; 
       } 
     } // end function 


     function get($id=NULL) { 
      if (is_null($id)) { 
       /*** The SQL SELECT statement ***/ 
       $sql = "SELECT * FROM products"; 

       $data = array('product_id' => $id); 

      $STH = $this->DBH->prepare($sql); 
      // binds the params 
      $STH->execute($data); 
      // wHAT TYPE OF DATA WE ARE GRABING 
      $STH->setFetchMode(PDO::FETCH_ASSOC); 

       // GO TRough IT ALL 
       while($row = $STH->fetch()) { 
        $rows[] = $row; 
       } // end while 
       return $rows; 
      } // end if 

      else { 
       $sql = "SELECT * FROM products WHERE product_id = :product_id"; 

       $data = array('product_id' => $id); 

      $STH = $this->DBH->prepare($sql); 
      // binds the params 
      $STH->execute($data); 
      // wHAT TYPE OF DATA WE ARE GRABING 
      $STH->setFetchMode(PDO::FETCH_ASSOC); 

       $row = $STH->fetch(); 
       return $row; 

      } // end else 
     } // end function 



      function add_teir_pricing($price, $buy_range_min, $buy_range_max, $product_id) { 

       // check to see if the min is higher then this max 
       if ($buy_range_min >= $buy_range_max) { 

        throw new RestException(417,'Your min price teir must be smaller then your max'); 
       } // end if 


       elseif ($buy_range_min <= $this->highest_max) { 
        throw new RestException(417,'One of your minimum price teirs cannot overlap with another.'); 
       } // end if 


       $this->highest_max = $buy_range_max; 


      # the data we want to insert 
$data = array('price' => $price, 'buy_range_min' => $buy_range_min, 'buy_range_max' =>  $buy_range_max, 'product_id' => $product_id); 


      $sql = "INSERT INTO teir_pricing (price, buy_range_min, buy_range_max, product_id, created) value (:price, :buy_range_min, :buy_range_max, :product_id, NOW())"; 


      $STH = $this->DBH->prepare($sql); 


      $STH->execute($data); 


    } // end function 

     function post($product_id=NULL,$member_id, $product_name, $upc_code, $sku, $global_trade_item_number, $link_to_product_reviews, 
        $url_to_product, 
        $msrp_price, 
        $category_name, $price, $buy_range_min, $buy_range_max) { 

        // ADD PRODUCT 
        if (!isset($product_name)) { 
         $error = true; 
         // $errors['message'][] = 'Mising a product_name'; 
         throw new RestException(417,'Mising a product_name'); 
        } // end if 

        if (!isset($msrp_price)) { 
         $error = true; 
         // $errors['message'][] = 'Mising a msrp_price'; 
         throw new RestException(417,'Missing MSRP price'); 


        } // end if 


        if (!isset($category_name)) { 
         $error = true; 
         // $errors['message'][] = 'You must assign a category_name to this product'; 
         throw new RestException(417,'You must assign a category_name to this product'); 
        } // end if 


      // We still need to grab the member id from the key when this is added.   
      $member_id = 1; 





      $product_data = array('member_id' => $member_id, 
            'product_name' => $product_name, 
            'upc_code' => $upc_code, 
            'sku' => $sku, 
            'global_trade_item_number' => $global_trade_item_number, 
            'link_to_product_reviews' => $link_to_product_reviews, 
            'url_to_product' => $url_to_product, 
            'msrp_price' => $msrp_price, 
            'category_name' => $category_name); 


      $sql = "INSERT INTO 
        products 
        (product_name, 
        upc_code, 
        sku, 
        global_trade_item_number, 
        link_to_product_reviews, 
        url_to_product, 
        member_id, 
        msrp_price, 
        created, 
        category_name) 

        VALUES 
        (:product_name, 
        :upc_code, 
        :sku, 
        :global_trade_item_number, 
        :link_to_product_reviews, 
        :url_to_product, 
        :member_id, 
        :msrp_price, 
        NOW(), 
        :category_name 
        )"; 

      $q = $this->DBH->prepare($sql); 

      $q->execute($product_data); 

      $product_id = $this->DBH->lastInsertId(); 



      foreach($price as $key => $value) { 
       Product::add_teir_pricing($price[$key], $buy_range_min[$key], $buy_range_max[$key], $product_id); 

      } // end foreach 

$response = array('product_id' => $product_id, 'status' => 'success', 'message' => 'Your product has been added', 'files' => $_FILES); 

      return $response;   
    } // end function 


    function upload_image($_FILES) { 

     return $_FILES; 

    } // end function 

} // end class 

?>

+1

我们无法知道你做错了什么,如果你不告诉我们你的代码。 –

+0

刚刚用代码更新了帖子。 – Gamak

+0

如果你想使用REST上传文件,那么你应该使用PUT,而不是POST,如果你想使用PUT,那么你不能使用简单的HTML表单,因为他们只做POST和GET。 – Quentin

回答

0

如果表单数据作为发送您只能上传文件multipart/form-data。缺省值是application/x-www-form-urlencoded。

the specification

<FORM action="http://server.com/cgi/handle" 
     enctype="multipart/form-data" 
     method="post"> 
+0

你是对的...我被困惑了,我将它添加到表单中,但是我不能访问我的方法中的任何参数,称为post。 – Gamak

+0

其实不,我很笨。这是我唯一的问题。为什么我应该在POST上使用PUT? – Gamak

+0

由于PUT是“在此处上传文件”的HTTP动词,而REST全部是关于正确使用HTTP动词的。 – Quentin