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
}
}
}
Related
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/";
so i try to make some upload form which will upload some image. but when the image has chosen then the button submit has clicked the result say "You did not select a file to upload."
im using php, with codeigniter framework
The view
<form class="text-left" enctype="multipart/form-data" method="post" action="<?= base_url('aduan/add'); ?>">
<div class="form-group">
<label>Upload Foto Bukti Aduan</label>
<div class="form-group">
<label>Bukti Aduan 1</label>
<input class="form-control-file" type="file" id="bukti1" name="bukti_aduan1" />
<?= form_error('bukti_aduan1', '<p class="text-danger font-weight-bold pl-2">', '</p>'); ?>
</div>
<div class="form-group">
<label>Bukti Aduan Pelapor di Lokasi</label>
<input class="form-control-file" type="file" id="bukti2" name="bukti_aduan2" />
<?= form_error('bukti_aduan2', '<p class="text-danger font-weight-bold pl-2">', '</p>'); ?>
</div>
</div>
which the view will run the controller "aduan" for input function
Controller file:
$aduan = $this->aduan_model;
$validation = $this->form_validation;
$validation->set_rules($aduan->rules());
if ($validation->run()) {
$aduan->tambah();
$this->session->set_flashdata('success', 'Berhasil disimpan');
$data['title'] = 'Buat Aduan Kasus Lingkungan';
$this->load->view('home/home_header', $data);
$this->load->view('home/aduansukses');
$this->load->view('home/home_footer');
} else {
$data['aduan'] = $this->aduan_model->getAll();
$data['title'] = 'Buat Aduan Kasus Lingkungan';
$this->load->view('home/home_header', $data);
$this->load->view('home/buataduan');
$this->load->view('home/home_footer');
}
then the controller will load a model which is "aduan_model"
Model file :
public function tambah()
{
$post = $this->input->post();
$this->nik_pelapor = $post["nik_pelapor"];
$this->nama_pelapor = $post["nama_pelapor"];
$this->email_pelapor = $post["email_pelapor"];
$this->telp_pelapor = $post["telp_pelapor"];
$this->alamat_pelapor = $post["alamat_pelapor"];
$this->aduan = $post["aduan"];
$this->bukti_aduan1 = $this->_uploadImage();
$this->bukti_aduan2 = $this->_uploadImage2();
$this->status_aduan = 'Dalam proses';
$this->tgl_aduan = time();
$this->db->insert($this->_table, $this);
}
private function _uploadImage()
{
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png|jpeg';
// $config['max_width'] = 1024;
// $config['max_height'] = 768;
$this->load->library('upload', $config);
if ($this->upload->do_upload('bukti_aduan1')) {
return $this->upload->data();
}
return "default.jpg";
}
private function _uploadImage2()
{
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png|jpeg';
// $config['max_width'] = 1024;
// $config['max_height'] = 768;
$this->load->library('upload', $config);
if ($this->upload->do_upload('bukti_aduan2')) {
return $this->upload->data();
}
return "default.jpg";
}
i expect the output of image can be stored in file upload and inserted in database but it doesnt select any file
please help :)
try my code
replace if condition in controller to upload
if(
(!$this->upload->do_upload('bukti_aduan2')) &&
(! $this->upload->do_upload('bukti_aduan1'))
)
{
echo "failed";
}
else
{
echo "success";
}
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'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;
}
}