Opencart - Upload Specific File Type - file-upload

I am using OpenCart 1.5.6 and have two options for the customer to upload files to a product (image and video). When the users clicks upload I would like them to only be able to select specific file types from their computer while they browse for the files. So instead of "All Files" in the browser window, I want it to only allow .png, .jpg, .jpeg, .pdf, etc. for the image upload and for the video file I just want it to look for .mp4, .mov, .flv.
Here is an example: http://jsfiddle.net/VCdvA/
I have tried to use the accept attribute according to this post but it doesn't work.
Here is my code for the product.tpl:
<input type="button" accept="video/*,image/*" value="<?php echo $button_upload; ?>" id="button-option-<?php echo $option['product_option_id']; ?>" class="button" >
<input type="hidden" name="option[<?php echo $option['product_option_id']; ?>]" value="" />
I think that the problem is that it is type="button" and not type="file". Is there another way to limit the upload file type within the OpenCart product page?

The problem is the jQuery plugin used. It is AjaxUpload which seems to be taken over by different developer (completely) and reworked (completely) and the links to the original (old) documentation are dead (completely).
Well, after exploring that jQuery plugin I have found out that it works this way: in the ptoduct.tpl You can see:
<?php foreach ($options as $option) { ?>
<?php if ($option['type'] == 'file') { ?>
<script type="text/javascript"><!--
new AjaxUpload('#button-option-<?php echo $option['product_option_id']; ?>', {
//...
});
//--></script>
<?php } ?>
<?php } ?>
This means that for each option of type file this AjaxUpload is instanciated. Basically it only creates a DIV containing INPUT of type FILE with 0 opacity that is rendered above the Upload File button. This means that when user clicks on Upload File he clicks on the INPUT of type FILE instead (without knowing this). Now, looking at the code I can see that there is no possibility to set this accept="image/*" attribute because there is no support for this (original code):
_createInput: function(){
var self = this;
var input = document.createElement("input");
input.setAttribute('type', 'file');
input.setAttribute('name', this._settings.name);
addStyles(input, {
// ...
});
// ...
},
So now if You'd like to add support for this, You'd need to change this code to this (file: catalog/view/javascript/jquery/ajaxupload.js, around line 350):
var input = document.createElement("input");
input.setAttribute('type', 'file');
input.setAttribute('name', this._settings.name);
if (this._settings.accept) {
input.setAttribute('accept', this._settings.accept);
}
And change the code for plugin initializiation in product.tpl to:
<?php foreach ($options as $option) { ?>
<?php if ($option['type'] == 'file') { ?>
<script type="text/javascript"><!--
new AjaxUpload('#button-option-<?php echo $option['product_option_id']; ?>', {
<?php if ($option['product_option_id'] == <IMAGE_FILE_OPTION_ID>) { ?>
accept: 'image/*',
<?php } else if ($option['product_option_id'] == <VIDEO_FILE_OPTION_ID>) { ?>
accept: 'video/*',
<?php } ?>
//...
});
//--></script>
<?php } ?>
<?php } ?>
This should be enough.

Related

Install ckeditor in YII framework

I am using Yii Framework version 1.1.14. I am able to install FCK editor but I want to use ck editor.
I download files from this links http://www.yiiframework.com/extension/the-ckeditor-integration/
and upload files on location but i get this error
check image
I am using this code in my view
<?php $this->widget('application.extensions.ckeditor.CKEditorWidget',array(
'model'=>$model, # Data-Model (form model)
'attribute'=>'content', # Attribute in the Data-Model
'height'=>'400px',
'width'=>'100%',
'toolbarSet'=>'Basic', # EXISTING(!) Toolbar (see: ckeditor.js)
'ckeditor'=>Yii::app()->basePath.'/../ckeditor/ckeditor.php',
# Path to ckeditor.php
'ckBasePath'=>Yii::app()->baseUrl.'/ckeditor/',
# Relative Path to the Editor (from Web-Root)
'css' => Yii::app()->baseUrl.'/css/index.css',
# Additional Parameters
) ); ?>
Download CKEditor from http://ckeditor.com/download (Select full package) and put it in your root directory (You can put it any where according to you)
Add this is your view file
<script src="<?php echo Yii::app()->baseUrl.'/ckeditor/ckeditor.js'; ?>"></script>
<script type="text/javascript">
CKEDITOR.replace( 'Articles_meta_description');
</script>
Where Articles_meta_description is id of input fields
If you want add image upload in ckeditor you can download KCfinder from this link http://kcfinder.sunhater.com/download and put it in your root directory (you can put it any where according to you)
Set upload path into session : in your view file
$_SESSION['KCFINDER']['disabled'] = false; // enables the file browser in the admin
$_SESSION['KCFINDER']['uploadURL'] = Yii::app()->baseUrl."/uploads/"; // URL for the uploads folder
$_SESSION['KCFINDER']['uploadDir'] = Yii::app()->basePath."/../uploads/"; //
For ckeditor with image upload
<script type="text/javascript">
CKEDITOR.replace( 'Articles_meta_description', { // input field id
filebrowserBrowseUrl: '<?php echo Yii::app()->baseUrl; ?>/kcfinder/browse.php?type=files',
filebrowserImageBrowseUrl: '<?php echo Yii::app()->baseUrl; ?>/kcfinder/browse.php?type=images',
filebrowserFlashBrowseUrl: '<?php echo Yii::app()->baseUrl; ?>/kcfinder/browse.php?type=flash',
filebrowserUploadUrl: '<?php echo Yii::app()->baseUrl; ?>/kcfinder/upload.php?type=files',
filebrowserImageUploadUrl: '<?php echo Yii::app()->baseUrl; ?>/kcfinder/upload.php?type=images',
filebrowserFlashUploadUrl: '<?php echo Yii::app()->baseUrl; ?>/kcfinder/upload.php?type=flash'
});
</script>

