So far I have implemented is done below.
The problem that I am facing is i cannot update the file name and as a result I am getting an error, resulting the file from not being upload.
Controller:
public function add(){
if (!empty($this->data)) {
if (!empty($this->data['Note'])){
$filename = basename($this->data['Note']['note_image']['name']);
move_uploaded_file($this->data['Note']['note_image']['tmp_name'],WWW_ROOT . 'files' . DS . $filename);
if($this->Note->save($this->data['Note'])){
$this->Session->setFlash('The note has been saved');
$this->redirect(array('action' => 'add'));
}
}
}
}
View:
<?php
echo $this->Form->create('Note', array('type' => 'file'));
echo $this->Form->input('note_title');
echo $this->Form->input('note_desc');
echo $this->Form->input('note_image', array('type' => 'file'));
echo $this->Form->end('Add');
?>
I think save function will not be there. Is there a way to do it.
Code For Controller Should be like this :
public function add(){
if ($this->request->is('post') || $this->request->is('put')) {
$file = $this->request->data['Note']['note_image'];
if($file['error'] === UPLOAD_ERR_OK)
{
$filename = $file['name'];
if(move_uploaded_file($file['tmp_name'],WWW_ROOT . 'files' . DS . $filename))
{
$this->request->data['Note']['note_image'] = $filename;
if($this->Note->save($this->request->data)){
$this->Session->setFlash('The note has been saved');
$this->redirect(array('action' => 'add'));
}
}
}
else
{
$this->Session->setFlash('ERROR');
$this->redirect(array('action' => 'add'));
}
}
}
View Should be like this :
<?php
echo $this->Session->flash();
echo $this->Form->create('Note', array('enctype'=>'multipart/form-data'));
echo $this->Form->input('note_title');
echo $this->Form->input('note_desc');
echo $this->Form->input('note_image', array('type' => 'file'));
echo $this->Form->end('Add');
?>
Related
<!-- File: /app/View/Posts/add.ctp -->
<h1>Add Post</h1>
<?php
echo $this->Form->create('Post');
echo $this->Form->file('Post.Image');
echo $this->Form->input('Post.name', array('rows' => '3'));
echo $this->Form->end('Save Post');
?>
<!-- File: /app/Controler/PostControler.php -->
public function add() {
if ($this->request->is('post') ) {
$filename=$this->request->data('Post.Image');
$filename = "app/webroot/img/uploads/".$_FILES[$filename]['name'];(line 19)
$tempname=$this->request->data('Post.Image');
if((move_uploaded_file($_FILES[$tempname]['tmp_name'],$filename))){ (line 21)
if ($this->Post->save($this->request->data)) {
$this->Session->setFlash(__('save images successful.'));
redirect();
$this->redirect('/posts/upload');
}}else{
$this->Session->setFlash(__('not save images.'));}
}
}
the code above 2 error
Undefined index: images.jpg [APP\Controller\PostsController.php, line 19]
Undefined index: images.jpg [APP\Controller\PostsController.php, line 21]
I was getting same error, but I solved it. Here is my view and controller code related to image. The column name in my database table is "picture".
view file form input tag
<?php
echo $this->Form->file('picture', array('class'=>'form-control'));
?>
controller file :: PostController.php
public function addNewPost() {
$this->layout = false;
if ($this->request->is('post')) {
$data = $this->request->data; //echo '<pre>'; print_r($data); die;
if (!empty($data['Post']['picture']['name'])) {
$file = $data['Post']['picture'];
$ext = substr(strtolower(strrchr($file['name'], '.')), 1);
$arr_ext = array('jpg', 'jpeg', 'gif','png');
if (in_array($ext, $arr_ext)) {
move_uploaded_file($file['tmp_name'], WWW_ROOT . 'img/postimages/' . $file['name']);
$this->request->data['Post']['picture'] = $file['name'];
//echo '<pre>'; print_r($data); die;
}else{
$this->Session->setFlash(__('Please upload a valid image.'), 'default', array('id' => 'flashMessage', 'class' => 'alert alert-danger'), 'error');
}
}
$path = $data['Post']['picture']['name'];
if(isset($path)){
$data['Post']['picture'] ='postimages/' . $path;
}
$data['Post']['picture'] ='postimages/default.jpg';
//echo '<pre>'; print_r($data); die;
if ($this->Post->save($data)) {
$this->Session->setFlash(__('Congrats !!! Post Published to our blog. Thanks for posting.'), 'default', array('id' => 'flashMessage', 'class' => 'alert alert-success'), 'success');
return $this->redirect(array('controller' => 'dashboard', 'action' => 'addNewPost'));
} else {
$this->Session->setFlash(__('The Post could not be published. Please, try again.'), 'default', array('id' => 'flashMessage', 'class' => 'alert alert-danger'), 'error');
}
}
}
i have small problem so i need some help. maybe its very easy but i just need a push... so i am withing one controller
public function edit($id = null) {
if (!$this->TypologyPicture->exists($id)) {
throw new NotFoundException(__('Invalid typology picture'));
}
if ($this->request->is(array('post', 'put'))) {
if(empty($this->data['TypologyPicture']['pic_path']['name'])){
unset($this->request->data['TypologyPicture']['pic_path']);
}
if ($this->TypologyPicture->save($this->request->data)) {
$this->Session->setFlash(__('The typology picture has been saved.'));
return $this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The typology picture could not be saved. Please, try again.'));
}
} else {
$options = array('conditions' => array('TypologyPicture.' . $this->TypologyPicture->primaryKey => $id));
$this->request->data = $this->TypologyPicture->find('first', $options);
//$options1 = array('conditions' => array('Typology.id' => $id));
$opt = array('conditions' => array('Typology.id' => $this->request->data['TypologyPicture']['typology_id']));
$this->request->data = $this->Typology->find('first', $opt);
}
if ( AuthComponent::user('role')==='admin' ||AuthComponent::user('role')==='superadmin' ){ //if the user is admin or superadmin, show all on dropdown
$items = $this->Typology->TypologyItem->find('list');
} else {// else if the user is author, show only item created by him.
$items = $this->Typology->TypologyItem->find('list', array('conditions' => array('TypologyItem.user_id' => AuthComponent::user('id'))));
}
$typologyCategories = $this->Typology->TypologyCategory->find('list');
$typologyConditions = $this->Typology->TypologyCondition->find('list');
$users = $this->Typology->TypologyUser->find('list');
$this->set(compact('items', 'typologyCategories', 'typologyConditions', 'users'));
if ( AuthComponent::user('role')==='admin' ||AuthComponent::user('role')==='superadmin' ){
$typologies = $this->TypologyPicture->ItemTypologyPicture->find('list');
} else {
$typologies = $this->TypologyPicture->ItemTypologyPicture->find('list', array('conditions' => array('ItemTypologyPicture.user_id' => AuthComponent::user('id'))));
}
$this->set(compact('typologies'));
}
so as you see from contact controller i want to access the contact that i want to edit and its its pictures that are stored in contact_picture table. contact by itself has like an icon or an avatar, and in contact picture are stored the gallery. so here the problem is that i get all the data as it supposed to, but the image of the contact (avatar, or icon) doesent dispay, the path is retrived correctly but it just doesent display the image.
So what im asking is that if there is another way or easy way or even better way to do that i would really appruciate it...really.
Thanks in advance!
EDIT: The View Part:
<?php echo $this->Form->create('TypologyPicture', array('type'=>'file')); ?>
<legend><?php echo __('Edit Typology Picture'); ?></legend>
<?php
$dir = "/img/uploads/typology/images/";
echo $this->Form->input('id'); ?>
<?php echo $this->Form->input('Typology.item_id',array('empty'=>true)); ?>
<?php echo $this->Form->input('Typology.title'); ?>
<?php echo $this->Form->input('Typology.description');?>
<?php echo $this->Form->input('Typology.thumbnail',array('type'=>'file')); ?>
<?php echo $this->Form->input('Typology.typology_category_id',array('empty'=>true)); ?>
<?php echo $this->Form->input('Typology.typology_condition_id',array('empty'=>true)); ?>
<?php echo $this->Form->input('Typology.price',array('placeholder'=>'Price')); ?>
<!-- Here is the second part of the update -->
<?php echo $this->Form->input('pic_path', array('label'=>'Picture','type'=>'file'));
echo $this->Form->input('hiddenimage', array('type'=>'hidden','value'=> $this->Form->value('pic_path') ));
$Image = $this->Form->value( 'pic_path');
if(empty($Image) || $Image==NULL)
{$Image = "/img/uploads/noimg.jpg";}
else {$Image = $dir . $Image; }
echo $this->Html->image($Image,array('align'=>'absbottom','style'=>'max-height:100px'));
?>
<?php echo $this->Form->end(__('Submit')); ?>
So when i do echo to the image it doesent display correctly... if i remove the typology model part like a normal edit, it displays normal...
try by removing the img part from $dir and $Image;
$dir = "/img/uploads/typology/images/";
should be
$dir = "uploads/typology/images/";
and
{$Image = "/img/uploads/noimg.jpg";}
should be
{$Image = "uploads/noimg.jpg";}
in this editdrprofile.ctp file not retrieves gender field value and when am click save Drprofile link in editprofile page no action donne page refreshing no image uploaded nothing changed
app/Controller/DashboardsController.php
public function index() {
$this-> loadModel('Drprofile');
$this->set('variable', $this->Drprofile->find('all', array('conditions' => array('Drprofile.user_id' => $this->Auth->user('id')))));
}
public function editdrprofile($id = null) {
$this-> loadModel('Drprofile');
if (!$id) {
throw new NotFoundException(__('Invalid post'));
}
$post = $this->Drprofile->findByuser_id($id);
if (!$post) {
throw new NotFoundException(__('Invalid post'));
}
if ($this->request->is(array('Drprofile', 'put'))) {
$this->Drprofile->user_id = $id;
// $this->set('posts', $this->carrier->find('all'));
if($this->request->is('post')){
Configure::read();
// pr($this->data);
$this->Carrier->create();
$filename = null;
if (
!empty($this->request->data['Drprofile']['image']['tmp_name'])
&& is_uploaded_file($this->request->data['Drprofile']['image']['tmp_name'])
) {
// Strip path information
$filename = basename($this->request->data['Drprofile']['image']['name']);
move_uploaded_file(
$this->data['Drprofile']['image']['tmp_name'],
WWW_ROOT . DS . 'documents' . DS . $filename
);
//$this->data['Carrier']['Resume'] = $filename;
}
//pr($filename);
// Set the file-name only to save in the database
$this->request->data['Drprofile']['image'] = $filename;
pr($this->data);
if ($this->Drprofile->save($this->request->data)) {
// ...
/*if ($this->Carrier->save($this->request->data)) {
if ($this->Carrier->save($this->data)) {
*/
$this->Session->setFlash(__('Your Details has been saved.'));
return $this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('Unable to add your Details'));
}
}
/*pr_('$this->Drprofile->user_id = $id');
if ($this->Drprofile->save($this->request->data)) {
//$this->Drprofile->save($this->request->data);
$this->Session->setFlash(__('Your post has been updated.'));
return $this->redirect(array('action' => 'index'));
}
$this->Session->setFlash(__('Unable to update your post.'));*/
}
if (!$this->request->data) {
$this->request->data = $post;
}
}
in model
app/model/Drprofile.php
<?php class Drprofile extends AppModel {
var $belongsTo = array(
'User' => array(
'className' => 'User',
'foreignKey' => 'user_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
);
}
?>
in view/dashboards/index.ctp
<?php
foreach ($variable as $post1):
?>
<table>
<tr><h3>Doctor Profile</h3></tr>
<tr> <td>TTTTTTTTTTTTTTTTTTTTTTTTTTTT</td> <td><table>
<tr><td>Name</td><td><?php echo $post1['User']['fullname'];?></td></tr>
<tr><td>Email</td><td><?php echo $post1['User']['email'];?></td></tr>
<tr><td>Mobile</td><td><?php echo $post1['User']['contactnumber'];?></td></tr>
<tr><td>Gender</td><td><?php echo $post1['User']['gender'];?></td></tr>
<tr><td>D.O.b</td><td><?php echo $post1['Drprofile']['dob'];?></td></tr>
<tr><td>Experience</td><td><?php echo $post1['Drprofile']['exp'];?></td></tr>
</table></td></tr>
</table>
<?php
echo $this->Html->link(
'Edit Profile', array('action' => 'editdrprofile', $post1['Drprofile']['user_id'])
);
?>
<?php
endforeach; ?>
app/view/editdrprofile.ctp
<h1>Edit Post</h1>
<?php
echo $this->Form->create('Drprofile');
?>
<table>
<tr><h3>Edit profile</h3></tr>
<tr><td>Name</td><td><?php echo $this->Form->text('User.fullname'); ?></td></tr>
<tr><td>Email</td><td><?php echo $this->Form->text('User.email'); ?></td></tr>
<tr><td>Mobile</td><td><?php echo $this->Form->text('User.contactnumber'); ?></td></tr>
<tr><td>Gender</td><td><?php
$options=array('M'=>'Male','F'=>'Female');
$attributes=array('legend'=>false);
echo $this->Form->radio('User.gender',$options,$attributes);
?></td></td></tr>
<tr><td>D.O.b</td><td><?php echo $this->Form->text('dob'); ?></td></tr>
<tr><td>Experience</td><td><?php echo $this->Form->select('exp', array('options' => array('1 year','2 years ','3 years','4 years','5-10 years'))); ?></td></tr>
<tr><td><?php echo $this->Form->input('drprofile.Resume', array('between'=>'<br />','type'=>'file'));?></td></tr>
<tr><td><?php echo $this->Form->end('Save Drprofile');?></td></tr>
<?php /*?><?php echo $this->Form->input('id', array('type' => 'hidden'));?><?php */?>
</table>
First thing you are doing this in Dashboards controller, and you are creating data for Drprofile. If you in the end you want to do it in DashboardsController then you should change your from to this:
echo $this->Form->create('Drprofile', array(
'url' => array('controller' => 'dashboards', 'action' => 'editdrprofile')
));
This way you are telling form what action to use. But I would suggest you move that to DprofilesController and edit that data there.
One more thing, you closed your form there and you place $this->Form->input for id after it, change that.
I am learning Cakephp framwork. I am having problem while I am trying to update database record.
This is Controller code for edit post....
$this->loadModel('Post');
if($this->request->is('put')):
$this->Post->id = $this->params['id'];
if($this->Post->save($this->request->data)):
$this->Session->setFlash(__('Page has been edited'));
$this->redirect('/User/index');
endif;
else:
$this->set('postinfo', $this->Post->findById($this->params['id']));
endif;
}
This is view/edit.ctp file
echo $this->Form->update('Post', array(
'method' => 'put'
));
echo $this->Form->input('title',array('type' => 'text','value'=>$postinfo['Post']['title']));
echo $this->Form->input('body', array('type' => 'textarea','value' => $postinfo['Post']['body']));
echo $this->Form->submit('Submit', array('class'=>'btn btn-primary'));
echo $this->Form->end();
But this code does not update record in database...I tried everything from book.cakephp.org tutorial and other tutorials related to cakephp..
I hope I'll get some help from you guys :)
If this is PostController, than you don't need to call the $this->loadModel('Post'); function.
In view you need a hidden field with id of post.
Controller code for edit post
public function edit($id = null) {
if (!$id) {
throw new NotFoundException(__('Invalid post'));
}
$post = $this->Post->findById($id);
if (!$post) {
throw new NotFoundException(__('Invalid post'));
}
if ($this->request->is('post') || $this->request->is('put')) {
$this->Post->id = $id;
if ($this->Post->save($this->request->data)) {
$this->Session->setFlash('Your post has been updated.');
$this->redirect(array('action' => 'index'));
}
else {
$this->Session->setFlash('Unable to update your post.');
}
}
if (!$this->request->data) {
$this->request->data = $post;
}
}
view/edit.ctp file
<h1>Edit Post</h1>
<?php
echo $this->Form->create('Post');
echo $this->Form->input('title');
echo $this->Form->input('body', array('rows' => '3'));
echo $this->Form->input('id', array('type' => 'hidden'));
echo $this->Form->end('Save Post');
?>
I am facing a strange problem while creating edit functionality in cakephp 2.1
Error genreated:
Illegal offset type [CORE\Cake\Model\Model.php, line 2689]
My edit.ctp file is
<?php echo $this->Form->create('Task');?>
<fieldset>
<legend>Edit Task</legend>
<?php
echo $this->Form->hidden('id');
echo $this->Form->input('title');
echo $this->Form->input('done');
?>
</fieldset>
<?php echo $this->Form->end('Save');?>
Model: Task.php
<?php
class Task extends AppModel {
var $name = 'Task';
}
?>
Controller :TasksController.php
<?php
class TasksController extends AppController {
var $name = 'Tasks';
var $helpers = array('Html', 'Form');
function index() {
$this->set('tasks', $this->Task->find('all'));
}
function add() {
if (!empty($this->data)) {
$this->Task->create();
if($this->Task->save($this->data)){
$this->Session->setFlash('The Task has been saved');
$this->redirect(array('action'=>'index'),null,true);
}else{
$this->Session->setFlash('Task not saved.Try again.');
}
}
}
function edit($id = null) {
if (!$id) {
$this->Session->setFlash('Invalid Task');
$this->redirect(array('action' => 'index'), null, true);
}
if (empty($this->data)) {
$this->data = $this->Task->find(array('id' => $id));
} else {
if ($this->Task->save($this->data)) {
$this->Session->setFlash('The Task has been saved');
$this->redirect(array('action' => 'index'), null, true);
} else {
$this->Session->setFlash('The Task could not be saved.Please, try again.');
}
}
}
}
?>
I think your find() method is erroneous:
$this->data = $this->Task->find(array('id' => $id));
change to
$this->data = $this->Task->find('all', array('conditions' => array('id' => $id)));
http://book.cakephp.org/2.0/en/models/retrieving-your-data.html
In order to prepopulate the data on the form you need to do the following:
<?php echo $this->Form->create('Task');?>
<fieldset>
<legend>Edit Task</legend>
<?php
echo $this->Form->hidden('id', array('value' => $this->data[0]['Task']['id']));
echo $this->Form->input('title', array('value' => $this->data[0]['Task']['title']));
echo $this->Form->input('done', array('value' => $this->data[0]['Task']['done']));
//var_dump($this->data[0]['Task']['id']);
?>
</fieldset>
<?php echo $this->Form->end('Save');?>
<?php echo $this->Html->link('List All Tasks', array('action'=>'index')); ?><br />
<?php echo $this->Html->link('Add Task', array('action'=>'add')); ?><br />
<?php echo $this->Html->link('List Done Tasks', array('action'=>'index')); ?><br />
<?php echo $this->Html->link('List Pending Tasks', array('action'=>'index')); ?><br />