Note that there are some explanatory texts on larger screens.

plurals
  1. POError saving Item in cakephp when I upload an image within that item
    primarykey
    data
    text
    <p>I am working in cakephp for the first time. I was able to get started with a Product model that has an image variable. I am able to upload the image but then I am not able to save to product afterwards. This is my add.ctp</p> <pre><code>&lt;div class="products form"&gt; &lt;?php echo $this-&gt;Form-&gt;create('Product', array('enctype' =&gt; 'multipart/form-data')); ?&gt; &lt;fieldset&gt; &lt;legend&gt;&lt;?php echo __('Add Product'); ?&gt;&lt;/legend&gt; &lt;?php echo $this-&gt;Form-&gt;input('name'); echo $this-&gt;Form-&gt;input('slug'); echo $this-&gt;Form-&gt;input('description'); echo $this-&gt;Form-&gt;input('price'); echo $this-&gt;Form-&gt;input('weight'); //upload image echo $this-&gt;Form-&gt;input('Product.image', array('type'=&gt;'file', 'tmp_name'=&gt;'temp')); ?&gt; &lt;/fieldset&gt; &lt;?php echo $this-&gt;Form-&gt;end(__('Submit')); ?&gt; &lt;/div&gt; &lt;div class="actions"&gt; &lt;h3&gt;&lt;?php echo __('Actions'); ?&gt;&lt;/h3&gt; &lt;ul&gt; &lt;li&gt;&lt;?php echo $this-&gt;Html-&gt;link(__('List Products'), array('action' =&gt; 'index')); ?&gt;&lt;/li&gt; &lt;/ul&gt; &lt;/div&gt; </code></pre> <p>And here is my ProductsController add() funciton</p> <pre><code> public function add($seller_id = null) { if ($this-&gt;request-&gt;is('post')) { $this-&gt;Product-&gt;create(); debug($this-&gt;request-&gt;data); if(isset($this-&gt;request-&gt;data["Product"]["image"]["name"])) { $file = new File($this-&gt;request-&gt;data["Product"]["image"]["name"]); debug($this-&gt;request-&gt;data); $ext = pathinfo(($this-&gt;request-&gt;data["Product"]["image"]["name"]), PATHINFO_EXTENSION); if ($ext != 'jpg' &amp;&amp; $ext != 'jpeg' &amp;&amp; $ext != 'gif' &amp;&amp; $ext != 'png') { $this-&gt;Session-&gt;setFlash('You may only upload image files.'); }else { if(move_uploaded_file($this-&gt;request-&gt;data["Product"]["image"]["tmp_name"],WWW_ROOT."/img/uploads/" . $this-&gt;data["Product"]["image"]["name"]) == true) { $this-&gt;data["Product"]["image"] = $this-&gt;data["Image"]["image"]["name"]; } } if ($this-&gt;Product-&gt;save($this-&gt;request-&gt;data)) //error here { $this-&gt;Session-&gt;setFlash(__('The product has been saved')); $this-&gt;redirect(array('action' =&gt; 'index')); $this-&gt;request-&gt;data['Product']['seller_id'] = $seller_id; } else { $this-&gt;Session-&gt;setFlash(__('The product could not be saved. Please, try again.')); } }$this-&gt;Session-&gt;setFlash(__('The image could not be saved. Please, try again.')); } } </code></pre> <p>Can anyone see why it would upload and move the image but not save the Product? Also, here is what is produced from debug($this->request->data);</p> <pre><code>array( 'Product' =&gt; array( 'name' =&gt; '8', 'slug' =&gt; '8', 'description' =&gt; '8', 'price' =&gt; '8', 'weight' =&gt; '8', 'image' =&gt; array( 'name' =&gt; 'celeb16.com-purple-strapless-short-bridesmaid-dress-g129-33(1).jpg', 'type' =&gt; 'image/jpeg', 'tmp_name' =&gt; 'C:\xampp\tmp\phpDD08.tmp', 'error' =&gt; (int) 0, 'size' =&gt; (int) 1404 ) ) ) </code></pre> <p>one more piece of information that might be helpful is that when I use this code:</p> <pre><code>&lt;?php echo $this-&gt;Form-&gt;create('Product', array('enctype' =&gt; 'multipart/form-data')); ?&gt; </code></pre> <p>it will allow the image to properly upload but will not save the product and when I use this code:</p> <pre><code> &lt;?php echo $this-&gt;Form-&gt;create('Product'); ?&gt; </code></pre> <p>it will save the product but not upload the image</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload