I would try to write a script where users make their own questions. I was able to safely carry the fields such as title, description and some other details. But I want to make users upload images (also for the moment), unfortunately I can not, I ask therefore help to you.
Paste the code:
<?php require_once 'app/init.php';
if (isset($_POST['submit']))
{
$validator = Validator::make(
array(
'title' => $_POST['title'],
'question' => $_POST['question'],
),
array(
'title' => 'required',
'question' => 'required',
)
);
if ($validator->fails())
{
$errors = $validator->messages();
}
else
{
DB::table('question')->insert(
array(
'q_title' => escape($_POST['title']),
'q_desc' => escape($_POST['question']),
'page_title' => escape($_POST['title']),
'h_image' => escape($_POST['filename']),
'user_id' => escape($_POST['userid']),
'user_name' => escape($_POST['username'])
)
);
return redirect_to('question.php');
}
}
?>
<?php echo View::make('header')->render() ?>
<div class="row">
<div class="col-md-8">
<h3 class="page-header">Question</h3>
<!-- Display errors, if are any -->
<?php if (isset($errors)): ?>
<ul>
<?php foreach ($messages->all('<li>:message</li>') as $message) {
echo $message;
} ?>
</ul>
<?php endif ?>
<!-- Form -->
<?php if (Auth::check()): ?>
<form action="" method="POST" enctype="multipart/form-data">
<label for="title">Title</label>
<div class="input-group input-group-lg">
<input type="text" name="title" class="form-control" placeholder="Title" aria-describedby="basic-addon2">
<span class="input-group-addon" id="basic-addon2">?</span>
</div><br />
<label for="question">Description</label>
<textarea class="form-control" name="question" rows="4" cols="10" placeholder="Description..."></textarea><br />
<label for="question">Image</label>
<input type="file" name="filename" id="image" size="40">
<input type="hidden" name="userid" value="<?php echo Auth::user()->id ?>">
<input type="hidden" name="username" value="<?php echo Auth::user()->display_name ?>"><br />
<button type="submit" name="submit" class="btn btn-md btn-success">Help me!</button>
</form>
<?php else: ?>
<p>
<!-- <?php _e('comments.logged_in', array('attrs' => 'href="login.php"')) ?> -->
<?php _e('comments.logged_in', array('attrs' => 'href="#" class="login-modal" data-target="#loginModal"')) ?>
</p>
<?php endif ?>
</div>
</div>
<?php echo View::make('footer')->render() ?>
I wish the pictures were included in a directory (or if you advise me, even in the database, what is better?).
I hope for your help, thank you.
Simple Example:
HTML
<form action='upload.php' method='post' enctype='multipart/form-data'>
<input name='filename' type='file' />
<input name='btnSubmit' type='submit' />
</form>
PHP
<?php
$filename = $_FILES["filename"]["name"];
$tmpFilename = $_FILES["filename"]["tmp_name"];
$path = "path/to/upload/" . $filename;
if(is_uploaded_file($tmpFilename)){ // check if file is uploaded
if(move_uploaded_file($tmpFilename, $path)){ // now move the uploaded file to path (directory)
echo "File uploaded!";
}
}
?>
When you submit the form,
the $_FILES global variable is created with the infomation on the file!
You can see that's in that variable with this function :
<?php
print_r($_FILES); // this will echo any array
?>
First you should submit the post (normal data & file data)
$data = $_POST['data'];
$files = $_FILES['file'];
var_dump the $files, and you will see a bunch of data, where you can do a lot with in statements.
Then you should check if there is actually a file uploaded:
(file_exists($_FILES['file']['tmp_name'])) || (is_uploaded_file($_FILES['file']['tmp_name']
If a file is uploaded, you could check if there is an existing directory to store the files in:
$directory = 'your path here';
$filename = $directory.'/'. $_FILES['file']['name'];
if (!file_exists($directory)) {
mkdir($directory, 0777, true);
}
move_uploaded_file($_FILES['file']['tmp_name'], $filename);
I hope this helped you a little...
Related
My web app only allows for one file upload which is an issue because users want to be able to submit 2 or more files when necessary.
Please how do I allow multiple file uploads and displaying those files to be downloaded by another type of users?
MODEL
public function upload_docs($data)
{
$this->db->where('homework_id',$data['homework_id']);
$this->db->where('student_id',$data['student_id']);
$q = $this->db->get('submit_assignment');
if ( $q->num_rows() > 0 )
{
$this->db->where('homework_id',$data['homework_id']);
$this->db->where('student_id',$data['student_id']);
$this->db->update('submit_assignment',$data);
} else {
$this->db->insert('submit_assignment',$data);
}
}
CONTROLLER
public function upload_docs()
{
$homework_id = $_REQUEST['homework_id'];
$student_id =$_REQUEST['student_id'];
$data['homework_id'] = $homework_id;
$data['student_id'] = $student_id;
$data['message'] = $_REQUEST['message'];
// $data['id']=$_POST['assigment_id'];
$is_required=$this->homework_model->check_assignment($homework_id,$student_id);
$this->form_validation->set_rules('message', $this->lang->line('message'), 'trim|required|xss_clean');
$this->form_validation->set_rules('file', $this->lang->line('attach_document'), 'trim|xss_clean|callback_handle_upload['.$is_required.']');
if ($this->form_validation->run() == FALSE) {
$msg=array(
'message'=>form_error('message'),
'file'=>form_error('file'),
);
$array = array('status' => 'fail', 'error' => $msg, 'message' => '');
}else{
if (isset($_FILES["file"]) && !empty($_FILES['file']['name'])) {
$time = md5($_FILES["file"]['name'] . microtime());
$fileInfo = pathinfo($_FILES["file"]["name"]);
$img_name = $time . '.' . $fileInfo['extension'];
$data['docs'] = $img_name;
move_uploaded_file($_FILES["file"]["tmp_name"], "./uploads/homework/assignment/" . $data['docs']);
$data['file_name']=$_FILES["file"]['name'];
$this->homework_model->upload_docs($data);
}
$array = array('status' => 'success', 'error' => '', 'message' => $this->lang->line('success_message'));
}
echo json_encode($array);
}
public function handle_upload($str,$is_required)
{
$image_validate = $this->config->item('file_validate');
if (isset($_FILES["file"]) && !empty($_FILES['file']['name']) && $_FILES["file"]["size"] > 0) {
$file_type = $_FILES["file"]['type'];
$file_size = $_FILES["file"]["size"];
$file_name = $_FILES["file"]["name"];
$allowed_extension = $image_validate['allowed_extension'];
$ext = pathinfo($file_name, PATHINFO_EXTENSION);
$allowed_mime_type = $image_validate['allowed_mime_type'];
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$mtype = finfo_file($finfo, $_FILES['file']['tmp_name']);
finfo_close($finfo);
if (!in_array($mtype, $allowed_mime_type)) {
$this->form_validation->set_message('handle_upload', 'File Type Not Allowed');
return false;
}
if (!in_array($ext, $allowed_extension) || !in_array($file_type, $allowed_mime_type)) {
$this->form_validation->set_message('handle_upload', 'Extension Not Allowed');
return false;
}
if ($file_size > $image_validate['upload_size']) {
$this->form_validation->set_message('handle_upload', $this->lang->line('file_size_shoud_be_less_than') . number_format($image_validate['upload_size'] / 1048576, 2) . " MB");
return false;
}
return true;
} else {
if($is_required==0){
$this->form_validation->set_message('handle_upload', 'Please choose a file to upload.');
return false;
}else{
return true;
}
}
}
VIEW
<form id="upload" role="form" method="post" class="ptt10" enctype="multipart/form-data" action="upload_docs">
<div class="modal-body pt0">
<div class="row">
<input type="hidden" name="student_id" value="<?php echo $student_id; ?>">
<input type="hidden" id="homework_id" name="homework_id">
<input type="hidden" id="assigment_id" name="assigment_id">
<div class="col-sm-12">
<div class="form-group">
<label for="pwd"><?php echo $this->lang->line('message'); ?></label>
<textarea type="text" id="assigment_message" name="message" class="form-control "></textarea>
</div>
</div>
<div class="col-sm-12">
<div class="form-group">
<label for="pwd"><?php echo $this->lang->line('attach_document'); ?></label>
<input type="file" id="file" name="file" class="form-control filestyle">
</div>
</div>
<p id="uploaded_docs"></p>
</div>
</div>
<div class="box-footer">
<div class="" id="footer_area">
<button type="submit" form="upload" class="btn btn-info pull-right" id="submit" data-loading-text='Please wait...'><?php echo $this->lang->line('save'); ?></button>
</div>
</div>
</form>
This is displayed like this
controller
public function assigmnetDownload($doc)
{
$this->load->helper('download');
$name = $this->uri->segment(5);
$ext = explode(".", $name);
$filepath = "./uploads/homework/assignment/" . $doc;
$data = file_get_contents($filepath);
force_download($name, $data);
}
view
<table class="table table-hover table-striped table-bordered example">
<thead>
<tr>
<th><?php echo $this->lang->line('name') ?></th>
<th><?php echo $this->lang->line('message') ?></th>
<th class="text-right"><?php echo $this->lang->line('action') ?></th>
</tr>
</thead>
<tbody id="homework_docs_result">
</tbody>
</table>
Thanks to all helping me out. I did as you guys directed. However, I noticed I'm able to select multiple files now but gets stuck on the save button.
When I click, nothing happens. Data is not submitted.
This is what I have done so far
controller
if (isset($_FILES["file"])){
foreach($_FILES["file"] as $file){
if(!empty($file["name"])){
$time = md5($file["file"]['name'] . microtime());
$fileInfo = pathinfo($file["file"]["name"]);
$img_name = $time . '.' . $fileInfo['extension'];
$data['docs'] = $img_name;
move_uploaded_file($file["file"]["tmp_name"], "./uploads/homework/assignment/" . $data['docs']);
$data['file_name']=$file["file"]['name'];
$this->homework_model->upload_docs($data);
}
$array = array('status' => 'success', 'error' => '', 'message' => $this->lang->line('success_message'));
}
echo json_encode($array);
}
}
}
view
<form id="upload" role="form" method="post" class="ptt10" enctype="multipart/form-data" action="uploaded_docs">
<input type="file" multiple="" id="file" name="file[]" class="form-control filestyle">
<button type="submit" form="upload" class="btn btn-info pull-right" id="submit" data-loading-text=' Please wait'><?php echo $this->lang->line('save'); ?></button>
</form>
HTML
<input type="file" id="file" name="file[]" class="form-control filestyle">
Setting name to file[] will accept array of files
PHP
if (isset($_FILES["file"]){
foreach($_FILES["file"] as $file){ //this loop will get one file from 'file[]' array at a time
if(!empty($file["name"])){
//your alreay written code
//replace $_FILES["file"] with $file
}
}
}
Hope this helpful :)
Some tips, if help. Do not forget the vote to strengthen. Let's go...
<form method="post" action="upload_docs" enctype="multipart/form-data">
<input name="filesToUpload[]" type="file" multiple="" />
</form>
On your controller it's simple ...
if(isset($_FILES['filesToUpload'])) {
foreach ($_FILES['filesToUpload'] as $fileUp) {
$time = md5($fileUp["file"]['name'].microtime());
$fileInfo = pathinfo($fileUp["file"]["name"]);
$img_name = "{$time}.{$fileInfo['extension']}";
$data['docs'] = $img_name;
move_uploaded_file($fileUp["file"]["tmp_name"], "./uploads/homework/assignment/{$data['docs']}");
$data['file_name']=$fileUp["file"]['name'];
$this->homework_model->upload_docs($data);
}
}
To display multiple files you need to use the same concept as uploading working with
foreach($files as $file){ ... }
I hope I helped. Success!
Setting attribute multiple will allow selecting multiple files at once by pressing ctrl, setting the name as an array(file[]) will allow more than files name to be stored.
<input type="file" id="file" name="file[]" class="form-control filestyle" multiple>
However, if you don't want the user to upload multiple files at once then you'll have to make multiple input fields with the same name as an array(file[])
<input type="file" id="file" name="file[]" class="form-control filestyle">
<input type="file" id="file1" name="file[]" class="form-control filestyle">
...
...
In your controller, you'll have to traverse through each element as it is now an array.
foreach($_FILES["file"]['name'] as $file){ //single element
echo $file; // returns the name of file
// your-logic-to-upload
}
If you're having trouble uploading multiple files, see here, here and here.
Hope it helps you.
I’m working in form to upload image in project folder and store the other data into Database. when I try to submit this form data was submitted and image was not uploaded and one more the image was uploaded and not all of data was stored in database this is my code
model:
function update_news($data) {
extract($data);
$this->db->where('news_id', $news_id);
$this->db->update($table_name, array('title_en' => $title_en, 'title_ar' => $title_ar,'news_date' => $news_date,'image' => $image,'is_visible' => $is_visible,'main_news' => $main_news));
return true;
}
controller
public function update() {
$filename = $this->input->post('image');
$date = DateTime::createFromFormat("Y-m-d", $this->input->post('news_date'));
$data = array(
'table_name' => 'news', // pass the real table name
'news_id' => $this->input->post('news_id'),
'title_en' => $this->input->post('title_en'),
'title_ar' => $this->input->post('title_ar'),
'news_date' => $this->input->post('news_date'),
'image' => $date->format("Y") . '/' . $this->input->post('image'),
'is_visible' => $this->input->post('is_visible'),
'main_news' => $this->input->post('main_news')
);
$this->m_news_crud->update_news($data);
$folderName = 'assets/images/press-news/2015';
$config['upload_path'] = "$folderName";
if (!is_dir($folderName)) {
mkdir($folderName, 0777, TRUE);
}
$config['file_name'] = $this->input->post('image');
$config['overwrite'] = TRUE;
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 100;
$config['max_width'] = 1024;
$config['max_height'] = 768;
$this->load->library('upload', $config);
$this->upload->initialize($config);
if (!$this->upload->do_upload('userfile','image')) {
$error = array('error' => $this->upload->display_errors());
$data['error'] = $error;
$data['title_en'] = $this->input->post('title_en');
$data['title_ar'] = $this->input->post('title_ar');
$data['news_date'] = $this->input->post('news_date');
$data['image'] = base_url('assets/images/press-news/' . $date->format("Y")) . '/' . $this->input->post('image');
$data['is_visible'] = $this->input->post('is_visible');
$data['main_news'] = $this->input->post('main_news');
$data['news_id'] = $this->input->post('news_id');
$this->load->view('admin/news/d_admin_header');
$this->load->view('admin/news/vcrudedit_news', $data);
$this->load->view('admin/news/d_admin_footer');
} else {
$data = array('upload_data' => $this->upload->data());
redirect('c_news_crud/get_news_data', $data);
echo $filename;
}
}
view:
<div class="container">
<div class="col-md-9">
<?php echo form_open('C_news_crud/update', 'role="form" style="margin-top: 50px;"'); ?>
<div class="form-group">
<label for="title_en">Title Name English</label>
<input type="text" class="form-control" id="title_en" name="title_en" value="<?php echo $title_en ?>">
</div>
<div class="form-group">
<label for="title_ar">Title Name Arabic</label>
<input type="text" class="form-control" id="title_ar" name="title_ar" value="<?php echo $title_ar ?>">
</div>
<div class="form-group">
<div class="col-md-4 col-md-offset-4">
<label for="is_visible">is visible</label>
<?php
if ($is_visible == 1) {
$check_visible = 'checked="checked"';
} else {
$check_visible = ' ';
}
?>
<input role="checkbox" type="checkbox" id="is_visible" class="cbox" <?php echo $check_visible; ?> name="is_visible" value="<?php echo $is_visible; ?>">
</div>
<div class="col-md-4">
<label for="main_news">Main Arabic</label>
<?php
if ($main_news == 1) {
$check_main = 'checked="checked"';
} else {
$check_main = ' ';
}
?>
<input role="checkbox" type="checkbox" id="main_news" class="cbox" <?php echo $check_main; ?> name="main_news" value="<?php echo $main_news; ?>">
</div>
</div>
<div class="form-group">
<label for="news_date">News date</label>
<input type="date" class="form-control" id="news_date" name="news_date" value="<?php echo $news_date ?>">
</div>
<div class="form-group">
<label for="image">image</label>
<input type="file" class="form-control" id="image" name="image" value="<?php echo $image ?>">
</div>
<input type="hidden" name="news_id" value="<?php echo $news_id ?>" />
<input type="submit" name="save" class="btn btn-primary" value="Update">
<button type="button" onclick="location.href = '<?php echo site_url('C_news_crud/get_news_data') ?>'" class="btn btn-success">Back</button>
</form>
<?php echo form_close(); ?>
</div>
Make sure the path to the uploading folder is correct and it has write permission.
Make sure you are referring to the correct file input name:
$config['file_name'] = $_FILES['image']['name'];
set your uploading directory as ./assets/images/press-news/2015/
You need to display the error you get so that you know what is causing the error!
if ( ! $this->upload->do_upload()){
$error = array('error' => $this->upload->display_errors());
$this->load->view('upload_form', $error);
}
Codigniter's Upload Class
I have a problem with the function for upload video and images. I can upload images but not video I don' know why. In the and more code for controller and form.
Code in user_models php:
function newp($title,$post,$image,$video,$cat){
$id=$this->session->userdata('userid');
if($video!="")
{
$data=array(
'userid'=>$id,
'title'=>$title,
'forumpost'=>$post,
'categoryid'=>$cat,
'videourl'=>$video
);
}
Image:
else if($image!=""){
$data=array(
'userid'=>$id,
'title'=>$title,
'forumpost'=>$post,
'categoryid'=>$cat,
'imageurl'=>$image
);
}else{
$data=array(
'userid'=>$id,
'title'=>$title,
'forumpost'=>$post,
'categoryid'=>$cat,
);}
$this->db->insert('forum',$data);
}
}
Form:
<form method="post" enctype="multipart/form-data" action="<?php echo base_url();?>index.php/forum/newpk">
<div style="float:left;width: 95%;margin-top:5px;margin-left: 15px">
<h3> Add A Post</h3>
<div class="list-group" >
<input type="text" required name="title" id="title" class="form-control" placeholder="Title of the Post">
</div>
<div class="list-group">
<textarea name="post" class="form-control" style="min-height: 150px;" placeholder="Post">
</textarea>
</div>
<div class="list-group">
<span style="float:left;display:inline-block;"><p style="font-size:15px">Video File</p> <input type="file" name="video" accept="video/*" /></span>
<p style="font-size:15px;margin-left:10%;width: 32px;display:inline-block;">OR</p>
Image File
</div>
<div class="listgroup">
To which category it belongs
<select name="cat"><?php foreach($cato as $cat){ ?><option name="cat" value="<?php echo $cat->categoryid;?>"><?php echo $cat->categoryname;?></option><?php }?></select>
</div>
<div class="list-group" style="margin-top: 70px;margin-left: 35%;">
<button type="submit" style="width: 159px;height: 47px;line-height: 2.3;font-size: 17px;" class="btn btn-success">Submit</button>
</div>
</div>
</form>
</div>
Controller:
public function newpk(){
if($this->session->userdata('userid')){
$title=$this->security->xss_clean($this->input->post('title'));
$post=$this->security->xss_clean($this->input->post('post'));
$cat=$this->security->xss_clean($this->input->post('cat'));
if($cat=="" || $post=="" || $title=="")
{
echo "Required field cannot be empty";
die();
}
$themepicid="";
$this->load->model('forum_model');
Video part:
if(isset($_FILES['video']) && $_FILES['video']['size']>0){
$pktid=$this->idGenerator();
$pathh='assets/video/';
$themepicid=$this->session->userdata('userid');
$type= pathinfo($_FILES['video']['name'], PATHINFO_EXTENSION);
$pic=$themepicid;
$themepicid=$themepicid.$pktid.".".$type;
move_uploaded_file($_FILES['video']['tmp_name'], $pathh.$themepicid);
$image="";
$this->forum_model->newp($title,$post,$image,$themepicid,$cat); // How the forum likes
Image part:
}
else if(isset($_FILES['image']) && $_FILES['image']['size']>0){
$pathh='assets/images/';
$themepicid=$this->session->userdata('userid');
$type= pathinfo($_FILES['image']['name'], PATHINFO_EXTENSION);
$pktid=$this->idGenerator();
$pic=$themepicid;
$themepicid=$themepicid.$pktid.".".$type;
move_uploaded_file($_FILES['image']['tmp_name'], $pathh.$themepicid);
$image="";
$this->forum_model->newp($title,$post,$themepicid,$image,$cat);
}
else{
$image="";
$this->forum_model->newp($title,$post,$image,$image,$cat);
}
redirect('index.php/forum','refresh');
}
else{
redirect('index.php/user','refresh');
}
}
}
Maybe your video size larger than max size upload config in php.ini
You can use upload library from CI, it's easy to use and help you prevent error
http://ellislab.com/codeigniter/user-guide/libraries/file_uploading.html
If you using upload lib of CI, dont forget config allowed types
i am uploading the files to PHP script. it process and give response. i want to load that response in the div or frame on same web page. now the following script giving response in the blank web page. please help me to solve this.
php:
<html>
<head>
<script type="text/javascript">
function init() {
if(top.uploadDone) top.uploadDone();
}
window.onload=init;
</script>
<body>
<?php
$fn = (isset($_SERVER['HTTP_X_FILENAME']) ? $_SERVER['HTTP_X_FILENAME'] : false);
if ($fn) {
file_put_contents(
'uploads/' . $fn,
file_get_contents('php://input')
);
echo "$fn uploaded";
exit();
}
else {
// form submit
$files = $_FILES['fileselect'];
//print $files['name'];
foreach ($files['error'] as $id => $err) {
if ($err == UPLOAD_ERR_OK) {
$fn = $files['name'][$id];
move_uploaded_file(
$files['tmp_name'][$id],
'uploads/' . $fn
);
if($id==1)
{
$filename = 'uploads/' . $fn;
$fp = fopen( $filename, "r" ) or die("Couldn't open $filename");
$line = fgets($fp);
fclose($fp);
$arr = split(",",$line);
for($i=2;$i< count($arr);$i++)
{
print '<input type="checkbox" name="traits" value="1">'.$arr[$i]."<br>";
}
}
}
}
}
?>
</body>
</html>
Jquery:
function init() {
document.getElementById("upload").onsubmit=function() {
document.getElementById("upload").target = "Analysis";
document.getElementById("Analysis").onload = uploadDone; }
}
function uploadDone() { //Function will be called when iframe is loaded
var ret = frames['Analysis'].document.getElementsByTagName("body")[0].innerHTML;
}
HTML script:
<div id="content_column">
<form id="upload" action="upload.php" method="POST" enctype="multipart/form-data">
<fieldset>
<div id="Geno" style="display:none">
<strong>Select Genotypic File</strong>
<input type="hidden" id="MAX_FILE_SIZE" name="MAX_FILE_SIZE" value="300000" />
<div>
<input type="file" id="fileselect" name="fileselect[]" />
</div>
</div>
<P>
<div id="Peno" style="display:none">
<strong>Select Penotypic File</strong>
<div>
<input type="file" id="fileselect1" name="fileselect[]" />
</div>
</div>
<P>
<div id="Other" style="display:none">
<strong>Select Other Files*</strong>
<div>
<input type="file" id="fileselect2" name="fileselect[]" multiple="multiple" />
</div>
</div>
<div id="submitbutton" style="display:none">
<INPUT TYPE="Submit" VALUE="Upload" />
</div>
</fieldset>
</form>
<iframe id="Analysis" name="Analysis" src="" ></iframe>
</div>
Thank you in adavance. .
Kindly have a visit on following link: Demo link for problem
You can view that form is not showing submitted values and not uploading file.
Following is form code :
<form id="frm" name="frm" method="post" enctype="multipart/form-data">
<fieldset class="fieldset2">
<legend class="legend2">Add Bid Type</legend>
<div class="form_fields">
<p>
<label for="subject" class="label2">Bid Type:</label>
<input type="input" id="type" name="type" class="textfield2" />
</p>
<p>
<label for="subject" class="label2">Bid Type Code:</label>
<input type="input" id="code" name="code" class="textfield2" />
</p>
<p>
<label for="description" class="label2">Bid Type Description:</label>
<textarea id="description" name="description" class="textarea2"></textarea>
</p>
<p>
<label for="userfile" class="label2">Icon:</label>
<input type="file" id="userfile" name="userfile" class="input_file" />
</p>
</div>
</fieldset>
<fieldset class="none">
<p>
<input type="submit" id="btnsubmit" name="btnsubmit" class="btn" value="Add" />
<input type="reset" id="btnreset" name="btnreset" class="btn" value="Reset" />
</p>
</fieldset>
</form>`
And following is controller code :
function create() {
$data = '';
echo '<pre>';
echo 'below is Post Fields Data:<br>';
print_r($this->input->post());
echo '<br>Post Fields Data Ends: ';
echo '</pre>';
$this->form_validation->set_rules('type', 'Bid Type', 'trim|required|xss_clean');
$this->form_validation->set_rules('code', 'Bid Type Code', 'trim|xss_clean');
$this->form_validation->set_rules('description', 'Bid Type description', 'trim|xss_clean');
$data['errors'] = array();
if ($this->form_validation->run()) {
$data = array(
'code' => $this->form_validation->set_value('code'),
'type' => $this->form_validation->set_value('type'),
'description' => $this->form_validation->set_value('description'),
);
if (!is_null($this->bid_types->create_bid_type($data))){
$data['errors']['success'] = 'Record Successfully Added!';
} else {
$errors = $this->bid_types->get_error_message();
foreach ($errors as $k => $v) $data['errors'][$k] = $this->lang->line($v);
}
$config['upload_path'] = base_url().'resource/images/uploads/';
$config['allowed_types'] = 'gif|jpg|png';
//$config['max_size'] = '100';
//$config['max_width'] = '1024';
//$config['max_height'] = '768';
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload())
{
$data['errors']['upload'] = 'Not Uploaded';
//$data['errors']['upload'] = $this->upload->display_errors();
//$error = array('error' => $this->upload->display_errors());
//$this->load->view('upload_form', $error);
}
else
{
$data['errors']['upload'] = 'Yes Uploaded';
//$data['errors']['upload'] = $this->upload->data();
//$data = array('upload_data' => $this->upload->data());
//$this->load->view('upload_success', $data);
}
echo '<pre>';
print_r($this->upload->data());
echo '</pre>';
} /* END OF FORM VALIDATRION RUN */
if(!$this->input->is_ajax_request() && !$this->input->get_post('is_ajax'))
{
$this->load->view('bend/bid_type/create', $data);
}
} /* END OF FUNCTION CREATE */
Can some one guide me what and where Iam doing wrong and how it can be rectrified.
File data appears in $_FILE not in $_POST.
I think this line is your problem:
$config['upload_path'] = base_url().'resource/images/uploads/';
This $config['upload_path'] should point to a file system path, not a URL.
Try something more like:
$config['upload_path'] = realpath(APPPATH . '../resource/images/uploads/');
which will start from your application folder, up one folder then into resource/images/uploads
change it accordingly if its somewhere else.
p.s. Also check your write permissions.
put $this->output->enable_profiler(TRUE) on the first line of create() function, it will show you very usefull information $_POST global to debug your code like
Charles shows that the request from the form submit does contain the post data.
Does anything else happen before the create() function that could wipe the POST data?
I would add an action attribute to the form using echo form_open('/phpdemo/b1/admin/bid_type/create') as described in the user manual.
you can not use
$this->form_validation->set_rules('code', 'Bid Type Code', 'trim|xss_clean');
in multipart , so you must create your own function for validation and use callbacks