Changing text beside upload field in Yii Framework

hopefully a quick question here, have not been able to find out the answer on google. Anyway I have the following File upload field.
<div class="row">
<?php echo CHtml::form('', 'post', array('enctype' => 'multipart/form-data')); ?>
<?php echo $form->labelEx($model, 'doc'); ?>
<?php echo CHtml::activeFileField($model, 'doc'); ?>
<?php echo $form->error($model, 'doc'); ?>
</div>
The question I have is how to change the default text? On the page in question the upload field comes up with "No file chosen" beside the button when the page loads. I wish to change this text, anyone tell me how? Thanks.
I quick & dirty fix is this:
<?php echo CHtml::activeFileField($model, 'type',array('style'=>'width:100px;', 'onchange' => 'this.style.width = "100%";')); ?>

How should I display the content of the database in CActiveForm?

Now I am building the view of a website and I use the widget CActiveForm as the view. However, I need to display some of the contents of the database, for example, the result of a SQL query. Then what should I do to accomplish the goal? Thank you!
Some of my code is provided below:
<div class="form">
<?php
$form=$this->beginWidget('CActiveForm', array(
'id'=>'login-form',
'enableClientValidation'=>true,
'clientOptions'=>array(
'validateOnSubmit'=>true,
),
));
?>
<p class="note">Fields with <span class="required">*</span> are required.</p>
<div class="row buttons">
<?php echo CHtml::submitButton('Login'); ?>
</div>
<div class="row radiobuttons">
<?php echo CHtml::radioButtonList(
$Paper,
'Q1No',
array('A'=>'A','B'=>'B','C'=>'C','D'=>'D'),
array('template'=>'<span class="radio">{input}{label}</span>','separator'=>''));
?>
</div>
<?php $this->endWidget(); ?>
</div><!-- form -->
This is my code. And I need to add some contents of the database in the form. The content is listed below:
public function actionTaketest()
{
// Get the ID of the test paper from the URL ---ztm
if(isset($_GET['paperid']))
{
$paperid=(int)$_GET['paperid'];
$dsn = 'mysql:host=localhost;dbname=mydatabase';
$username = 'root';
$password = '000000';
$connection=new CDbConnection($dsn,$username,$password);
// establish connection. You may try...catch possible exceptions
$connection->active=true;
$rawData = Yii::app()->db->createCommand()
->select('*')
->from('Paper, Question') array(':PaperNo'=>$paperid))
->queryAll();
$dataProvider=new CArrayDataProvider($rawData, array(
'id'=>'PaperNo',
));
$connection->active=false; // close connection
$model=new LoginForm;
$this->render('form',array('model'=>$model, 'dataProvider'=>$dataProvider));
}
else
{
// Deny illegal access to the page ---ztm
throw new CHttpException(404,'invalid request');
}
}
This code are in the controller.
Your code is too messy, and looks invalid in some places. First of all you don't need to create CDbConnection in controller, you can specify credentials in config. To read data from database you can use ActiveRecord:
$paper = Paper::model()->findByAttributes(array('PaperNo' => $paperid));
And then in your html:
echo CHtml::activeRadioButtonList(
$paper,
'Q1No',
array('A'=>'A','B'=>'B','C'=>'C','D'=>'D'),
array('template'=>'<span class="radio">{input}{label}</span>','separator'=>'')
);
I hope it helps a little bit.

