Adding a file field to the manifest file - joomla

My plugin requires a user to upload a file.
The manifest field doesn't set the form enctype="multipart/form-data" so when a file is uploaded it gets lost.
Is there a way to change this?

form xml :
<field name="con_image" type="file" label="" description="" hint="Image"/>
default.php
<div class="controls">
<?php if (!empty($this->item->con_image) && file_exists(JPATH_SITE.'/images/contact_image/thumb_' . $this->item->con_image)) : ?>
<img src="<?php echo JRoute::_(JUri::root() . 'images/contact_image/thumb_' . $this->item->con_image, false);?>">
<?php endif; ?>
<input type="hidden" name="jform[con_image]" id="jform_image_hidden" value="<?php echo $this->item->con_image; ?>" />
<?php echo $this->form->renderField('con_image'); ?>
</div>
your view table file code for image upload:
$files = $app->input->files->get('jform', array(), 'raw');
$array = $app->input->get('jform', array(), 'ARRAY');
if (!empty($files['con_image']['name']))
{
// Deleting existing files
$oldFiles = PlansHelpersPlans::getFiles($this->id, $this->_tbl, 'con_image');
foreach ($oldFiles as $f)
{
$oldFile = JPATH_ROOT . '/images/contact_image/' . $f;
if (file_exists($oldFile))
{
unlink($oldFile);
}
}
$this->con_image = "";
$singleFile = $files['con_image'];
jimport('joomla.filesystem.file');
// Check if the server found any error.
$fileError = $singleFile['error'];
$message = '';
if ($fileError > 0 && $fileError != 4)
{
switch ($fileError)
{
case 1:
$message = JText::_('File size exceeds allowed by the server');
break;
case 2:
$message = JText::_('File size exceeds allowed by the html form');
break;
case 3:
$message = JText::_('Partial upload error');
break;
}
if ($message != '')
{
$app->enqueueMessage($message, 'warning');
return false;
}
}
elseif ($fileError == 4)
{
if (isset($array['con_image']))
{
$this->con_image = $array['con_image'];
}
}
else
{
// Replace any special characters in the filename
jimport('joomla.filesystem.file');
$filename = JFile::stripExt($singleFile['name']);
$extension = JFile::getExt($singleFile['name']);
$filename = preg_replace("/[^A-Za-z0-9]/i", "-", $filename);
$filename = rand()."-".$filename . '.' . $extension;
$uploadPath = JPATH_ROOT . '/images/contact_image/' . $filename;
$fileTemp = $singleFile['tmp_name'];
if (!JFile::exists($uploadPath))
{
if (!JFile::upload($fileTemp, $uploadPath))
{
$app->enqueueMessage('Error moving file', 'warning');
return false;
}
}
//$this->con_image .= (!empty($this->con_image)) ? "," : "";
$this->con_image = $filename;
}
}else{
$this->con_image = $array['con_image'];
}
Still you have problem then check HERE
Download my simple component : https://github.com/erakashpatel/Important-notes/blob/master/com_helloworld.zip
OR
https://github.com/erakashpatel/Important-notes/blob/master/com_test.zip
I hope its works.Thanks in advance.

for xml :
Joomla File form field type in xml:
https://docs.joomla.org/File_form_field_type
<field name="myfilevalue" type="file" label="Enter some text" description="Choose an image from your computer with maximum 100KB" size="10" accept="image/*" />
OR
if your file field in html form then used :
<input type="file" name="jform[myfilevalue]" >
I hope its help,
else provide more description please

Related

codeigniter upload 2 files (images and video ) in separate field

