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;
}
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 */
}
Following is my controller In this I am using two if statements one for multiple images and another is for the featured image.. my images are uploaded in a folder very well but multiple names are not inserted in the database...Only one file name is inserted in the database...
public function uploadApi()
{
if (isset($_FILES['userfile'])) {
$config['upload_path'] = 'uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 200000;
$config['max_width'] = 2024;
$config['max_height'] = 1768;
$this->upload->initialize($config);
$this->load->library('upload', $config);
$this->upload->do_upload('userfile');
$data = array( $this->upload->data());
$this->m->update_post($data[0]['file_name']);
}
if(isset($_FILES['userfile1'])) {
$config['upload_path'] = 'uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 200000;
$config['max_width'] = 2024;
$config['max_height'] = 1768;
$this->upload->initialize($config);
$this->load->library('upload', $config);
$this->upload->do_upload('userfile1');
$data = array( $this->upload->data());
$this->m->update_feature($data[0]['file_name']);
}
}
This is a model ..
#-Update images Post-#
public function update_post($picture) {
$post = array(
'post_images'=>$picture,
);
$this->db
->where('post_status','draft')
->update('post',$post);
return true;
}
public function update_feature($picture) {
$post = array(
'post_featured_image'=>$picture,
);
$this->db
// ->set('post_created', 'NOW()', FALSE)
->where('post_status','draft')
->update('post',$post);
return true;
}
filepond plugin script
FilePond.registerPlugin(
FilePondPluginFileValidateSize,
FilePondPluginImageExifOrientation,
FilePondPluginImageCrop,
FilePondPluginImageResize,
FilePondPluginImagePreview,
FilePondPluginImageTransform
);
// Set default FilePond options
FilePond.setOptions({
// maximum allowed file size
maxFileSize: '50MB',
imagePreviewHeight: 100,
imagePreviewWidth: 200,
instantUpload: true,
// crop the image to a 1:1 ratio
imageCropAspectRatio: '1:1',
// upload to this server end point
server: {
url: '<?php echo base_url() ?>Admin/uploadApi',
}
});
var pond = FilePond.create(document.querySelector('input[name="userfile"]'));
var pond = FilePond.create(document.querySelector('input[name="userfile1"]'));
**This is a view ..**
<form method="post" enctype="multipart/form-data" class="toggle-disabled" action="<?php echo base_url() ?>Admin/update_post1" id='ritesh'>
<div class="col-md-6">
<div class="form-group">
<label>Upload images</label>
<input type="file"
class="filepond"
name="userfile"
multiple
data-max-file-size="5MB"
data-max-files="50" data-validation="required extension" />
</div>
<div class="form-group">
<label>Feature image</label>
<input type="file"
class="filepond"
name="userfile1"
data-max-file-size="5MB"
data-validation="required extension"
/>
</div>
</form>
For multiple image upload you should post images array like; imagename[]. Your current approach is not good.
You must try already posted answers:
Multiple image upload with CodeIgniter
Multiple image upload with Codeigniter saving only one file path to MySQL Database
https://www.codexworld.com/codeigniter-upload-multiple-files-images/
Please try to this in controller
function uploadApi() {
$image = $_FILES;
foreach ($image as $key => $img) {
if (!is_dir('./Uploads/')) {
mkdir('./Uploads/', 0777, TRUE);
}
if (!empty($img['name'])) {
$config['upload_path'] = './Uploads/Products/';
$config['allowed_types'] = '*';
$config['max_size'] = '100';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$config['overwrite'] = TRUE;
$config['file_name'] = date('U') . '_' . $img['name'];
$this->load->library('upload', $config);
$this->upload->initialize($config);
if (!$this->upload->do_upload($key)) {
$error = array('error' => $this->upload->display_errors());
print_r($error);
die;
} else {
if ($this->upload->do_upload($key)) {
$image_data = $this->upload->data();
$update["userfile"] = $config['file_name'];
$res = $this->m->update_post($update);
}
}
}
}
$this->load->view('imgtest');
}
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.
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.
How to do a multiple file upload in codeigniter
<input type="file" name="pic[]">
<input type="file" name="pic[]">
<input type="file" name="pic[]">
How can I upload this?
using the do_upload function
You can upload any number of files
$config['upload_path'] = 'upload/Main_category_product/';
$path=$config['upload_path'];
$config['allowed_types'] = 'gif|jpg|jpeg|png';
$config['max_size'] = '1024';
$config['max_width'] = '1920';
$config['max_height'] = '1280';
$this->load->library('upload');
foreach ($_FILES as $key => $value)
{
if (!empty($key['name']))
{
$this->upload->initialize($config);
if (!$this->upload->do_upload($key))
{
$errors = $this->upload->display_errors();
flashMsg($errors);
}
else
{
// Code After Files Upload Success GOES HERE
}
}
}
You can see There is no need of name property.