Can't upload multiple files - php

I can't seem to be able to upload several files on the same form, when not all are chosen (if I upload all, it works).
The relevant code:
<form id = "form" name = "form" action="<?php echo URLgenerator::getURL('project', 'group-edit', array('id' => $projectGroup->getId()), 'admin'); ?>" method="post" enctype='multipart/form-data'>
<?php
foreach($langs as $lang):
$langId = $lang->getId();
$displayName = $lang->getDisplayName();
?>
<fieldset id="In<?php echo $displayName; ?>">
<legend id ="<?php echo $langId?>">
<?php echo $displayName; ?>:
</legend>
<label for="name-<?php echo $langId; ?>">Name: </label>
<input type="text" id="name-<?php echo $langId; ?>" name="name-<?php echo $langId; ?>" style="width: 500px;" value="<?php echo $projectGroup->getName($lang); ?>" />
<br />
<label for="imageOff-<?php echo $langId; ?>">Image Off: </label>
<input type="file" id="imageOff-<?php echo $langId; ?>" name="imageOff-<?php echo $langId; ?>" />
<img src="<?php echo $projectGroup->getImageOffURL($lang); ?>" />
<br />
<label for="imageOn-<?php echo $langId; ?>">Image On: </label>
<input type="file" id="imageOn-<?php echo $langId; ?>" name="imageOn-<?php echo $langId; ?>" />
<img src="<?php echo $projectGroup->getImageOnURL($lang); ?>" />
</fieldset>
<?php
endforeach;
?>
<div align="center">
<input type="submit" value="Edit" id="button1" />
<input type="reset" id="button2" />
</div>
</form>
and the action (the var_dump is for debugging)
public function groupEditAction()
{
$id = $this->getRequest()->getParam('id');
$projectGroup = new ProjectGroup($id);
$name = Array();
$langs = LangFuncs::getAllLangs();
foreach($langs as $lang) {
$langId = $lang->getId();
$name[$langId] = $this->getRequest()->getParam("name-$langId");
}
$projectGroup->edit(null, $name);
$upload = new Zend_File_Transfer_Adapter_Http();
$upload->setDestination(URLgenerator::getTempFolder());
$upload->receive();
$info = $upload->getFileInfo();
var_dump($info);
return;
foreach($langs as $lang) {
$langId = $lang->getId();
try {
if($info["imageOff-$langId"]['tmp_name'] != '') {
$projectGroup->uploadImageOff($lang, $info["imageOff-$langId"]['tmp_name']);
unlink($info["imageOff-$langId"]['tmp_name']);
}
if($info["imageOn-$langId"]['tmp_name'] != '') {
$projectGroup->uploadImageOn($lang, $info["imageOn-$langId"]['tmp_name']);
unlink($info["imageOn-$langId"]['tmp_name']);
}
}
catch (Zend_File_Transfer_Exception $e) {
$this->_helper->redirector('image-upload', 'error', 'admin', array());
}
}
$this->_helper->redirector('index', 'project', 'admin', array());
}

The name of all the file fields is identical. Add a [] after it, i.e.
name="imageOff-<?php echo $langId; ?>[]"
Since all three have the same name, the last (empty) one overwrites the first ones.

Related

Session creation issue in shopping cart

