i am trying to display an image using cakephp and mysql..my code is running but it is not displaying an image
uploadsController is::
<?php
class UploadsController extends AppController{
var $name='Uploads';
function share_video($stu_id=null)
{
$this->set('stu',$stu_id);
$tut=$this->Session->read('tutor_id');
$data=$this->Upload->query("select * from tutors AS Tutor where tutor_id='$tut'");
$this->set('val1',$data);
if (!empty($this->data)) {
$this->Upload->create();
if ($this->uploadFile() && $this->Upload->save($this->data)) {
$this->Session->setFlash(__('The upload has been saved', true));
//$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The upload could not be saved. Please, try again.', true));
}
}
$data=$this->Upload->find('all');
$this->set('val2',$data);
$this->layout='tutor';
}
function uploadFile() {
$id = mysql_insert_id();
$file = $this->data['Upload']['file'];
if ($file['error'] === UPLOAD_ERR_OK) {
//$id = String::uuid();
if (move_uploaded_file($file['tmp_name'], APP.'webroot/uploads'.DS.$file['name'])) {
$this->request->data['Upload']['upload_id'] = $id;
$this->request->data['Upload']['filename'] = $file['name'];
$this->request->data['Upload']['filesize'] = $file['size'];
$this->request->data['Upload']['filemime'] = $file['type'];
return true;
}
}
return false;
}
function download($filename=null)
{
$data=$this->Upload->find('all',array('conditions'=>array('filename'=>$filename)));
$this->set('val1',$data);
}
}
?>
and my download.ctp file is:
Html->image('uploads/'.$val1[0]['Upload']['filename']);
//echo 'Html->url(array('controller' => 'uploads','action' => 'download',$val1[0]['Upload']['filename'])).'" width="100" height="100"/>';
?>
if (move_uploaded_file($tmp_name['tmp_name'],WWW_ROOT."/img/uploads/".$file['name'])) { /* conditions*/ }
$data=$this->Upload->find('first',array('conditions'=>array('filename'=>$filename)));
echo $this->Html->image('uploads/'.$val1['Upload']['filename'],array('class'=>'media-object img img-thumbnail','alt'=>$val1['Upload']['filename']) );
Related
I am using cakephp 3.3. I am able to upload single image but it is not getting saved under webroot/uploads. I want to upload multiple images and save it. How can I achieve it? Please provide some inputs. I am very new to PHP programming and this framework.Thanks!
`Images\add.ctp
<?= $this->Form->create($image,['type' => 'file']) ?>
<fieldset>
<legend><?= __('Add Image') ?></legend>
<?php
echo $this->Form->input('path',['type' => 'file']);
?>
</fieldset>`
ImagesController\add
public function add()
{
$image = $this->Images->newEntity();
if ($this->request->is('post')) {
$image = $this->Images->patchEntity($image, $this->request->data);
if ($this->Images->save($image)) {
$this->Flash->success(__('The image has been saved.'));
return $this->redirect(['action' => 'index']);
} else {
$this->Flash->error(__('The image could not be saved. Please, try again.'));
}
}
$properties = $this->Images->Properties->find('list', ['limit' => 200]);
$this->set(compact('image', 'properties'));
$this->set('_serialize', ['image']);
}
Table\ImageTable.php
$validator
->requirePresence('path', 'create')
->notEmpty('path')
->add('processImageUpload', 'custom', [
'rule' => 'processImageUpload'
]);
public function processImageUpload($check = array()) {
if(!is_uploaded_file($check['path']['tmp_name'])){
return FALSE;
}
if (!move_uploaded_file($check['path']['tmp_name'], WWW_ROOT . 'img' . DS . 'images' . DS . $check['path']['name'])){
return FALSE;
}
$this->data[$this->alias]['path'] = 'images' . DS . $check['path']['name'];
return TRUE;
}
In add.ctp make input field something like:
echo $this->Form->input('path[]',['type' => 'file','multiple'=>'multiple']);
And in controller make save method something like this:
// $image = $this->Images->newEntity();
$images= $this->Articles->newEntities($this->request->data);
if ($this->request->is('post')) {
// $image = $this->Images->patchEntity($image, $this->request->data);
if ($this->Images->saveMany($images)) {
$this->Flash->success(__('The image has been saved.'));
return $this->redirect(['action' => 'index']);
} else {
$this->Flash->error(__('The image could not be saved. Please, try again.'));
}
}
if I click on login button, it is taking empty input values since the condition is directly going into else part of cakephp.
Below is the code:
if($this->request->is('post'))
{
if(isset($this->data['Loginsubmit']))
{
if($this->data['Reg']['email']=='')
{
echo 'hii';
}
elseif( $this->data['Reg']['password']=='')
{
echo "hi";
}
else
{
$result = $this->Reg->find('list',array('conditions'=>array('email'=>$this->request->data['Reg']['email'], 'password'=>$this->request->data['Reg']['password'],'status'=>1)));
pr($result);
if(!empty($result)){
$email=$this->request->data['Reg']['email'];
$this->Session->write('Reg', $result);
$this->redirect(array('action' => 'login'));}
else{$this->Flash->error("invalid");
}
$result = $this->Reg->find('all');
$this->set('results',$result);
}
Hi please do like this:
public function beforeFilter() {
parent::beforeFilter();
// Allow users to register and logout.
$this->Auth->fields = array(
'email' => 'email',
'password' => 'secretword'
);
}
public function login() {
if(!$this->Auth->Reg('id')){
$this->layout="login";
if ($this->request->is('post')) {
App::uses('Validation', 'Utility');
$user=0;
if(Validation::email($this->request->data['email']))
$user = $this->Reg->find('first', array(
'conditions' => array( 'Reg.email' => $this->request->data['email'],
),'recursive' => -1 ));
if($user)
{
$this->request->data['email']=$user['Reg']['email'];
}
$this->request->data=array('Reg'=>$this->request->data);
if ($this->Auth->login()) {
return $this->redirect($this->Auth->redirectUrl());
}
$this->Session->setFlash(__('Invalid username or password, try again'));
}
}
else
{
return $this->redirect($this->Auth->redirectUrl());
}
}
Please review & share your feedback.
I am trying to insert multiple records using cakephp.My controller code are given below :
if (!empty($this->request->data)) {
foreach($this->request->data['Deal']['deal_date'] as $key => $data)
{
$this->Deal->create();
$this->Deal->id = $id;
$this->request->data['Deal']['deal_date'] = $data;
$this->request->data['Deal']['recur'] = $this->request->data['Deal']['recur'][$key];
if ($this->Deal->save($data)) {
$this->Session->setFlash(__('The deal has been saved'));
//$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The deal could not be saved. Please, try again.'));
}
}
}
Its not inserting data into database properly.I used array in all the input fields.Any Idea?
Use saveAll or saveMany
if (!empty($this->request->data)) {
$records = array();
foreach($this->request->data['Deal']['deal_date'] as $key => $data) {
$record = $this->request->data;
$record['Deal']['id'] = $id; // are you sure you need this ?
$record['Deal']['deal_date'] = $data;
$record['Deal']['recur'] = $this->request->data['Deal']['recur'][$key];
$records[] = $record;
}
if ($this->Deal->saveMany($records)) {
$this->Session->setFlash(__('The deal has been saved'));
} else {
$this->Session->setFlash(__('The deal could not be saved. Please, try again.'));
}
}
//try this
if (!empty($this->request->data)) {
$myData = array();
foreach($this->request->data['Deal']['deal_date'] as $key => $data)
{
$this->Deal->create();
$myData['Deal']['deal_date'] = $data;
$myData['Deal']['recur'] = $this->request->data['Deal']['recur'][$key];
if ($this->Deal->save($myData)) {
$this->Session->setFlash(__('The deal has been saved'));
//$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The deal could not be saved. Please, try again.'));
}
unset($myData);
}
}
For inserting many rows use savemany or saveall as mention by cornelb,
but try to debug why your code is not working by printing sql log
if (!empty($this->request->data)) {
foreach($this->request->data['Deal']['deal_date'] as $key => $data)
{
$this->Deal->create();
$this->Deal->id = $id;
$this->request->data['Deal']['deal_date'] = $data;
$this->request->data['Deal']['recur'] = $this->request->data['Deal']['recur'][$key];
if ($this->Deal->save($data)) {
$log=$this->Deal->getDataSource()->getLog(false, false);
echo "<pre>";print_r($log);exit;
$this->Session->setFlash(__('The deal has been saved'));
//$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The deal could not be saved. Please, try again.'));
}
}
}
I have following code to upload a file in cakephp
class UsersController extends AppController {
var $name = 'Users';
function index() {
$this->set('users', $this->User->find('all'));
}
function add() {
if (!empty($this->data)) {
if ($this->User->save($this->data)) {
$this->Session->setFlash('Your user data has been saved.');
$this->redirect(array('action' => 'index'));
}
$this->User->create();
if ($this->uploads() && $this->User->save($this->data)) {
$this->Session->setFlash(_('Upload saved', true));
} else {
$this->Session->setFlash(_('Upload not saved.Please try again', true));
}
}
}
function uploads() {
$uploads_dir = '/uploads';
$users = $this->request->data['Upload']['file'];
if ($users['error'] === UPLOAD_ERR_OK) {
if (move_uploaded_file($users['User']['file'], $uploads_dir)) {
$this->User->saveAll($this->data);
return true;
}
}
return false;
}
function edit($id = null) {
$this->User->id = $id;
if (empty($this->data)) {
$this->data = $this->User->read();
} else {
if ($this->User->save($this->data)) {
$this->Session->setFlash('Your user details has been updated.');
$this->redirect(array('action' => 'index'));
}
}
}
function delete($id) {
$this->User->delete($id);
$this->Session->setFlash('This user has been deleted.');
$this->redirect(array('action' => 'index'));
}
}
When I'm trying to upload, the file is uplaoding but I need the file uploaded to be viewed when the hyperlink given for each .jpeg to be displayed but I'm getting the error as
" The requested address '/Users/app/webroot/index.php/uploads' was not found on this server."
Also please help me how to store the uploaded image in another folder
PS:: code should b purely in CakePHP
Please help,
Thanks in advance
Make sure the folder used for uploading files have sufficient permissions and you can use Cakephp Media view to download files. For your reference,check this.
Downloading files using media view in CakePHP
Use image name or image id as an argument in function download and change path to
'path' => APP . 'uploads' . DS
"uploads" folder should be inside webroot directory.
http://webdesign4.georgianc.on.ca/~100141468/comp2084/todo/clients
http://bin.cakephp.org/view/1402280124
The first link- where add is- it is working fine, but the inner join is not working. The SQL is correct, just not the code in the controller. Another issue I am having is the undefined action views. I am certain it has to do with what's inside the controller.
<?php
class ClientsController extends AppController
{
var $name = 'Clients';
function index()
{
$this->set('Clients', $this->Client->find('all'));
}
function add()
{
if ($this->request->is('post')) {
if ($this->Client->save($this->request->data)) {
$this->Session->SetFlash('Client has been saved');
$this->redirect(array(
'action' => 'index'
));
} else {
$this->Session->setFlash('Nope');
}
}
}
public function view($id = null)
{
$this->Client->id = $id;
$this->set('Client', $this->Client->read(null, $id));
}
public function edit($id = null)
{
$this->Client->id = $id;
if ($this->request->is('get')) {
$this->request->data = $this->Client->read();
} else {
if ($this->Client->save($this->request->data)) {
$this->Session->setFlash('Client has been updated.');
$this->redirect(array(
'action' => 'index'
));
} else {
$this->Session->setFlash('Unable to update Client.');
}
}
$this->loadModel('Employee');
$this->set('Employees', $this->Employee->find('list', array(
'order' => array(
'Employee.name'
)
)));
$this->set(compact('Employees'));
}
public function delete($id = null)
{
if ($this->request->is('/Clients/delete/{id}')) {
throw new MethodNotAllowedException();
}
if ($this->Client->delete($id)) {
$this->Session->setFlash('The Client with id: ' . $id . ' has been deleted.');
$this->redirect(array(
'action' => 'index'
));
}
}
}
?>