开发者

Using HTTP to Add files [closed]

开发者 https://www.devze.com 2023-04-11 06:44 出处:网络
Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed. This question is off-topic. It is not currently accepting answers.

Want to improve this question? Update the question so it's on-topic for Stack Overflow.

Closed 11 years ago.

Improve this question

I'm using Luracast's Restler Framework which is great. But I was wondering if someone could tell me how I can upload files through HTTP.

I was using a simple HTML Form to POST data to the API, and trying to grabe the file information from $_FILES, but i'm not getting anything.

Here is my super simple form

    <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开发者_StackOverflow社区="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>

Here is my class that works with 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 

?>


You can only upload files if the form data is sent as multipart/form-data. The default is application/x-www-form-urlencoded.

From the specification:

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

精彩评论

暂无评论...
验证码 换一张
取 消