Yii Upload Image to Database - php

Ive been trying to upload an image file into my data base which as the attribute image_name. I want to be able to store the image and then store its file path into my database.. Ive tried many options on here.. but can't seem to figure out how to do it...
This is what I've got so far if anyone could point me in the right direction that would be great...
Thanks
Model -
public function attributeLabels()
{
return array(
'id' => 'ID',
'movie_name' => 'Movie Name',
'studio_id' => 'Studio',
'country_id' => 'Country',
'movie_rating_id' => 'Movie Rating',
'map_pin_id' => 'Map Pin',
'description' => 'Description',
'title_image' => 'Title Image',
'title_trailer_youtube_code' => 'Title Trailer Youtube Code',
'release_date' => 'Release Date',
'bg_colour_code' => 'Bg Colour Code',
'text_colour_code' => 'Text Colour Code',
'is_released' => 'Is Released',
'is_advanced_booking_allowed' => 'Is Advanced Booking Allowed',
'advanced_booking_start_date' => 'Advanced Booking Start Date',
'advanced_booking_end_date' => 'Advanced Booking End Date',
'domain_prefix' => 'Domain Prefix',
'facebook_app_id' => 'Facebook App',
'facebook_app_url' => 'Facebook App Url',
'facebook_icon_image_url' => 'Facebook Icon Image Url',
'facebook_text' => 'Facebook Text',
'twitter_text' => 'Twitter Text',
'booking_share_text' => 'Booking Share Text',
'home_tracking_url' => 'Home Tracking Url',
'shows_tracking_url' => 'Shows Tracking Url',
);
}
Controller -
public function actionCreate()
{
$model=new Movie;
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if(isset($_POST['Movie']))
{
$model->attributes=$_POST['Movie'];
$uploadedFile=CUploadedFile::getInstance($model,'title_image');
$fileName = $uploadedFile;
$model->title_image = $fileName;
if($model->save())
$uploadedFile->saveAs('http://localhost:8888/MY/Movies/title_image/'.$fileName);
$this->redirect(array('view','id'=>$model->id));
}
$this->render('create',array(
'model'=>$model,
));
}

Reading your code, uploadedFile->saveAs() method expect full path to the file, not url.
To get full path, try with this:
$uploadedFile->saveAs(Yii::getPathOfAlias('webroot').'/images/uploaded/'.$fileName);
In this example images dir should be at same level as protected dir (only if images dir is being used for display public images through url):
assets/
images/
protected/
themes/
If you are on Unix environment, make sure to give proper read/write access to images dir.
If doesn't work, review this useful sample tutorial step by step that helps to introduce to file uploading with Yii.

Related

Facebook php api conflict between url and picture

Something strange happens using the facebook feed, if I enter the destination URL in the format "app.facebook.com/..." the stream of public information correctly including the attached image, but if I change the link in the "www.facebook.com/page_name/APP_ID", no image appears into stream.
Code below:
$publishStream = $facebook->api("/$session/feed", 'post', array(
'message' => "Message",
'picture' => URL_APP,
'name' => 'Name',
'description'=> 'Description',
'link' => 'https://www.facebook.com/PageName?sk=app_1447229...',
)
);

Yii uploading images in Yiistrap for carausel

In my web application I need to upload images from my system and use it in Yiistrap carausel. I copied the image into my images directory and gave the address .But its not recognizing , its not showing the image although I gave the correct adress of my image
My code for using the image in Yiistrap widgets.
<?php echo TbHtml::carousel(array(
array('image' => '/var/www/store3/images/download.jpg/830x477', 'label' => 'First Thumbnail label', 'caption' => 'image1'),
array('image' => '/var/www/store3/images/download1.jpg/830x477', 'label' => 'Second Thumbnail label', 'caption' => 'image2'),
)); ?>
I also tried all combinations by removing the full path naming ,adding the relative path , but nothing seems to work .
The error I am getting is..
Failed to load resource: the server responded with a status of 404 (Not Found)
Any body kindly help me with this .I am stuck up
Try this. It worked at my end. Using Yii's baseurl is the best way to reference directories.
<?php echo TbHtml::carousel(array(
array('image' => Yii::app()->request->baseUrl.'/images/download.jpg', 'label' => 'First Thumbnail label', 'caption' => 'image1'),
array('image' => Yii::app()->request->baseUrl.'/images/download1.jpg', 'label' => 'Second Thumbnail label', 'caption' => 'image2'),
)); ?>

CakePHP validation not detecting form field