In my code for shopping cart, sessions are not formed dynamically. I want to create session in this page and use sessions on the other page for item cart details.
How do I solve this problem?
<form method="post" action="men.php?action=add&id=<?php echo
$product['id']; ?>">
<img src="<?php echo $product['image'];?>">
<img class="hover-img" src="<?php echo $product['h_image'];?>" >
<p><?php echo $product['price'];?></p>
<h6><?php echo $product['name'];?></h6>
<input type="hidden" name="id" value="<?php echo $product['id']; ?>" />
<input type="hidden" name="hidden_name" value="<?php echo
$product['name']; ?>" />
<input type="hidden" name="hidden_price" value="<?php echo
$product['price']; ?>" />
<input type="hidden" name="hidden_image" value="<?php echo
$product['image']; ?>" />
Quantity <input type="text" class="form-control" name="qty" value="1" /><br>
<input type="submit" name="cart" value="Add to Cart" />
</form>
<?php
session_start();
if(isset($_POST['cart']))
{
// not working
if(isset($_SESSION['shopping_cart']))
{
$count= count($_SESSION['shopping_cart']);
$product_ids= array_column($_SESSION['shopping_cart'],'id');
if(!in_array(filter_input(INPUT_GET,'id'), $product_ids))
{
$_SESSION['shopping_cart'][$count]=array
(
'id'=> filter_input(INPUT_GET,'id'),
'name'=> filter_input(INPUT_POST,'hidden_name'),
'price'=> filter_input(INPUT_POST,'hidden_price'),
'quantity'=> filter_input(INPUT_POST,'qty'),
'image'=> filter_input(INPUT_POST,'hidden_image')
);
}
else
{
for($i=0; $i<count($product_ids); $i++)
{
if($product_ids[$i]==filter_input(INPUT_GET,'id'))
{
$_SESSION['shopping_cart'][$i]['quantity']+=
filter_input(INPUT_POST,'quantity');
}
}
}
}
else
{
}
}
?>
Sara. The session_start(); should be the first line in your document.
So, it should look like:
<?php session_start(); ?>
and then the rest of your work
NOTE : IF IT STILL NOT WORKING PLEASE PROVIDE THE ERROR.

codeigniter - get the value of checked checkboxes in the form in other way

Is there other way how to get the value of all checked checkboxes in my form and display it on the other view?
here's my input view
<div class="col-md-6">
<input type="checkbox" name="sen_id[]" class="Form-label-radio" value="<?php echo $sen['id']; ?>" id="<?php echo $sen['id']; ?>" />
<input type="checkbox" name="sen_id[]" class="Form-label-radio" value="<?php echo $sen['id']; ?>" id="<?php echo $sen['id']; ?>" />
<input type="checkbox" name="sen_id[]" class="Form-label-radio" value="<?php echo $sen['id']; ?>" id="<?php echo $sen['id']; ?>" />
</div>
my model
public function view_sen($sen_id){
$sen_id[] = $this->input->post('sen_id');
foreach ($sen_id as $id) {
$this->db->select('*')->from('party_candidates')->where('id', $id);
$query[] = $this->db->get()->result_array();
}
return $query;
}
my controller
public function balot_form(){
$sen_id[] = $this->input->post('sen_id');
$view_sen_votes = $this->vote->view_sen($sen_id);
$data = array("view_sen_votes" => $view_sen_votes);
$this->load->view("admin_dashboard/votation_page/balot_form_page", $data);
}
display results view
<div class="col-md-6">
<?php foreach($view_sen_votes as $sen => $id) { ?>
<div class="row">
<div class="form-group">
<label>Senators</label>
<input type="text" class="form-control" value="<?php echo $sen[$id]; ?>" name="sen_id" />
</div>
</div>
<?php } ?>
</div>
how to get the value and display it on my result view? thanks, regards..
Try like below to check key value.
print_r( $view_sen_votes );

How to restrict user to Upload only 3 images?

I'm creating a web app in Codeigniter. Here's the code in VIEW's.
<div class="bold light-gray">Add a photo or two!</div>
<p class="short">Or three, or more! Prospective Buyers love photos that highlight the features of your parts or gear.</p>
<form class="ajaxform" method="post" enctype="multipart/form-data" action='<?php echo site_url('UploadImage/upload_Image');?>'>
<input type="hidden" name="do" value="upload"/>
Upload your image <input type="file" name="Filedata" id="photo" onChange="abc()" />
<input type="hidden" name="timestamp" value="<?php echo $timestamp;?>" />
<input type="hidden" name="token" value="<?php echo md5('unique_salt' . $timestamp);?>" />
<input type="hidden" name="customer_id" value="<?php echo $customer['id'];?>" />
</form>
</div>
<!-- /.box.center -->
<?php echo form_open('/secure/add_item/'.$id, array('id'=>'listitem') ); ?>
<div id="img_cant"> <?php
if(isset($images) && !empty($images)) {
foreach($images as $key=>$img){
?>
<input type="hidden" value="<?php echo $img; ?>" name="images[]" id="hid_<?php echo $key; ?>">
<?php
}
}
?>
</div>
I want to allow the user to just add 3 or 4 images maximum. How to do that?
<?php $count=count($_FILES['name']);
if($count>3)
{
echo "Your error message";
}
else{
echo "your desired activity";
}?>
Using jquery you can call this function while submit your form
var file_nu = $("input:file")[0].files.length;
if(file_nu >3)//apply your condition here
{
return FALSE;
}else{
return TRUE;
}

