I am trying to implement a post functionality and want to pick message and image from a php view. My message functionality is working good. But on image upload i receive an error You did not select a file to upload. This is my controller function
function post_func()
{
session_start();
echo $post_message=$_POST['post'];
echo $share_with=$_POST['share_with'];
echo $image=$_POST['image'];
if($image==null){
echo "<br/>no image<br/>";
}
else{
////////////////////////////////////////////////////////////////////////////////////////
$config['upload_path'] = './application/css';
$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())
{
$error = array('error' => $this->upload->display_errors());
echo "<br/>";
echo $this->upload->display_errors();
echo "<br/> image error<br/>";
}
else
{
echo "<br/> reached <br/>";
session_start();
$this->membership_model->insert_images($this->upload->data(),$email);
$data = array('upload_data' => $this->upload->data());
echo "<br/ problem<br/>";
}
////////////////////////////////////////////////////////////////////////////////////////
}
$public;
if($share_with=="public"){
echo "1";
$public=true;
}else{
echo "0";
$public=false;
}echo "-----------------------------<br/>";
echo $user=$this->session->userdata('user_identification');
$data = array
(
'userid'=> $user,
'public' => $public,
'message' => $post_message,
'picname' => "None"
);
$this->load->model('membership_model');
$this->membership_model->add_message($data);
echo "</br>";
echo $user=$this->session->userdata('user_identification');
}
This is my view.
<?php echo form_open('search/post_func');?>
<!--<form id="loginForm" action="../search/post_func" method="post" enctype="multipart/form-data" >-->
<div id="your_post">
<div id="post_image">
<img id ="post_img" src="<?php echo $this->config->item('base_url'); ?><?php echo '/application/css/'. $img ?>"/>
</div>
<textarea name="post" rows="5" cols="30" placeholder="Share an update..." id="post_text" rows="2" value=""></textarea>
//other view items
<?php
echo form_close();
?>
Please help me
Change:
$this->upload->do_upload()
To this:
$this->upload->do_upload('my_file_input_name')
and it will work!! :)
Change:
<?php echo form_open_multipart('search/post_func');?>
Your form type should be "multipart"
Change your form tag to:
<?php echo form_open_multipart('search/post_func');?>
enctype='multipart/form-data'
you should put previous attribute in form tag
$this->upload->do_upload('file_name')
pass input tag file name to pervious line in your controller
<input type="file" name ="file_name" >
example code is
$config['upload_path'] = FCPATH.'upload/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '100';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$this->load->library('upload');
$this->upload->initialize($config);
if ( ! $this->upload-> do_upload('userfile'))
{
$errors = $this->upload->display_errors();
$this->session->set_flashdata('error', $errors);
redirect('ur_link');
}
else
{
$data = array('upload_data' => $this->upload->data());
$fullpath= $data['upload_data']['full_path'];
$file_name = $data['upload_data']['file_name'];
}
}
Do this:
if(isset($_FILES['profile_pic']['name']) && !empty($_FILES['profile_pic']['name'])){
$config['upload_path']= './assets/img/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 10000;
$config['max_width'] = 10000;
$config['max_height'] = 10000;
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload('profile_pic'))
{
$error = array('error' => $this->upload->display_errors());
print_r($error);
exit;
}
else
{
$data = array('upload_data' => $this->upload->data());
if(!empty($data)){
$img1 = $data['upload_data']['file_name'];
}
}
}
$insert_data['profile_pic'] = $img1;
Another reason I find this problem to be occurred. if you forget to initialize after load your library:
$this->load->library('upload', $config);
$this->upload->initialize($config); //Make this line must be here.
I hope this might help you.
So note I was doing a mysql upload import and I got the "You did not select a file to upload" message. It turns out my import sql file had extra columns which caused the fail and returned this error message.
Related
i am unable to upload multiple
Here is my view form
<form method="POST" action="<?=base_url()?>register/saverecord" enctype="multipart/form-data">
<input type="file" name="file_upload[]" multiple="multiple" value=""><br/><br/>
<input type="submit" name="submit" value="SUBMIT">
</form>
here is my code for multiple upload
public function saveRecord() {
$config['upload_path'] = APPPATH . './uploads/';
$path = $config['upload_path'];
$config['allowed_types'] = '*';
$config['max_size'] = '1024';
$config['max_width'] = '1920';
$config['max_height'] = '1280';
$this->load->library('upload', $config);
$fileName = [];
foreach ($_FILES as $fieldname => $fileObject) //fieldname is the form field name
{
if (!empty($fileObject['name'])) {
$this->upload->initialize($config);
if (!$this->upload->do_upload($fieldname)) {
$errors = $this->upload->display_errors();
} else {
$fileName[] = $this->upload->data();
}
}
}
echo "<pre>";
print_r($fileName);
echo "</pre>";
exit;
}
Here is my error message i am getting after upload
I followed this url Upload multiple files in CodeIgniter
if (!$this->upload->do_upload($fieldname)) {
Here fieldname is an array, you need to have individual files here instead of array.
Try Out This code, It will work. You are passing array to do_upload function. That is not valid. I corrected the code please check after replace this code.
public function saveRecord() {
$config['upload_path'] = APPPATH . './uploads/';
$path = $config['upload_path'];
$config['allowed_types'] = '*';
$config['max_size'] = '1024';
$config['max_width'] = '1920';
$config['max_height'] = '1280';
$this->load->library('upload', $config);
$fileName = [];
foreach ($_FILES as $fieldname => $fileObject) //fieldname is the form field name
{
if (!empty($fileObject['name'])) {
$this->upload->initialize($config);
if (!$this->upload->do_upload($fileObject['name'])) {
$errors = $this->upload->display_errors();
} else {
$fileName[] = $this->upload->data();
}
}
}
echo "<pre>";
print_r($fileName);
echo "</pre>";
exit;
}
When processing a multiple file upload you can access the various files like this - how you tie that in with the native methods available to you via codeigniter I don't know
foreach( $_FILES[ 'fieldname' ] as $i => $void ){
$name=$_FILES[ 'fieldname' ]['name'][$i];
$tmp=$_FILES[ 'fieldname' ]['tmp_name'][$i];
$size=$_FILES[ 'fieldname' ]['size'][$i];
$type=$_FILES[ 'fieldname' ]['type'][$i];
/* other code */
}
How to upload multiple files with multiple inputs in codeigniter. Below is my code. I want to add many files but with different inputs for files.
if(!empty($_FILES['countryfile']['name']))
{
$filesCount = count($_FILES['countryfile']['name']);
for($i = 0; $i < $filesCount; $i++)
{
$imgFile=$_FILES['countryfile']['name'][$i];
$tmp_dir=$_FILES['countryfile']['tmp_name'][$i];
$imgSize=$_FILES['countryfile']['size'][$i];
//$upload_dir='../uploads/dish_images';
$imgExt=strtolower(pathinfo($imgFile,PATHINFO_EXTENSION));
$valid_extensions = array('jpeg', 'jpg', 'png', 'gif','pdf');
$image=rand(1000,10000).".".$imgExt;
$config['upload_path'] = '../admin/upload_doc';
//$config['upload_path'] = 'http://teq-staging.com/maswad-phase2/admin/uploads/dish_images/';
$config['allowed_types'] = 'jpg|jpeg|png|gif|pdf';
$config['file_name'] = $_FILES['countryfile']['name'];
$upload_dir=$config['upload_path'];
//$config['file_name']=$image;
$upload_dir=$config['upload_path'];
$this->upload->initialize($config);
if($this->upload->do_upload('countryfile')){
$upload_data = $this->upload->data();
}
}
upload form.php
<?php echo form_open_multipart('upload'); ?>
<p>
<?php echo form_label('File 1', 'userfile') ?>
<?php echo form_upload('userfile') ?>
</p>
<p>
<?php echo form_label('File 2', 'userfile1') ?>
<?php echo form_upload('userfile1') ?>
</p>
<p><?php echo form_submit('submit', 'Upload them files!') ?></p>
<?php form_close() ?>
the controller
function index()
{
// Has the form been posted?
if (isset($_POST['submit']))
{
// Load the library - no config specified here
$this->load->library('upload');
// Check if there was a file uploaded - there are other ways to
// check this such as checking the 'error' for the file - if error
// is 0, you are good to code
if (!empty($_FILES['userfile']['name']))
{
// Specify configuration for File 1
$config['upload_path'] = 'uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '100';
$config['max_width'] = '1024';
$config['max_height'] = '768';
// Initialize config for File 1
$this->upload->initialize($config);
// Upload file 1
if ($this->upload->do_upload('userfile'))
{
$data = $this->upload->data();
}
else
{
echo $this->upload->display_errors();
}
}
// Do we have a second file?
if (!empty($_FILES['userfile1']['name']))
{
// Config for File 2 - can be completely different to file 1's config
// or if you want to stick with config for file 1, do nothing!
$config['upload_path'] = 'uploads/dir2/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '100';
$config['max_width'] = '1024';
$config['max_height'] = '768';
// Initialize the new config
$this->upload->initialize($config);
// Upload the second file
if ($this->upload->do_upload('userfile1'))
{
$data = $this->upload->data();
}
else
{
echo $this->upload->display_errors();
}
}
}
else
{
$this->load->view("upload_form");
}
}
I have program to upload image and display result from upload. But when I display image only show path image not image. And in database I save by full_path. Please tell me code how to display image from my code.
Here is the controller:
function do_upload()
{
$config['upload_path'] = './images/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '100000';
$config['max_width'] = '10240';
$config['max_height'] = '7680';
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload())
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('upload_form', $error);
}
else
{
$data = array('upload_data' => $this->upload->data());
print_r($this->upload->data());
$datafoto=$this->upload->data();
$nm_file = time().$datafoto['full_path'];
$this->load->model('mkegiatan');
$this->mkegiatan->update_foto($nm_file);
copy('images/'.$datafoto['full_path'], 'images/'.$nm_file);
}
}
Here is the view:
<?php echo e($kegiatan->image) ; ?>
So you said you see just image path and not image itself? Try to replace this:
<?php echo e($kegiatan->image) ; ?>
With this:
<img src="/<?php echo e($kegiatan->image) ; ?>" />
I am trying to upload a file and replace its name with the title name, but I am unable to get the file name by echo in controller even not in as a POST in profiler.
I also need to rename it but before that I need to know the post value ie file name.
Here I am posting my code.
My view
<?php echo form_open_multipart('emailtemplate/do_upload');?>
<tr class='odd gradeX'>
<td class='center'>
<input type="file" name="userfile" size="20" />
</td>
<td class='center'>
<input placeholder='Title For Your File' name='title' class='form-control'>
<td class='center'>
<input placeholder='Comment On File' name='comment' class='form-control'>
</td>
</tr>
<input type="submit" value="upload" />
</form>
My Controller
function do_upload()
{
$this->load->helper('form');
$this->load->helper('html');
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '1000';
$config['encrypt_name'] = true;
//$file=$this->input->post('userfile');
$this->load->library('upload', $config);
$this->upload->data();
$file_name = $this->upload->do_upload('userfile');
echo $file_name;
if ( ! $this->upload->do_upload())
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('add/addfile', $error);
}
else
{
$data = array('upload_data' => $this->upload->data());
$this->load->view('add/addfile', $data);
}
$this->output->enable_profiler(true);
}
To rename uploaded file with POST value:
//get extension
$ext = end(explode(".", $_FILES["userfile"]['name']));
$config['file_name'] = $this->input->post("title").'.'.$ext;
Remove $config['encrypt_name'] = true;. Otherwise your file name will be encrypted .
Make sure that your title doesn't not exist in your uploaded folder. Or append some random number / characters with title.
I have created a function to upload the file :-
it takes argument i.e field_name
public function fileUpload($field) {
//get file extension
$exts = #split("[/\\.]", $_Files[$field][name]);
$n = count($exts) - 1;
$ext = $exts[$n];
//create image name or replace it with your desired name
$imageName = date("Ymdhis") . time() . rand();
$config['file_name'] = $imageName; //set the desired file name
$config['overwrite'] = true;
$config['allowed_types'] = str_replace(',', "|", $ext);
$config['upload_path'] = ABSOLUTEPATH . 'product/original/';
$config['optional'] = true;
$config['max_size'] = '1000';
$config['max_width'] = '00';
$this->load->library('upload', $config);
$this->upload->initialize($config);
if (!$this->upload->do_upload($field, true)) {
$error = array('error' => $this->upload->display_errors());
$r = $error;
} else {
$data = array('upload_data' => $this->upload->data());
$r = "sucess";
}
return $r;
}
the bellow i have written code it can be helpful you
$config['upload_path'] = './uploads/path';
$config['allowed_types'] = 'png|jpg|jpeg|gif';
$config['max_size'] = '1000000';
$config['max_width'] = '1000000';
$config['max_height'] = '1000000000';
$this->load->library('upload', $config);
$files = $_FILES;
if(isset($files['file']['name']))
{
$filename = $files['file']['name'];
$ext = end(explode(".", $filename));
$_FILES['file']['name']= date('d').date('m').date('y').'_'.date(time()).'.'.$ext; //here i am changing file name with current date as your wish to change your logic
// print_r($_FILES);exit; check the changed name
if ($this->upload->do_upload('file'))
{
$upload = array('upload_data' => $this->upload->data());
$imagename = $upload['upload_data']['file_name']; //uploaded your image name
}
else
{
$error = array('error' => $this->upload->display_errors());
print_r($error); //error
}
}
NOTE:Hey user3345331 , See the line
$this->upload->do_upload()
Replace With this
$this->upload->do_upload('userfile');
This userfile is the ID of Input box with filetype in your HTML code.
Title says is all.
Form in view:
<tr>
<form action="<?=base_url()?>index.php/admin/product_add" method="post" enctype="multipart/form-data">
<td><input type="text" name="pid"></td>
<td><input type="text" name="name"></td>
<td><input type="file" name="image" id = "image"></td>
<td><input type="text" name="price"></td>
<td><textarea name="description"></textarea></td>
<td><input type="text" name="type"></td>
<td>
<input type="submit" value="add">
</td>
</form>
</tr>
Function used in controller:
public function product_add()
{
$this->load->helper(array('form','url'));
$this->load->library('form_validation');
/*upload stuff*/
$config = array();
$config['upload_path'] = base_url()."imgs/products/";
$config['allowed_types'] = 'jpg|png|jpeg';
$config['max_size'] = '9999';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$this->load->library('upload', $config);
$_POST['price']=str_replace(",", ".", $_POST['price']);
$this->form_validation->set_rules('pid','Product ID','required');
$this->form_validation->set_rules('name','Denumire Produs','required');
$this->form_validation->set_rules('image','Imagine','required');
$this->form_validation->set_rules('price','Pret','required');
$this->form_validation->set_rules('description','Descriere','required');
$this->form_validation->set_rules('type','Tip','required');
if ( ! $this->upload->do_upload('image'))
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('admin/produse', $error);
echo "epic fail";
}
else
{
if ($this->form_validation->run() == FALSE)
{
echo "FAIL";
}
else
{
echo "asdsad";
$this->db_actions->addProduct($_POST["pid"],$_POST["name"],$_POST["image"],$_POST["price"],$_POST["description"],$_POST["type"]);
redirect(base_url()."index.php/admin/produse");
}
}
}
I've been busting my ass with this all day so I'm seriously frustrated atm.
I'm getting the error because
if ( ! $this->upload->do_upload('image'))
is indeed false. Every.Single.Damn.Time.
What am I doing wrong here ?
Is the error message not giving you anything helpful? It looks like what Kai Qing mentioned, you need to use the server (file system) path as per EllisLab's File Upload Docs and not the URL path.
here some of my codes of uploading files
function upload(){
$config['upload_path'] = './files/contracts-csv/';
$config['allowed_types'] = 'csv';
$config['max_size'] = '4096';
$config['overwrite'] = TRUE;
$this->load->library('upload', $config);
$this->upload->display_errors('', '');
if (!$this->upload->do_upload("image")) {
echo $this->upload->display_errors(); die();
$this->data['error'] = array('error' => $this->upload->display_errors());
//error message
} else {
//do something
}
redirect("contracts/upload_exit");
}
$config['upload_path'] = "var/www/your_app_images_path";
$config['allowed_types'] = "gif|jpg|png|jpeg";
$config['overwrite'] = false;
$config['remove_spaces'] = true;
$this->load->library('upload', $config);
Check your upload_path it should be like above.
if (!$this->upload->do_upload("file_fieldName")) {
pr($this->upload->display_errors());exit;
}