I'm have a problem uploading a picture using codeigniter. I followed the online upload tutorial, but can't figure out the problem. I keep getting "The upload path does not appear to be valid". So, here's my form structure:
<?php if (isset($error)) {?>
<div class="col-md-12">
<div class="alert alert-danger" role="alert">
<?= $error ?>
</div>
</div>
<?php } ?>
<?php echo form_open_multipart('pages/do_upload');?>
<input type="file" name="userfile" size="20" />
<br /><br />
<input type="submit" value="upload" />
</form>
I also checked for my directory to be a valid path and to be writable with this code:
<?php if(is_writable('./uploads') && is_dir('./uploads')){
echo 'valid';
}
else { echo (base_url('uploads'). ' ' ."is not writable");} ?>
And this returns valid.
The following is my controller structure:
public function do_upload()
{
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|jpeg|png';
$config['max_size'] = 10000000000;
$config['max_width'] = 1920;
$config['max_height'] = 1080;
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload('userfile'))
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('user/profile/profile', $error);
}
else
{
$data = array('upload_data' => $this->upload->data());
$this->load->view('upload_success', $data);
}
}
In construct i loaded the upload library.
With all this being said, I keep getting the error I mentioned before.
Try Using
$config["upload_path"] = $_SERVER['DOCUMENT_ROOT']."/your/desired/location/";
In CodeIgniter, I have 4 file upload controls as under:
<?php echo form_open_multipart('uploadimg');?>
<input type="file" name="image1" />
<input type="file" name="image2" />
<input type="file" name="image3" />
<input type="file" name="image4" />
<input type="submit" value="upload" />
<?php echo form_close();?>
Following is my file upload code:
$name1="img1.jpg";
$config1=array(
'upload_path'=>$_SERVER['DOCUMENT_ROOT']."/c/uploads/",
'allowed_types'=>"gif|jpg|png|jpeg|pdf|JPG|JPEG",
'overwrite'=>TRUE,
'max_size'=>"500",
'file_name'=>$name1,
);
$this->load->library('upload',$config1);
if($this->upload->do_upload('image1'))
{
echo "Image 1 has been uploaded successfully";
}
else
{
echo $this->upload->display_errors();
}
$name2="img2.jpg";
$config2=array(
'upload_path'=>$_SERVER['DOCUMENT_ROOT']."/c/uploads/",
'allowed_types'=>"gif|jpg|png|jpeg|pdf|JPG|JPEG",
'overwrite'=>TRUE,
'max_size'=>"500",
'file_name'=>$name2,
);
$this->load->library('upload',$config2);
if($this->upload->do_upload('image2'))
{
echo "Image 2 has been uploaded successfully";
}
else
{
echo $this->upload->display_errors();
}
$name3="img3.jpg";
$config3=array(
'upload_path'=>$_SERVER['DOCUMENT_ROOT']."/c/uploads/",
'allowed_types'=>"gif|jpg|png|jpeg|pdf|JPG|JPEG",
'overwrite'=>TRUE,
'max_size'=>"500",
'file_name'=>$name3,
);
$this->load->library('upload',$config3);
if($this->upload->do_upload('image3'))
{
echo "Image 3 has been uploaded successfully";
}
else
{
echo $this->upload->display_errors();
}
$name4="img4.jpg";
$config4=array(
'upload_path'=>$_SERVER['DOCUMENT_ROOT']."/c/uploads/",
'allowed_types'=>"gif|jpg|png|jpeg|pdf|JPG|JPEG",
'overwrite'=>TRUE,
'max_size'=>"500",
'file_name'=>$name4,
);
$this->load->library('upload',$config4);
if($this->upload->do_upload('image4'))
{
echo "Image 4 has been uploaded successfully";
}
else
{
echo $this->upload->display_errors();
}
I am getting the following output:
Image 1 has been uploaded successfullyImage 2 has been uploaded
successfullyImage 3 has been uploaded successfullyImage 4 has been
uploaded successfully
But, in the uploads folder, I am getting only 1 file. Strange thing is that the file that is being shown is of last file upload control, but the name of file is img1., i.e. name of first uploaded file.
Why is this happening? What is the solution?
Set each config using the initialize() method and not when loading the upload library.
Changed:
$this->load->library('upload', $config*);
To:
$this->upload->initialize($config*);
Updated code:
$this->load->library('upload');
$name1 = "img1.jpg";
$config1 = array(
'upload_path' => FCPATH . "uploads/",
'allowed_types' => "gif|jpg|png|jpeg|pdf|JPG|JPEG",
'overwrite' => TRUE,
'max_size' => "500",
'file_name' => $name1
);
$this->upload->initialize($config1);
if ($this->upload->do_upload('image1')) {
echo "Image 1 has been uploaded successfully";
} else {
echo $this->upload->display_errors();
}
$name2 = "img2.jpg";
$config2 = array(
'upload_path' => FCPATH . "uploads/",
'allowed_types' => "gif|jpg|png|jpeg|pdf|JPG|JPEG",
'overwrite' => TRUE,
'max_size' => "500",
'file_name' => $name2
);
$this->upload->initialize($config2);
if ($this->upload->do_upload('image2')) {
echo "Image 2 has been uploaded successfully";
} else {
echo $this->upload->display_errors();
}
$name3 = "img3.jpg";
$config3 = array(
'upload_path' => FCPATH . "uploads/",
'allowed_types' => "gif|jpg|png|jpeg|pdf|JPG|JPEG",
'overwrite' => TRUE,
'max_size' => "500",
'file_name' => $name3
);
$this->upload->initialize($config3);
if ($this->upload->do_upload('image3')) {
echo "Image 3 has been uploaded successfully";
} else {
echo $this->upload->display_errors();
}
$name4 = "img4.jpg";
$config4 = array(
'upload_path' => FCPATH . "uploads/",
'allowed_types' => "gif|jpg|png|jpeg|pdf|JPG|JPEG",
'overwrite' => TRUE,
'max_size' => "500",
'file_name' => $name4
);
$this->upload->initialize($config4);
if ($this->upload->do_upload('image4')) {
echo "Image 4 has been uploaded successfully";
} else {
echo $this->upload->display_errors();
}
You may try this. This is working properly to upload two file
(audio, image) or more file to the database.
Table Name : products
| id | product_name | price | description| product_image | audio_file |
HTML
<form name="addProduct" id="addProduct" method="POST" enctype="multipart/form-data">
<input type="text" name="product_name" placeholder="Product Name">
<input type="text" name="price" placeholder="Product Price">
<textarea id="description" name="description" class="materialize-textarea" placeholder="Description"></textarea>
<input type="file" name="product_image" id="fileImg">
<input type="file" name="audio_file" id="audio_file">
<input type="button" name="addProduct" value="Add Product" class="btn btn-flat btn-add-product">
</form>
Controller
public function addProducts(){
/*Initialization of model*/
$this->load->model("product_model");
/*store data into array*/
$result=array(
"product_name"=>$_POST["product_name"],
"price"=>$_POST["price"],
"description"=>$_POST["description"],
);
$proID = $this->product_model->addProduct($result); //sending data to model for insertion
//Define the file names with blog id with same extension which has been uploaded
$product_image = $proID."_product.".pathinfo($_FILES['product_image']['name'], PATHINFO_EXTENSION);
$product_audio = $proID."_audio.".pathinfo($_FILES['audio_file']['name'], PATHINFO_EXTENSION);
$updateData = array(
"product_image" => $product_image,
"audio_file"=>$product_audio
);
// update the name of the images in the database
$this->product_model->updateProduct($updateData,$proID);
//set configuration for the upload library
/* |===> Image Config <==|*/
$config['upload_path'] = 'html/images/products/';
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$config['overwrite'] = TRUE;
$config['encrypt_name'] = FALSE;
$config['remove_spaces'] = TRUE;
/* |==> Audio Config <==|*/
$config_audio['upload_path'] = 'html/images/products/audios/';
$config_audio['allowed_types'] = 'mp3|mp4';
$config_audio['overwrite'] = TRUE;
$config_audio['encrypt_name'] = FALSE;
$config_audio['remove_spaces'] = TRUE;
//set name in the config file for the feature image
$config['file_name'] = $proID."_product";
$this->load->library('upload', $config); // config first file
$this->upload->do_upload('product_image');
$this->upload->display_errors();
$config_audio['file_name'] = $proID."_audio";
$this->upload->initialize($config_audio); // config seconf file
$this->upload->do_upload('audio_file');
$this->upload->display_errors();
}
Model
/*Add Product*/
public function addProduct($pro_data){
$query=$this->db->insert("products",$pro_data);
$id=$this->db->insert_id();
return $id;
}
/*Update for Multiple File Upload*/
public function updateProduct($result,$up_id){
$this->db->where('id',$up_id);
$this->db->update("products",$result);
}
This is working nicely to my website and i hope it will help you to boost your code.
HTML
<form class="col s12" id='propertyform' enctype="multipart/form-data">
<input type="file" name="image1" />
<input type="file" name="image2" />
<input type="file" name="image3" />
<input type="file" name="image4" />
<a class="waves-effect waves-light btn addproperty">add</a>
</form>
js
$(".addproperty").on("click",function()
{
var baseurl = $("#base_url").val();
var property = new FormData($("#propertyform")[0]);
$.ajax({
url : baseurl+"dropdowns/add_propertyy",
data : property,
type:"POST",
contentType:false,
processData:false,
success:function(res)
{
window.location.reload();
}
});
});
controller
public function add_propertyy()
{
$id = 1;
$images = $id."_image1.".pathinfo($_FILES['image1']['name'], PATHINFO_EXTENSION);
$images1 = $id."_image2.".pathinfo($_FILES['image2']['name'], PATHINFO_EXTENSION);
$images2 = $id."_image3.".pathinfo($_FILES['images3']['name'], PATHINFO_EXTENSION);
$images3 = $id."_image4.".pathinfo($_FILES['image4']['name'], PATHINFO_EXTENSION);
$addimg = array(
"images1" => $images,
"images2" => $images1,
"images3" => $images2,
"images4" => $images3,
);
$this->load->model('property_model');
$this->property_model->add_pro_img($addimg,$id);
$uploadPath = "./html/images/property";
$config['upload_path'] = $uploadPath;
$config['allowed_types'] = 'gif|jpg|png|pdf';
$config['overwrite'] = TRUE;
$config['encrypt_name'] = FALSE;
$config['remove_spaces'] = TRUE;
$config['file_name'] = $id."_images1";
$this->load->library('upload', $config);
$this->upload->do_upload('images');
$this->upload->display_errors();
$config['file_name'] = $id."_images2";
$this->upload->initialize($config);
$this->upload->do_upload('images2');
$this->upload->display_errors();
$config['file_name'] = $id."_images3";
$this->upload->initialize($config);
$this->upload->do_upload('images3');
$this->upload->display_errors();
$config['file_name'] = $id."_images4";
$this->upload->initialize($config);
$this->upload->do_upload('images4');
$this->upload->display_errors();
}
modal
public function add_pro($data) {
$this->db->insert("property",$data);
$id = $this->db->insert_id();
return $id;
}
I'm trying to upload a file , but file name is a variable,so to do that I have get the name of input type(i can do it by getting all data on post and run a loop and get the keyname)
then I'm getting all post data , but then it returns me a empty array,when I remove multipart/form data from the form it will return the array with the value.then I can get the file type name, but it doesn't upload the file because I remove the multipart from the form.
How can I do this? if read the code u will understand. Please help me to do this.
This is my view page
public function addTest($app_id)
{
$data=$this->input->post();
$id=$this->session->userdata['loggedHospitalData']['hp_id'];
while ($test = current($data)) {
$dataArr=array(
"hospital_id" => $id,
);
$test_id=key($data);
$wherArr = array("test_id"=>$test_id);
//$result = $this->Hospital_model->updateTestData($dataArr,$wherArr);
$report=$this->input->post($test_id);
$config['upload_path'] = './uploads/';
$config['allowed_types'] = '*';
$config['max_size'] = 5000000000000;
$config['max_width'] = 10240;
$config['max_height'] = 10240;
$this->load->library('upload', $config);
if (!$this->upload->do_upload($report))
{
$error = array('error' => $this->upload->display_errors());
}
else
{
$data1 = array('upload_data' => $this->upload->data());
}
/*$dataRepArr=array(
"r_test_id " => $test_id,
"file" => $photo1,
);*/
next($data);
//header('Location: http://localhost/docto/index.php/Hospital/patientView/'.$app_id);
}
}
And this my View
<form action="<?php echo base_url(); ?>index.php/Hospital/addTest/<?php echo $appoinmentData->app_id; ?>" role="form" method='post' onmouseover="return form_validate()" name="vform">
<div class="">
<ul class="to_do">
<?php
foreach ($testData as $test) {
echo '
<li>
<p>
'.$test->test_name.' </p>
</br>
<input type="file" class="btn btn-primary" name="'.$test->test_id.'">
</li>
';
}
?>
</ul>
</div>
<input type="submit" class="btn btn-success btn-lg btn-block" value="Update" name="">
</form>
You need multipart/form-data for the rules to be uploaded. Put it back.
Then loop over $_FILES to get all the files that were uploaded.
multipart/form-data MUST be there for file uploads
You can try something like this:
$this->load->library('upload');
$config['upload_path'] = './assets/uploads/'
$config['allowed_types'] = 'jpg|png|jpeg|pdf';
$config['max_size'] = '0';
$config['overwrite'] = TRUE;
$config['encrypt_name'] = TRUE;
$this->upload->initialize($config);
foreach($_FILES as $k => $v)
{
if ($_FILES[$k]['name'] != '')
{
//This do the trick, do_upload need the name of the input
//And in this case, name is the KEY of an array.
if($this->upload->do_upload($k))
{
//Upload Successful
}
}
}
This is my view code of uploading an image but it is showing error You did not select a file to upload.
<form id="signupForm" action="<?php echo base_url();?>dashboard/saveSpeciality" method="post">
<div class="form-row">
<input type="text" required="" name="speciality_Name" placeholder="Speciality Name" >
<?php echo form_error('speciality_Name','<span class="help-block">','</span>'); ?>
</div>
<input type="file" name="image" size="20"/>
<?php echo form_error('image','<span class="help-block">','</span>'); ?>
<div class="form-row">
<input type="submit" name="addSubmit" value="Add Speciality"/>
</div>
</form>
This is code of my controller dashboard and function is saveSpeciality. when i try to upload image it shows error "You did not select a file to upload.".
public function saveSpeciality()
{
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png|bmp|jpeg';
$config['max_size'] = '2048000';
$config['max_width'] = '2048';
$config['max_height'] = '2048';
$config['overwrite'] = true;
$this->load->library('upload', $config);
$imag="image";
if( ! $this->upload->do_upload($imag)){
$this->form_validation->set_error_delimiters('<p class="error">', '</p>');
echo $this->upload->file_name;
$error = array('error' => $this->upload->display_errors());
print_r($error);
} else {
$data = array('upload_data' => $this->upload->data());
}
}
You can try add enctype="multipart/form-data" to your <form>
add enctype="multipart/form-data" attribute to your form
<form id="signupForm" action="<?php echo base_url();?>dashboard/saveSpeciality" method="post" enctype="multipart/form-data">
</form>
Try This
<form id="signupForm" action="<?php echo base_url();?>dashboard/saveSpeciality" method="post" enctype="multipart/form-data">
$file_name = time() . "." . pathinfo($_FILES['image']['name'], PATHINFO_EXTENSION);
$config['upload_path'] = FCPATH . 'uploads/';
$config['allowed_types'] = 'gif|jpg|png|jpeg|GIF|JPG|PNG|JPEG';
$config['max_size'] = '2048000';
$config['max_width'] = '2048';
$config['max_height'] = '2048';
$config['file_name'] = $file_name;
$config['overwrite'] = true;
$obj->upload->initialize($config);
$imag = "image";
if (!$obj->upload->do_upload($imag)) {
$error = array('error' => $this->upload->display_errors());
print_r($error);
//return $obj->upload->display_errors();
} else {
$data = array('upload_data' => $this->upload->data());
}
I'm using Codeigniter Upload Class but I have a problem with allowed file types. I need to allow only pdf files to upload but any kind of file can be upload. Here is my code;
model (muser.php);
function cv_ekle()
{
$config['upload_path'] = 'uploads/cv';
$config['allowed_types'] = 'pdf';
$config['max_size'] = '0';
$config['max_width'] = '0';
$config['max_height'] = '0';
$this->load->library('upload', $config);
$this->upload->do_upload();
$data = $this->upload->data();
$this->load->library('upload', $config);
$upload_data = $this->upload->data(); //Returns array of containing all of the data related to the file you uploaded.
$file_name = $upload_data['file_name'];
$data = array
(
'userid' => $this->session->userdata('id'),
'kullanici' => $this->session->userdata('isim'),
'kategori' => $this->input->post('kategori'),
'tarih' => time(),
'dosya' => $file_name
);
if($this->db->insert('cv', $data))
{
return true;
}
else
{
return false;
}
}
controller (cv.php);
function cv_ekle()
{
if($this->muser->cv_ekle())
{
$this->session->set_flashdata('ok', 'CV başarıyla gönderildi!');
redirect('cv');
}
else
{
$this->session->set_flashdata('hata', 'Sadece PDF, Excel ya da Word formatında yükleme yapabilirsiniz!');
redirect('cv');
}
}
view (cv.php);
<form method="post" action="<?php echo site_url('cv/cv_ekle'); ?>" class="login" enctype="multipart/form-data">
<div class="controls">
<label for="email">Dosya: <span class="text-error">*</span></label>
<input type="file" id="pass" class="input-block-level" name="userfile" >
</div>
<div class="controls">
<label for="email">Kategori: <span class="text-error">*</span></label>
<select class="input-block-level" name="kategori">
<?php foreach($kategoriler as $kat) { ?>
<option value="<?php echo $kat['isim']; ?>"><?php echo $kat['isim']; ?></option>
<?php } ?>
</select>
</div>
<div class="controls">
<button type="submit" class="btn btn-primary">CV Yükle</button>
</div>
</form>
Thanks in advance!
function cv_ekle()
{
$config['upload_path'] = 'uploads/cv';
$config['allowed_types'] = 'pdf';
$config['max_size'] = '0';
$config['max_width'] = '0';
$config['max_height'] = '0';
$this->load->library('upload', $config);
$this->upload->do_upload();
$upload_data = $this->upload->data(); //Returns array of containing all of the data related to the file you uploaded.
$file_name = $upload_data['file_name'];
$data = array
(
'userid' => $this->session->userdata('id'),
'kullanici' => $this->session->userdata('isim'),
'kategori' => $this->input->post('kategori'),
'tarih' => time(),
'dosya' => $file_name
);
if($this->db->insert('cv', $data))
{
return true;
}
else
{
return false;
}
}