cakephp - Cake PHP image upload -


I'm new to the cake and I need some help in uploading an image to allow me to upload an image to a user And I want to save it to the image directory (www / CakePHP / app / webroot / img /), and also want to store the file's path to the database. This is what I have so far received,

ProductsADD.ctp:

  & lt; Div class = "products form" & gt; & Lt ;? Php echo $ this- & gt; Form- & gt; Create ('product'); ? & Gt; & Lt; Fieldset & gt; & Lt; Legend & gt; & Lt ;? Php Echo __ ('Add Product Details'); ? & Gt; & Lt; / Narrative & gt; & Lt ;? Php echo $ this- & gt; Form- & gt; Input ('product_name', array ('required' = & gt; wrong)); Copy $ the-> gt; Form- & gt; Input ('product_model_number', array ('required' = & gt; wrong)); Echo $ the-> Form- & gt; Input ('product_brand', array ('required' = & gt; wrong)); Echo $ the-> Form- & gt; Input ('product_description', array ('required' = & gt; wrong)); Echo $ the-> Form- & gt; Input ('price_bronze', array ('required' = & gt; wrong)); Copy $ this- & gt; Form- & gt; Input ('price_silver', array ('required' = & gt; wrong)); Anco $ this-> Form- & gt; Input ('price_gold', array ('required' = & gt; wrong)); Copy $ this- & gt; Form- & gt; Input ('upload', array ('type' = & gt; 'file')); ? & Gt; & Lt; / Fieldset & gt; & Lt ;? Php echo $ this- & gt; Form- & gt; End (__ ('send')); ? & Gt; & Lt; / Div & gt;  

ProductsComputer:

  add public function () (if ($-is-> this is-> Request-> ('Post')) { $ This - & gt; Product-> (); Save ($ -> - (- $ ---; -> Data->) {$ this- & gt; session- & Set Flash (__ ('The product has been saved.')); $$ -> (- 'Action' => Index;); Redirect;); } And {$ this- & gt; Session-> Set Flash (__ ('The product could not be saved, please try again.')); } If (if you have uploaded the image! (Empty! ($ - this-> data)) / / If the image has been uploaded, check that (if! (Empty ($ ---> [['' '[' '' [' '' [['' Product '] [' upload '] [' name '])) {$ File = $ this-> Data [' product '] [' upload ']; // easy access $ Enter the data in a var for ext = substr (strtolower (strrchr ($ file ['name'], '.')), 1) // extension $ arr_ext = array ('jpg', 'jpeg', 'gif ') Receive // ​​set approved extension // Only process if the extension is valid (if_array ($ ext, $ arr_ext)) is the actual upload of the file {//}, the first ARG is TMP name, Ra AGR is // where we are taking it to move _loadload_file ($ file ['tmp_name'], WWW_ROOT. 'Kppp / app / webtrot / img /'. $ File ['name']); // Create a filename for database entry $ -> data ['product'] ['product_image'] = $ file ['name'];}} // now save $ $ for this-> Products-> Save ($ -> -> Data);}}}  

I do not know why the uploaded image does not save the directory.

While creating a new look with file input in the cakepip, you can type 'enctype' => 'multipart / form -Data '

The code should be:

  & lt ;? Php echo $ this- & gt; Form- & gt; Made ('product', array ('enctype' = & gt; 'multipartile / form-data')); ? & Gt;  

I hope this will work


Comments

Popular posts from this blog

import - Python ImportError: No module named wmi -

Editing Python Class in Shell and SQLAlchemy -

c# - MySQL Parameterized Select Query joining tables issue -