Hi i am new in codeigniter , I want to upload image and video in same form with different field. i did but it store either one. last one is stored like video.
<div id="file-upload" class="form-fields">
<div class="new_Btn"><i title="Upload Image" class="fa fa-cloud-upload" aria-hidden="true"></i><span>Upload Image</span></div>
<input id="html_btn" type="file" id="fileToUpload" name="fileToUpload" />
</div>
<div id="file-upload" class="form-fields">
<div class="new_Btn"><i title="Upload Image" class="fa fa-cloud-upload" aria-hidden="true"></i><span>Upload Video</span></div>
<input id="html_btn" type="file" id="videoToUpload" name="videoToUpload" />
</div>
And my controller is
public function videoupdate() {
$data['user_data'] = $this->session->userdata('user_logged_in');
if (($this->session->flashdata('success')))
$data['success'] = $this->session->flashdata('success');
else
$data['error'] = $this->session->flashdata('error');
if (!empty($data['user_data']['user_id'])) {
$title = $_POST['title'];
$description = htmlentities($_POST['description']);
$target_dir = './cms/uploads/blog/video3/';
$temp = explode('.', $_FILES['fileToUpload']['name']);
$video = explode('.', $_FILES['videoToUpload']['name']);
if (!empty($_FILES['fileToUpload']['name'])) {
$newfilename = round(microtime(true)) . '.' . end($temp);
} else {
$newfilename = "";
}
if (!empty($_FILES['videoToUpload']['name'])) {
$videofilename = round(microtime(true)) . '.' . end($video);
} else {
$videofilename = "";
}
move_uploaded_file($_FILES['fileToUpload']['tmp_name'], './cms/uploads/blog/video3/' . $newfilename);
move_uploaded_file($_FILES['videoToUpload']['tmp_name'], './cms/uploads/blog/video3/' . $videofilename);
$createddate = date('Y-m-d H:i:s');
$ipaddress = $_SERVER['REMOTE_ADDR'];
$status = $this->microblog_model->insertBlogvideo($title, $description, $newfilename, $videofilename, $data['user_data']['user_id'], $createddate, $ipaddress);
I found your code for upload files is working fine. As I check it in separate php file:
<pre>
<?php
if(isset($_POST)){
$temp = explode('.', $_FILES['fileToUpload']['name']);
$video = explode('.', $_FILES['videoToUpload']['name']);
try{
//upload image
if (!empty($_FILES['fileToUpload']['name'])) {
$newfilename = round(microtime(true)) . '.' . end($temp);
if(move_uploaded_file($_FILES['fileToUpload']['tmp_name'], './uploads/' . $newfilename)){
echo "Image Uploaded<br/>";
}
}
//upload video
if (!empty($_FILES['videoToUpload']['name'])) {
$videofilename = round(microtime(true)) . '.' . end($video);
if(move_uploaded_file($_FILES['videoToUpload']['tmp_name'], './uploads/' . $videofilename)){
echo "Video Uploaded<br/>";
}
}
}catch(Exception $e){
print_r($e);
}
}
?>
</pre>
<form method="post" enctype="multipart/form-data">
<div id="file-upload" class="form-fields">
<div class="new_Btn"><i title="Upload Image" class="fa fa-cloud-upload" aria-hidden="true"></i><span>Upload Image</span></div>
<input id="html_btn" type="file" id="fileToUpload" name="fileToUpload" />
</div>
<div id="file-upload" class="form-fields">
<div class="new_Btn"><i title="Upload Image" class="fa fa-cloud-upload" aria-hidden="true"></i><span>Upload Video</span></div>
<input id="html_btn" type="file" id="videoToUpload" name="videoToUpload" />
</div>
<input type="submit" name="submit">
</form>
You should check the size of video you are trying to upload and the maximum upload size allowed. I hope this will help. Because I checked it multiple times with many images and videos and found it is working fine.

Inserting extra comma while multi upload how to avoid this

here what i need to do for avoiding extra comma separater while upload the images.it takes one more image while upload the file.i used comma separater for the concatination.
Controller
$filename_new_get = "";
$count = Input::get('count');
for($i = 0 ; $i < $count ; $i++)
{
$file_more = Input::file('file_more'.$i);
$file_more_name = $file_more->getClientOriginalName();
$move_more_img = explode('.',$file_more_name);
$filename_new = $move_more_img[0].str_random(8).".".$move_more_img[1];
$newdestinationPath = '../assets/categoryimage/';
$uploadSuccess_new =Input::file('file_more'.$i)->move($newdestinationPath,$filename_new);
$filename_new_get .= $filename_new.",";
}
$inputs = Input::all();
$file = Input::file('file');
$filename = $file->getClientOriginalName();
$move_img = explode('.',$filename);
$filename = $move_img[0].str_random(8).".".$move_img[1];
$destinationPath = '../assets/categoryimage/';
$uploadSuccess = Input::file('file')->move($destinationPath,$filename);
$file_name_insert = $filename . ",".$filename_new_get;
$data=array(
'new_category_image'=> $file_name_insert,
);
Views
<div class="col-lg-8" id="img_upload">
<input type="file" id="file0" name="file" style="background:none;width:185px;border:none;">no files
<div id="divTxt"></div>
<p><a onClick="addproductimageFormField(); return false;" style="cursor:pointer;color:#F60;width:84px;" id="add_img_btn" >Add</a></p>
<input type="hidden" id="aid" value="1">
<input type="text" id="count" name="count" value="0">
</div>
Use this:
$newFileName = rtrim($file_name_insert, ',');
rtrim will remove the last unwanted , from the concatenated string.

File (picture) Upload not working

Okay so I just basically got this code online.
It works locally (to upload a file), but it doesn't wanna work on the website.
(I've already put the permissions at 0777, for the folder, but it still won't upload.
<header>
<?php
function UploadOne($fname)
{
$uploaddir = 'pictures/';
if (is_uploaded_file($fname['tmp_name']))
{
$filname = basename($fname['name']);
$uploadfile = $uploaddir . basename($fname['name']);
if (move_uploaded_file ($fname['tmp_name'], $uploadfile))
$res = "File " . $filname . " was successfully uploaded and stored.<br>";
else
$res = "Could not move ".$fname['tmp_name']." to ".$uploadfile."<br>";
}
else
$res = "File ".$fname['name']." failed to upload.";
return ($res);
}
?> </header>
<body>
<?php
if ($_FILES['picture']['name'] != "")
{
$res = UploadOne($_FILES['picture']);
$filname = $_FILES['picture']['name'];
echo ($res);
}
?>
<h1>UPLOADING FILES</h1>
<form name="fupload" enctype="multipart/form-data" action="upfiles.php" method="post">
<input type="file" name="picture" />
<input type="submit" value="Submit" />
</form>
</body>

how to upload multiple files in one <input>

I have made a code that let me upload multiple files but in separate
I was trying to upload a multiple files in one input where i set my input tag into
<input type="file" multiple="" name="file1">
I selected 3 images but only 1 image was uploaded..
here is my VIEW before changing my input:
<?php echo form_open_multipart('test'); ?>
<p>
<?php echo form_label('File 1: ', 'file1') ?>
<input type='file' name='file1' id='file1'>
</p>
<p>
<?php //echo form_label('File 2: ', 'file2') ?>
<input type='file' name='file2' id='file2'>
</p>
<p><?php echo form_submit('submit', 'Upload them files!') ?></p>
and here is my CONTROLLER
function index()
{
if (isset($_POST['submit']))
{
$this->load->library('upload');
$config['upload_path'] = './upload_documents/';
$config['allowed_types'] = 'jpg|png|gif|jpeg|JPG|PNG|GIF|JPEG';
$config['max_size'] = '0'; // 0 = no file size limit
$config['max_width'] = '0';
$config['max_height'] = '0';
$config['overwrite'] = TRUE;
$this->upload->initialize($config);
foreach($_FILES as $field => $file)
{
// No problems with the file
if($file['error'] == 0)
{
// So lets upload
if ($this->upload->do_upload($field))
{
$data = $this->upload->data();
//alert("nice");
}
else
{
$errors = $this->upload->display_errors();
die();
}
}
else{
echo "alksjdfl";
die();
}
}
}
$this->load->view("test");
}
}
Use this multi file upload library
https://github.com/stvnthomas/CodeIgniter-Multi-Upload/blob/master/MY_Upload.php
// Prepraing upload config & upload files
$config = array();
$config['upload_path'] = 'temp/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '256000';
$this->load->library('upload');
$this->upload->initialize($config);
$this->upload->do_multi_upload("my_file");
$files_upload = $this->upload->get_multi_upload_data();
$err_msg = $this->upload->display_errors();
if (!empty($files_upload) && is_array($files_upload)) {
foreach ($files_upload as $file_data) {
// Your files here :)
}
}
Special thanks to #stvnthomas for the library :)
You should use this library for multi upload in CI
https://github.com/stvnthomas/CodeIgniter-Multi-Upload
Installation
Simply copy the MY_Upload.php file to your applications library directory.
Use: function test_up in controller
public function test_up(){
if($this->input->post('submit')){
$path = './public/test_upload/';
$this->load->library('upload');
$this->upload->initialize(array(
"upload_path"=>$path,
"allowed_types"=>"*"
));
if($this->upload->do_multi_upload("myfile")){
echo '<pre>';
print_r($this->upload->get_multi_upload_data());
echo '</pre>';
}
}else{
$this->load->view('test/upload_view');
}
}
upload_view.php in applications/view/test folder
<form action="" method="post" enctype="multipart/form-data">
<input type="file" name="myfile[]" id="myfile" multiple>
<input type="submit" name="submit" id="submit" value="submit"/>

CodeIgniter - Image submit button not working

I am using CI to create a project for a client, I have a submit button as an image but it doesn't seem to be submitting the form, the code I have at the moment is.
<input type="image" name="trialSubmit" id="trialSubmit" src="<?php echo base_url().'images/subscribe_free.jpg'; ?>" style="height:29px;width:207px;border-width:0px;" />
The code I have to use at the moment is as follows
<input type="submit" name="trialSubmit" value=" Subscribe Free " id="" class="button" />
If anyone could shed some light on why it's not working with the image, that would be tight.
Cheers,
Is it across all browsers? Or IE specific? What about if you add an onclick event just for testing? onclick="javascript:document.formidhere.submit()"
I'd recommend using either <input type="submit" /> or my preference would be to use <button type="submit> due to browser inconsistencies with <input type="image" /> and just style the button with something like:
button{
background:url('/images/subscribe_free.jpg') no-repeat;
height:29px;
width:207px;
border:0;
}
Try adding value="trialSubmit" to get the image to submit. Seemed to work for me.
You could also see if this answer helps:
image submit button
**inside the view(as comerciales in my case)**,
<form action= "<?php echo site_url()?>/admin/doupload" method="post" enctype="multipart/form-data" >
<b> Select the image to upload( Maximum size 500 kb, jpg, jpeg): </b>
<input style="color:#00A76F" type="file" name="fileToUpload" id="fileToUpload">
<div class="input-group" style="left:10%;width:85%;">
<input class="btn btn-success pull-right" style="background:#00A76F" type="submit" value="Upload Image" name="submit">
</div>
</form>
<div class="modal-body">
<div id="user_data">
<?php
if (!empty($massage)){
echo $massage ;
}
?>
</div>
**inside the controller define a method**
public function doupload(){
$root1 = $_SERVER['DOCUMENT_ROOT'];;
$target_dir = $root1."/nawaloka/uploads/";
// $target_dir = $root1."/nawaloka/application/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if file already exists
// Check file size
if ($_FILES["fileToUpload"]["size"] > 500000) {
$data['massage']= "Sorry, your file is too large.";
$partials = array('content' => 'Admin/comerciales');
$this->template->load('template/admin_template', $partials,$data);
$uploadOk = 0;
}
if (is_dir($target_dir) && is_writable($target_dir)) {
// do upload logic here
} else {
echo 'Upload directory is not writable, or does not exist.';
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "jpeg" ) {
$data['massage']= "Sorry, only JPG, JPEG files are allowed.";
$partials = array('content' => 'Admin/comerciales');
$this->template->load('template/admin_template', $partials,$data);
$uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
$data['massage']= "Sorry, your file was not uploaded.";
$partials = array('content' => 'Admin/comerciales');
$this->template->load('template/admin_template', $partials,$data);
// if everything is ok, try to upload file
} else {
array_map('unlink', glob("/var/www/html/nawaloka/uploads/*"));
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"],"/var/www/html/nawaloka/uploads/shamith.jpg")) {
$data['massage']= "The image ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
$partials = array('content' => 'Admin/comerciales');
$this->template->load('template/admin_template', $partials,$data);
//echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
} else {
$data['massage']= "Sorry, there was an error while uploading your file.";
$partials = array('content' => 'Admin/comerciales');
$this->template->load('template/admin_template', $partials,$data);
}
}
}

Resources