show uploaded image yii

I am new to php and yii. I have a file field in my form to upload images. I use the following code to upload the image using file field.
Code in View:
<?php echo $form->fileField($model,'logo', array('class'=>'input-file')); ?>
<img src="<?php
echo Yii::app()->request->baseUrl.'/protected/uploads/sitelogo/'.$savedvalues['varLogo'];
?>" width="50" height="50" />
<?php echo $form->hiddenField($model,'hiddenfile',
array('value'=>$savedvalues['varLogo'])); ?>
code in controller:
$randnum = rand(0,100);
$home->varLogo = $randnum.$model->logo;
$file= Yii::app()->getBasePath().'/uploads/sitelogo/'.$randnum.$model->logo;
$model->logo->saveAs($file);
The images are uploading fine now. I have saved the uploaded images in protected\uploads\ folder. I am trying to show the uploaded image in edit image section. But the image doesn't display. It Shows failed to load the given url in firebug.
How can i correct this issue?
protected folder is protected for some reason - nobody can access it
if you want to access your pictures move them to public folder (or public/uploaded)
here is .htaccess content for protected
deny from all
EDIT:
of course you can put your files into protected folder(or make uploaded folder not accessible too) if you want some ACL for your users, and then check access, read and output file by your script(class)
in view:
<img src="<?php
echo Yii::app()->request->baseUrl.'/image.php?url=protected/uploads/sitelogo/'.$savedvalues['varLogo'];
?>" width="50" height="50" />
in your controller or class:
if (Yii::app()->session['user_can_access_files']) {
header('Content-Type: image/jpeg');
readfile($_GET['url']);
} else {
Yii::app()->user->loginRequired();
}
Caveat: I'm a total yii noob, so take this w/ a grain of salt :)
I took a similar approach, but have user-specific folders & added a MIME type check.
I upload files in a similar way, then view them with this in the view:
echo '<embed src="/tim_0.5/index.php/files/Getdatafile/' . $modelid . '" width=100% height=400>';
And from my controller I have:
public function actionGetdatafile($id)
{
$model=$this->loadModel($id);
$imgpath = $model->user_id . '/' . $model->filename;
$file=Yii::app()->getBasePath().'/datafiles/'.$imgpath;
if (file_exists($file))
{
$img=getimagesize($file);
header('Content-Type: '.$img['mime']);
readfile($file);
exit;
} else {
echo 'File not found!';
}
}
<b><?php echo CHtml::encode($data->getAttributeLabel('binaryfile')); ?>:</b>
<?php echo CHtml::image(Yii::app()->request->baseUrl.'/upload/'.$data->fileName); ?>
<br />
filename is name of your file which you have stored in upload folder in root directory.
(Yii::app()->request->baseUrl.'/upload/' point to your upload folder which is in your root directory
Hope it works....

I am unable to upload file to my local host folder in php

I have the follwing code
<form enctype="multipart/form-data" action="upload.php" method="POST">
Please choose a file: <input name="uploaded" type="file" /><br />
<input type="submit" value="Upload" />
</form>
<?php
$target = "upload/";
$target = $target . basename( $_FILES['uploaded']['name']) ;
$ok=1;
if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target))
{
echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded";
}
else {
echo "Sorry, there was a problem uploading your file.";
}
?>
my page is in http://local.host/nausal/upload.php
now I am having the follwing error though i have created a folder in site with name upload.
Notice: Undefined index: uploaded in C:\wamp\www\Nausal\upload.php on line 15
Notice: Undefined index: uploaded in C:\wamp\www\Nausal\upload.php on line 17
Sorry, there was a problem uploading your file.
If the form is a part of upload.php too, you need to encapsulate the PHP-code and first check if $_FILES is not empty, otherwise you will always get a Notice if you only want to display the form.
<?php
if(!empty($_FILES))
{
//your PHP-code here
}
?>
<!-- your form here -->
Khan sb Place this code in file
called upload.php
create a folder called upload where
you created upload.php
change echo "The file ". basename(
$_FILES['uploadedfile']['name']). "
has been uploaded"; to echo "The
file ". basename(
$_FILES['uploaded']['name']). " has
been uploaded";
This will upload file successfully. First time when upload.php is uploaded there is no file selected so you will see some errors. But after you select a file and click upload you will not see any error. To avoid this error do as suggested by Dr. Molle