I have a form where users can upload images, and I'm printing it to the page like so:
<?php echo $this->Form->label('file', 'Image file', array('class' => 'col-lg-1 control-label')); ?>
Then, in the model I'm setting up validation like so:
public $validate = array(
'file' => array(
'required' => array(
'rule' => array('notEmpty'),
'message' => 'You must select an image to upload'
),
'extension' => array(
'rule' => array('extension', array('png')),
'message' => 'Images must be in PNG format'
),
'size' => array(
'rule' => array('fileSize', '<', '1MB'),
'message' => 'Images must be no larger than 1MB'
),
'goodUpload' => array(
'rule' => 'uploadError',
'message' => 'Something went wrong with the upload, please try again'
)
)
);
However, Cake doesn't seem to be associating the form field with the validation rule, as if I select an image to upload I always get "You must select an image to upload" as a form error. I've made sure the form has enctype="multipart/form-data".
Is this happening because file isn't a database field? How can I make cake run some validation on file?
Edit: Here's my entire form, as requested: http://pastebin.com/SbSbtDP9
You can validate fields that are not in the database, long as you have the correct field name in the correct model.
From what I can see in your code it seems your outputting a label instead of an actual input, for the image upload I would try
echo $this->Form->create('Model', array('type'=>'file'));
echo $this->Form->input('file', array('type'=>'file'));
echo $this->Form->submit('Upload Image'):
echo $this->Form->end();
For the validation I would try something like with the rest of your validate options (size, etc...) CakePHP usually throws an error on notEmpty on File Uploads. So just checking for the extension type is usually good enough.
public $validate = array(
'file' => array(
'rule' => array(
'extension', array('jpeg', 'jpg')
'message' => 'You must supply a file.'
)
)
);
Majority of time in CakePHP for Image Uploading I resort to a plugin such as https://github.com/josegonzalez/cakephp-upload it does validation and upload handling all in one.
Managed to figure it out. Turns out having the notEmpty validation on a file fieldnever works, it always thinks there's nothing there and so always throws that validation message.
Worked around it by writing my own validation method.

Adding new custom Permissions in Drupal tutorial

I created module now i want to set permission on that module so some of the users can see that field. I searched on google and stackoverflow but I didn't get proper answer as i needed.
My code as below
function downloaded_menu() {
$items['user/%user/downloaded_poems'] = array(
'title' => 'Downloaded Poems',
'page callback' => 'downloaded_content_page',
'access arguments' => array('poet downloaded work'),
'type' => MENU_LOCAL_TASK,
'weight' => 11,
);
return $items;
}
Now I want to give permission to particular user. who can only see.
You will have to use hook_permission to do so.
Code example:
function downloaded_permission()
{
return array(
'poet downloaded work' => array(
'title' => t('poet downloaded work'), // the title to be shown in the permissions page
'description' => t('poet downloaded work'), // the description to be shown in the permissions page
'restrict access' => FALSE,
),
);
}
Then go to the permissions page and give the permissions to the required roles.
Hope this helps... Muhammad.

Zend framework - form not rendering

I'm just starting to use Zend Framework and was following the quick start documentation for the latest version (1.11.10). Everything was going just fine, but when I placed the form code and ran the application, the form did not render. My code is exactly like http://framework.zend.com/manual/en/learning.quickstart.create-form.html
On the view, I can dump the form just fine with var_dump($this->form);
I've tried echo $this->form(), echo $this->form->render(), but nothing appeared... What could it be?
This problem can occur when Zend can't find the template file for an element. Look at following code:
$element->setDecorators(array(
array('ViewScript',
array(
'viewScript' => 'directory/_input.phtml'
)
)
));
The file _input.phtml must be in the right folder for this Controller. Otherwise Zend can't find the template for input and can't successfully render your element and will not show anything.
Make sure you pass the form to the view from the controller.
In your action handler:
$this->view->form = $my_form;
In your view:
echo $this->form;
I suspected that this was the cause of your problem because Zend Framework doesn't complain if you try to echo a parameter that doesn't exist. (i.e. echo $this->some_fake_parameter won't do anything)
Ok so i tried your code, and it worked for me no problem.
Here is everything:
Controller
<?php
class IndexController extends Zend_Controller_Action
{
public function myTestAction()
{
$form = new Form_Guestbook();
// ... processing logics
if($this->getRequest()->isPost())
{
if($form->isValid($this->getRequest()->getPost()))
{
var_dump($form->getValues());
}
}
$this->view->assign('form', $form);
}
}
Form
<?php
class Form_Guestbook extends Zend_Form
{
public function init()
{
// Set the method for the display form to POST
$this->setMethod('post');
// Add an email element
$this->createElement('text', 'email', array(
'label' => 'Your email address:',
'required' => true,
'filters' => array('StringTrim'),
'validators' => array(
'EmailAddress',
)
));
// Add the comment element
$this->addElement('textarea', 'comment', array(
'label' => 'Please Comment:',
'required' => true,
'validators' => array(
array('validator' => 'StringLength', 'options' => array(0, 20))
)
));
// Add a captcha
$this->addElement('captcha', 'captcha', array(
'label' => 'Please enter the 5 letters displayed below:',
'required' => true,
'captcha' => array(
'captcha' => 'Figlet',
'wordLen' => 5,
'timeout' => 300
)
));
// Add the submit button
$this->addElement('submit', 'submit', array(
'ignore' => true,
'label' => 'Sign Guestbook',
));
// And finally add some CSRF protection
$this->addElement('hash', 'csrf', array(
'ignore' => true,
));
}
}
?>
View
<?php echo $this->form->render(); ?>
can be seen on: http://yaconiello.com/index/my-test
If this isnt working for you, you may be having a configuration error.
I had a problem like that (exact same form, since it is eclipse example)
My problem was due to misunderstanding. Since I thought that I have to directly access to the view script. I entered in the browser something like: hostname/application/view/script/something.php
But in zend all accesses should be through public folder. You have to access to the view like this: hostname/app_name/public/guestbook
hope that would help you

Categories