anyone can help me.. its work only if at least 2 files uploaded and then the first file didn't move to upload folder.. anyone can help.. here's my code
// view
<?php echo form_open_multipart($this->uri->uri_string()); ?>
<p>
<?php echo form_label('Image') ?>
<input type="file" name="userfile[]" size="20" class="multi" accept="gif|jpg|png"/>
</p>
// controller
function album($id){
if (isset($_POST['submit']))
{
$config['upload_path'] = './assets/gallery/'; // server directory
$config['allowed_types'] = 'gif|jpg|png'; // by extension, will check for whether it is an image
$config['max_size'] = '1000'; // in kb
$config['max_width'] = '1024';
$config['max_height'] = '768';
$this->load->library('upload', $config);
$this->load->library('Multi_upload');
$files = $this->multi_upload->go_upload();
if ( ! $files )
{
$data['error'] = $this->upload->display_errors();
}
else
{
$data = array('upload_data' => $files);
}
}
$id = $this->uri->segment(3);
$data['query'] = $this->administrator_model->getAllPhoto($id);
$data['current'] = 'home';
$data['side'] = 'gallery';
$data['attr'] = 'view_album';
$data['content'] = 'backend/administrator_manage';
$data['sidebar'] = 'backend/home_sidebar';
$this->load->view("backend/index", $data);
}
// multi upload library
i use from here
with little modification
It is very simple to use pure PHP multi file Upload. Look into this script
View
<form method="post" action="<?php echo base_url('users/upload/'); ?>" enctype="multipart/form-data">
<label for="upload">Select : </label>
<input type="file" name="userfile[]" id="userfile" multiple="multiple" />
<input type="submit" name="fsubmit" id="fsubmit" value="Upload" />
</form>
Controller
public function album($id)
{
if (isset($_FILES['userfile']['name'])) {
// total files //
$count = count($_FILES['userfile']['name']);
// all uploads //
$uploads = $_FILES['userfile'];
for ($i = 0; $i < $count; $i++) {
if ($uploads['error'][$i] == 0) {
// FCPATH = root directory project
move_uploaded_file($uploads['tmp_name'][$i], FCPATH . 'assets/gallery/' . $uploads['name'][$i]);
echo $uploads['name'][$i] . "\n";
}
}
}
}
Hope this helps. Thanks!!
Related
view :
<form action="#" enctype="multipart/form-data" id="form" class="form-horizontal">
<div class="form-group">
<label class="control-label col-md-3" id="label-photo">Upload Photo </label>
<div class="col-md-9">
<input name="photo" type="file">
<span class="help-block"></span>
</div>
</div>
Controller :
private function _do_upload()
{
/* $config['upload_path'] = $_SERVER['DOCUMENT_ROOT'] . '/uploads/';
// */
$config['upload_path'] = 'uploads/';
// $config['allowed_types'] = 'gif|jpg|png|jpeg';
$config['allowed_types'] = 'jpg|gif|png|jpeg|JPG|PNG';
$config['max_size'] = 5000; //set max size allowed in Kilobyte
$config['max_width'] = 3000; // set max width image allowed
$config['max_height'] = 3000; // set max height allowed
$config['file_name'] = round(microtime(true) * 1000); //just milisecond timestamp fot unique name
$this->load->library('upload', $config);
if(!$this->upload->do_upload('photo')) //upload and validate
{
$data['inputerror'][] = 'photo';
$data['error_string'][] = 'Upload error: '.$this->upload->display_errors('',''); //show ajax error
$data['status'] = FALSE;
echo json_encode($data);
exit();
}
return $this->upload->data('file_name');
}
When I am trying to upload an image getting this error and I cann't find any solution. Tried by changing upload path and allowed types.
Ok so, You try this Code.You can upload image successfully.
View File : uoload.php
<form action="upload_controller" method="post" enctype="multipart/form-data">
First name: <input type="file" name="photo"><br>
<input type="submit" value="Submit">
</form>
Now This Is my Upload Controller :
public function upload_controller(){
if (count($_FILES) > 0) {
if (count($_FILES['photo']) > 0) {
if ($_FILES['photo']['name'] != '' && $_FILES['photo']['size'] > 0) {
$filename = $_FILES['photo']['name'];
$filesize = $_FILES['photo']['size'];
$config['upload_path'] = '../admin_zency/data/doctors/'; // Your Upload Path Directory
$config['allowed_types'] = '*';
$config['max_size'] = $this->config->item('image_size');
$config['max_width'] = $this->config->item('user_width');
$config['max_height'] = $this->config->item('user_height');
$this->upload->initialize($config);
$this->load->library('upload', $config);
$this->upload->do_upload('photo');
$imagedata = $this->upload->data();
$imagearror = $this->upload->display_errors();
}
}
}
}
Thanks and give ratting if this code is helpfull to you.
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
}
}
}
Problem is When upload a image its shows same error every time "The upload path does not appear to be valid". Tried so much solution but still same problem with it..Please help.
Controller:
public function updateprofile()
{
/***get current user data***/
$this->load->model('Admin_main','userinfo');
$data=$this->userinfo->getuserdata();
$this->load->model('Admin_main','updateprofile');
$result=$this->updateprofile->updatemyprofile();
if($data->identity=='admin')
{
redirect('admin/myprofile');
}
else
{
redirect('campuser/myprofile');
}
}
Model:
if($_FILES['fileToUpload']['name']!='')
{
$config["upload_path"] = './uploads/';
$config["allowed_types"] = 'jpg|jpeg|png|gif';
$config['max_width'] = 0;
$config['max_height'] = 0;
$config['max_size'] = 0;
$config['encrypt_name'] = TRUE;
//Load upload library and initialize configuration
$this->load->library('upload',$config);
$this->upload->initialize($config);
if($this->upload->do_upload('fileToUpload')){
$uploadData = $this->upload->data();
$picture = $uploadData['file_name'];
$mypic = array('profile_pic'=>$picture);
$this->db->set($mypic);
$this->db->where('id', $userid);
$uppic=$this->db->update('mokhayam_users');
}
else
{
echo $this->upload->display_errors();
}
}
View:
<form class="form-horizontal" action="admin/updateprofile" method="post" enctype="multipart/form-data">
<div class="form-group">
<label class="col-sm-2 control-label">Profile Picture</label>
<div class="col-sm-10">
<label for="fileToUpload"><img src="assets/admin/dist/img/gul.jpg" id="blah1" class="profile-user-img img-responsive img-circle"/></label><input type="file" name="fileToUpload" id="fileToUpload" onchange="$('#blah1')[0].src = window.URL.createObjectURL(this.files[0])">
</div>
</div>
<input type="hidden" name="userid" value="2">
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-success">Submit</button>
</div>
</div>
</form>
Thanks in advance..
If your codeigniter version is 3.x please change library file (system/library/upload.php) version codeigniter 3.x to 2.x
and then try
$config["upload_path"] = APPPATH.'/uploads/';
......
Without changing anything in your library, the solution is to initialize the config just after loading the upload library in your controller, follow this code
// Upload Image
$config['upload_path'] = './uploads';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '7048';
$config['max_width'] = '7000';
$config['max_height'] = '7000';
// Create folder (Uploads) if not exists
if (!is_dir($config['upload_path'])) {
mkdir($config['upload_path']);
}
// Load library Upload
$this->load->library('upload', $config);
// Initialize the Upload library with current $config
$this->upload->initialize($config);
$this->upload->do_upload();
if ($this->upload->do_upload()) {
$data = array('upload_data' => $this->upload->data());
$page_image = $_FILES['userfile']['name'];
} else {
$errors = array('error' => $this->upload->display_errors());
$page_image = 'noimage.jpg';
}
// Set message
$this->session->set_flashdata('page_created', 'Your File(s) has been uploaded');
I am trying to implement CodeIgniter's upload library. But everytime I tried to upload, it keeps on saying that I haven't selected a file to upload.
Here is the code:
View:
<form action="<?= base_url().'principal/updateuniform' ?>" method="post">
<h3 style="text-align:center">Polo</h3>
<input type="hidden" name="imageid" value="<?= $img_id ?>">
<input type="hidden" name="imagename" value="<?= $img_name ?>">
<input type="file" name="imagefile" accept="image/*" /><br>
<button type="submit" class="btn btn-gold"><span class="glyphicon glyphicon-upload" aria-hidden="true"></span> Upload</button>
</form>
Upload function:
function updateuniform(){
$img_id = $this->input->post('imageid');
$img_name = $this->input->post('imagename');
$config['upload_path'] = './images/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 5000;
$config['max_width'] = 3000;
$config['max_height'] = 4000;
$config['overwrite'] = TRUE;
$config['file_name'] = $img_name;
$this->load->library('upload', $config);
$this->upload->initialize($config);
$img = $this->input->post('imagefile');;
$id = $this->session->userdata('id');
$user['user'] = $this->UsersModel->select_principal($id);
if (!$this->upload->do_upload($img)){
$data['error'] = $this->upload->display_errors();
$data['uniforms'] = $this->InfoModel->getUniforms();
$this->load->view('include/header_principal',$user);
$this->load->view('principal/manage_uniforms', $data);
$this->load->view('include/footer_principal');
} else {
$img_path = 'images/'.$this->upload->data('orig_name');
$data['success'] = 'Uniforms has been updated.';
$this->InfoModel->updateUniform($img_path);
$this->load->view('include/header_principal',$user);
$this->load->view('principal/manage_uniforms', $data);
$this->load->view('include/footer_principal');
}
}
I also tried to echo the $img and it's displaying the file name.
$this->upload->do_upload($img)
The param should be the filedname,like:
$this->upload->do_upload('imagefile')
Add enctype="multipart/form-data" in your form tag of view this allows you to get $_FILE value and This will solve your issue I guess
I aam new to codeigniter, can any one tell me how do we can upload multiple files to the server at a same time with different names in codeigniter.
Create multiple file upload form. Here I refer to upload method in users controller.
<form method="post" action="<?php echo base_url('users/upload/'); ?>" enctype="multipart/form-data">
<label for="upload">Select : </label>
<input type="file" name="upload[]" id="upload" multiple="multiple" />
<input type="submit" name="fsubmit" id="fsubmit" value="Upload" />
</form>
And Write the following code in your upload method.
public function upload()
{
if (isset($_FILES['upload']['name'])) {
// total files //
$count = count($_FILES['upload']['name']);
// all uploads //
$uploads = $_FILES['upload'];
for ($i = 0; $i < $count; $i++) {
if ($uploads['error'][$i] == 0) {
move_uploaded_file($uploads['tmp_name'][$i], 'storage/' . $uploads['name'][$i]);
echo $uploads['name'][$i] . "\n";
}
}
}
}
The directory storage is example. You can move the files in your choice. NOTE It is not pure CodeIgniter method for multiple file uploads. Hope this helps thanks!!
you can upload files with different name in following way
in view
<?php echo form_open_multipart('upload/do_upload');?>
<input type="file" name="userfile1" size="20" />
<input type="file" name="userfile2" size="20" />
in controller just pass field name to do_upload method
function do_upload()
{
$this->upload_file('userfile1');
$this->upload_file('userfile2');
$this->load->view('upload_form', $error);
}
function upload_file($field_name){
$config['upload_path'] = './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($field_name))
{
return array('error' => $this->upload->display_errors());
}
else
{
$data = array('upload_data' => $this->upload->data());
}
}
for more information please read CI user guide User guide