Process uploaded file in Wordpress plugin development

I'm building my first WP plugin. Basically I should upload a CSV file and then read it values to store in a table at Wordpress DB. After read a lot of docs I follow this steps:
Create two files frequent-traveler.php where all the logic resides and ft_admin.php where the view part is.
This is the code I have in frequent-traveler.php:
// Create database tables and some others
function frequent_traveler_activation()
{
}
register_activation_hook(__FILE__, 'frequent_traveler_activation');
// Deletes database tables and some others
function frequent_traveler_deactivation()
{
}
register_deactivation_hook(__FILE__, 'frequent_traveler_deactivation');
function frequent_traveler_admin_actions()
{
add_options_page("Frequent Traveler Configuration", "Frequent Traveler Config", 'manage_options', "ftconfig", "frequent_traveler_admin");
}
add_action('admin_menu', 'frequent_traveler_admin_actions');
function frequent_traveler_admin()
{
include('ft_admin.php');
}
if (!empty($_POST) && isset($_POST['uploadfile_btn']) && check_admin_referer('name_of_my_action', 'wpnf_ft')) {
echo "entre";
}
else {
echo "Error";
}
And this is the code at ft_admin.php:
<?php
wp_enqueue_script('jquery-ui-datepicker');
wp_enqueue_style('jquery-style', 'http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.2/themes/smoothness/jquery-ui.css');
?>
<div class="wrap">
<?php echo "<h2>" . __('Frequent Traveler Configuration') . "</h2>"; ?>
<form name="frequent_traveler_form" method="post" action="<?php echo str_replace('%7E', '~', $_SERVER['REQUEST_URI']); ?>">
<input type="hidden" name="frequent_traveler_hidden" value="Y">
<?php echo "<h4>" . __('Common Settings') . "</h4>"; ?>
<p>
<?php _e("Default Conversion Value: "); ?><input type="text" name="ft_default" value="<?php echo $ft_default; ?>" size="5">
<?php _e("From Date: "); ?><input type="text" id="frequent_traveler_from_date" name="frequent_traveler_from_date" value="<?php echo $frequent_traveler_from_date ?>" class="datepicker" />
<?php _e("To Date: "); ?><input type="text" id="frequent_traveler_to_date" name="frequent_traveler_to_date" value="<?php echo $frequent_traveler_to_date; ?>" class="datepicker" />
</p>
<input type="submit" class="button-primary" name="Submit" value="<?php _e('Save Values') ?>" />
</form>
<hr/>
<?php ?>
<h2><?php echo __('Upload file to import') ?></h2>
<form name="uploadfile" id="uploadfile_form" method="POST" enctype="multipart/form-data" action="<?php echo str_replace('%7E', '~', $_SERVER['REQUEST_URI']) . '/uploadfile'; ?>" accept-charset="utf-8" >
<?php wp_nonce_field('name_of_my_action', 'wpnf_ft'); ?>
<p><?php echo __('Select file to upload') ?><input type="file" name="uploadfiles[]" id="uploadfiles" size="35" class="uploadfiles" /><p>
<input class="button-primary" type="submit" name="uploadfile_btn" id="uploadfile_btn" value="<?php echo __('Upload & Process') ?>" />
</form>
</div>
<script>
jQuery(document).ready(function() {
jQuery('.datepicker').datepicker({
dateFormat: 'dd/mm/yy'
});
});
</script>
As you may notice I'm trying to validate the uploaded file with this code:
if (!empty($_POST) && isset($_POST['uploadfile_btn']) && check_admin_referer('name_of_my_action', 'wpnf_ft')) {
echo "entre";
}
else {
echo "Error";
}
But I get always Error, why is that? What I'm doing wrong? Which is the best way to handle uploaded files?
You should not leave the check for if (!empty($_POST) outside any function, it can go inside the admin_menu output.
Also, the form actions should be left empty, so it posts to the same plugin page.
The targeted enqueue can be done in a standard way too and note the shortcut for jQuery $:
add_action( 'admin_menu', 'frequent_traveler_admin_actions' );
add_action( 'admin_enqueue_scripts', 'admin_enqueue' );
function frequent_traveler_admin_actions()
{
$my_hook = add_options_page("Frequent Traveler Configuration", "Frequent Traveler Config", 'manage_options', "ftconfig", "frequent_traveler_admin");
//var_dump($my_hook); die();
}
function admin_enqueue( $hook )
{
if( 'settings_page_ftconfig' !== $hook )
return;
wp_enqueue_script('jquery-ui-datepicker');
wp_enqueue_style('jquery-style', 'http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.2/themes/smoothness/jquery-ui.css');
}
function frequent_traveler_admin()
{
if (!empty($_POST) && isset($_POST['uploadfile_btn']) && check_admin_referer('name_of_my_action', 'wpnf_ft')) {
echo "<h1>entre</h1>";
}
else {
echo "<h1>Error</h1>";
}
$ft_default = '';
$frequent_traveler_from_date = '';
$frequent_traveler_to_date = '';
?>
<div class="wrap">
<?php echo "<h2>" . __('Frequent Traveler Configuration') . "</h2>"; ?>
<form name="frequent_traveler_form" method="post" action="">
<input type="hidden" name="frequent_traveler_hidden" value="Y">
<?php echo "<h4>" . __('Common Settings') . "</h4>"; ?>
<p>
<?php _e("Default Conversion Value: "); ?><input type="text" name="ft_default" value="<?php echo $ft_default; ?>" size="5">
<?php _e("From Date: "); ?><input type="text" id="frequent_traveler_from_date" name="frequent_traveler_from_date" value="<?php echo $frequent_traveler_from_date ?>" class="datepicker" />
<?php _e("To Date: "); ?><input type="text" id="frequent_traveler_to_date" name="frequent_traveler_to_date" value="<?php echo $frequent_traveler_to_date; ?>" class="datepicker" />
</p>
<input type="submit" class="button-primary" name="Submit" value="<?php _e('Save Values') ?>" />
</form>
<hr/>
<?php ?>
<h2><?php echo __('Upload file to import') ?></h2>
<form name="uploadfile" id="uploadfile_form" method="POST" enctype="multipart/form-data" action="" accept-charset="utf-8" >
<?php wp_nonce_field('name_of_my_action', 'wpnf_ft'); ?>
<p><?php echo __('Select file to upload') ?><input type="file" name="uploadfiles[]" id="uploadfiles" size="35" class="uploadfiles" /><p>
<input class="button-primary" type="submit" name="uploadfile_btn" id="uploadfile_btn" value="<?php echo __('Upload & Process') ?>" />
</form>
</div>
<script>
jQuery(document).ready(function($) {
$('.datepicker').datepicker({
dateFormat: 'dd/mm/yy'
});
});
</script>
<?php
}

Post data not being sent by a code igniter form

I've got a slight problem with my CI2.0.2 application.
From what I can tell, it is not getting any post data from a form and so is failing to run an update function, though most pages work fine.
The form is as follows:
<?php echo form_open('job/update/', array('id' => 'update', 'name' => 'update')); ?>
<div class="images">
<?php echo img('application/images/updateJob.png');?>
</div>
<h1>Update Job</h1>
<br><br><br><br>
<div class="fieldset">
<?php
if (isset($error))
{
echo '<div id ="error">';
echo '<h2>Error:</h2>';
echo '<p>'.$error.'</p>';
echo '</div>';
}
?>
<input type="hidden" name="c" id='c' value="<?php if(!empty($view[0])) { echo $view[0]; } else { echo "0"; } ?>">
<div class="left">
<h4>Job Details:</h4>
<div>
<label for="job_number">Job No:</label>
<input type="text" name="job_number" id="job_number" value="<?php echo $job['jobno']; ?>" disabled="disabled">
</div>
<div>
<label for="date">Date:</label>
<input type="text" name="date" id="date" value="<?php echo $job['datetime']->format('d-M-Y'); ?>">
</div>
<div>
<label for="council">Council:</label>
<input type="text" name="council" id="council" value="<?php echo htmlspecialchars($job['council']); ?>"> *
</div>
<div>
<label for="dano">DA No:</label>
<input type="text" name="dano" id="dano" value="<?php echo htmlspecialchars($job['dano']); ?>">
</div>
<div>
<label for="client">Client:</label>
<input type="text" name="client" id="client" value="<?php echo $job['clientid']; ?>" disabled="disabled">
</div>
</div>
<div class="right" style="margin-left: 390px;">
<h4>Location:</h4>
<label for="street_no">Street No:</label>
<input type="text" name="street_no" id="street_no" value="<?php echo $address['streetno']; ?>"> *
<br>
<label for="street_name">Street Name:</label>
<input type="text" name="street_name" id="street_name" value="<?php echo $address['streetname']; ?>"> *
<br>
<label for="street_type">Street Type:</label>
<select name="street_type" id="street_type">
<?php
foreach ($street_types as $type)
{
echo '<option';
if ($type == $address['streettype']) echo ' selected="selected"';
echo '>'.$type.'</option>';
}
?>
</select> *
<br>
<label for="suburb">Suburb:</label>
<input type="text" name="suburb" id="suburb" value="<?php echo $address['suburb']; ?>"> *
<br>
<label for="postcode">Postcode:</label>
<input type="text" name="postcode" id="postcode" value="<?php echo $address['postcode']; ?>"> *
<br>
<label for="state">State:</label>
<input type="text" name="state" id="state" value="<?php echo $address['state']; ?>"> *
<br>
<label for="country">Country:</label>
<input type="text" name="country" id="country" value="<?php echo $address['country']; ?>">
<br>
<label for="dp">DP:</label>
<input type="text" name="dp" id="dp" value="<?php echo $address['dp']; ?>">
<br>
<label for="lot_no">Lot No:</label>
<input type="text" name="lot_no" id="lot_no" value="<?php echo $address['lotno']; ?>">
<br>
<label for="po_box">PO Box:</label>
<input type="text" name="po_box" id="po_box" value="<?php echo $address['pobox']; ?>">
</div>
<div>
<input type="submit" id="submit" name="submit" value="Submit" class="button">
<p>* = Required fields</p>
</div>
</div>
<?php echo form_close();?>
And the update section of the controller is as follows:
/**
* Update an existing job.
* #param int $job_number The number of the job to update.
*/
public function update($job_number=0)
{
$this->load->model('Job_model');
$job = array();
$address = array();
// Get post data.
$job['joblocation'] = '';
$job['jobno'] = $this->input->post('job_number');
$job['datetime'] = new DateTime($this->input->post('date'));
$job['dano'] = $this->input->post('dano');
$job['council'] = $this->input->post('council');
echo $job['jobno'];
echo $job['dano'];
echo $job['council'];
$address['streetno'] = $this->input->post('street_no');
$address['streetname'] = $this->input->post('street_name');
$address['suburb'] = $this->input->post('suburb');
$address['country'] = $this->input->post('country');
$address['postcode'] = $this->input->post('postcode');
$address['state'] = $this->input->post('state');
$address['dp'] = $this->input->post('dp');
$address['lotno'] = $this->input->post('lot_no');
$address['pobox'] = $this->input->post('po_box');
$address['streettype'] = $this->input->post('street_type');
echo "here2";
if (isset($_POST['submit']))
{
$this->Job_model->update($job);
echo "here";
redirect('job/');
}
// Otherwise, get the data from the database.
else
{
$job = $this->Job_model->search($job_number);
$job = $job[0];
$job['datetime'] = new DateTime($job['datetime']);
$address = $this->Job_model->get_address($job['joblocation']);
$address = $address[0];
}
// Get the street types.
$street_types = array();
$this->load->model('staff_model');
$streets = $this->staff_model->get_street_types();
foreach ($streets as $street)
{
$street_types[$street['streettype']] = $street['streettype'];
}
// Load the client list.
$clients = array();
$this->load->model('client_model');
$people = $this->client_model->get_client_person_list();
$companies = $this->client_model->get_client_company_list();
// Allocate view data.
$viewdata = array();
$viewdata['job'] = $job;
$viewdata['street_types'] = $street_types;
$viewdata['address'] = $address;
$viewdata['people'] = $people;
$viewdata['companies'] = $companies;
$this->layout->view('job/update', $viewdata);
}
Any ideas why this might be happening?
Thanks in advance,
James
Try changing the quotes you're using on this line:
<input type="hidden" name="c" id='c' value="<?php if(!empty($view[0])) { echo $view[0]; } else { echo '0'; } ?>" />
The way you had it with the double quotes around the 0 you were breaking that php because the first one would be closing the opening value quote.

Categories