I have a function to upload multiple files, but sometimes file(s) is not uploaded but path is successfully added to database. i am not sure what is the ptoblem. i use function 'times()' to make it unique is this the root of the problem ?
i already tested it and it work successfully, but when its implemented sometimes the file doesnt uploaded successfully
if ($_FILES["lampiran"]["name"] != NULL) {
$config = array();
$ext = array();
$records = array();
$file_name = "Plan_".time();
$files = $_FILES['lampiran'];
$config['upload_path'] = "./uploads/Planning/attachment";
$config['allowed_types'] = '*';
foreach ($_FILES['lampiran']['name'] as $key => $value) {
$ext[]= pathinfo($_FILES['lampiran']['name'][$key], PATHINFO_EXTENSION);
}
$cpt = count ($_FILES['lampiran']['tmp_name']);
for($i=0; $i<$cpt; $i++){
$_FILES['lampiran']['name']= $file_name.$i.'.'.$ext[$i];
$_FILES['lampiran']['type']= $files['type'][$i];
$_FILES['lampiran']['tmp_name']= $files['tmp_name'][$i];
$_FILES['lampiran']['error']= $files['error'][$i];
$_FILES['lampiran']['size']= $files['size'][$i];
$records[] = $config['upload_path'].'/'.$_FILES['lampiran']['name'];
$this->upload->initialize($config);
if ( ! $this->upload->do_upload('lampiran'))
{
$error = array('error' => $this->upload->display_errors());
print_r($error);
// $this->load->view('upload_form', $error);
}
}
$pile = implode(";", $records);
} else {
$pile = NULL;
}
Use $records in else condition also make error array than, Like
if ( ! $this->upload->do_upload('lampiran'))
{
$error[] = $this->upload->display_errors();
}else{
$upload_data = $this->upload->data();
$records[] = $config['upload_path'].'/'.$upload_data['file_name'];
}
Here you will get all errors in $error and uploaded files in records.
Related
I have codeigniter application, Now i need to Upload excel using the codigniter.
How to do that. I follow some Online tutorials, but there are not complete one. Specially there are no what is the library they use.
if(isset($_POST['Submit']))
{
$mimes = array('application/vnd.ms-excel','text/xls','text/xlsx','application/vnd.oasis.opendocument.spreadsheet','application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
if(in_array($_FILES["file"]["type"],$mimes))
{
$uploadFilePath = 'uploads/'.basename($_FILES['file']['name']);
$filename = pathinfo($uploadFilePath);
$f = array("name"=>$filename['filename']);
foreach ($f as $k)
{
$policy = $k;
}
move_uploaded_file($_FILES['file']['tmp_name'], $uploadFilePath);
$Reader = new SpreadsheetReader($uploadFilePath);
$totalSheet = count($Reader->sheets());
//echo "You have total ".$totalSheet." sheets".
/* For Loop for all sheets */
for($i=0;$i<$totalSheet;$i++)
{
$Reader->ChangeSheet($i);
foreach ($Reader as $Row)
{
$treeno ++;
$policyno = isset($Row[0]) ? $Row[0] : '';
$act_treeno = isset($Row[1]) ? $Row[1] : '';
$dbh = isset($Row[2]) ? $Row[2] : '';
$height = isset($Row[3]) ? $Row[3] : '';
$pno = substr($policyno, -6);
$treeno = $pno.'_'.$act_treeno.'_T';
$query = "insert into tbl_trees(policyno,actual_treeno,dbh,height,treeno) values('".$policyno."','".$act_treeno."','".$dbh."','".$height."','".$treeno."')";
echo '</br>';
$mysqli->query($query);
this is work, but i have not idea to use this method in codeigniter.
First think you need upload that file then try open and prepare data for insert-
$fileName = $_FILES['file']['name'];
$uploadData = array();
if (!empty($fileName)) {
$config['upload_path'] = './your location/';
$config['allowed_types'] = 'txt|csv|xls|xlsx';
$config['max_size'] = 2048;
$config['remove_spaces'] = TRUE;
$config['encrypt_name'] = TRUE;
$this->load->library('upload', $config);
$this->upload->initialize($config);
if (!$this->upload->do_upload('file')) {
json_error("File Upload Error", $this->upload->display_errors(), 400);
} else {
$uploadData = array('upload_data' => $this->upload->data());
}
}//if
prepare data for insert-
if (!empty($uploadData)) {
$fileName = $uploadData['upload_data']['file_name'];
$data = array(); //empty array;
$fileInfo = fopen(base_url() . 'your file location/' . $fileName, "r");
$i = 0;
while (!feof($fileInfo)) {
$colInfo = fgets($fileInfo); //fgets means read file in line by line.
//prefer data to save
$data[$i]['your_column'] = $colInfo;
$i++;
}//while
fclose($fileInfo);
}
insert batch data-
$this->db->insert_batch('dbtable', $data);
I've a file uploader. I do the overwrite through the data. It makes me upload these data:
- Pict1.jpg
- Pict12.jpg
- Pict13.jpg
But when I tried to return it, its only showing pict1.jpg. Here the code
Controller:
function send(){
$mariupload = $this->upload_gambar($_FILES['gambar']);
if ($mariupload === FALSE) {
$upload = false;
}
if($upload = false){
$error = array('error' => $this->upload->display_errors());
$this->load->view('upload_form', $error);
} else {
$dataupload = $mariupload;
echo $dataupload;
}
private function upload_gambar($files){
//config buat upload gambar
$config['upload_path'] = 'path to upload';
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$config['max_size'] = '1024';
$config['overwrite'] = '0';
$this->load->library('upload', $config);
$gambar = array();
if (is_array($files['name']) || is_object($files['name'])){
foreach ($files['name'] as $key => $gambar) {
$_FILES['gambar[]']['name']= $files['name'][$key];
$_FILES['gambar[]']['type']= $files['type'][$key];
$_FILES['gambar[]']['tmp_name']= $files['tmp_name'][$key];
$_FILES['gambar[]']['error']= $files['error'][$key];
$_FILES['gambar[]']['size']= $files['size'][$key];
$fileName = $gambar;
$images[] = $fileName;
$config['file_name'] = $fileName;
$this->upload->initialize($config);
if ($this->upload->do_upload('gambar[]')) {
$dataupload = $this->upload->data('file_name');
return $dataupload.'<br>';
} else {
$mariupload = false;
}
}
}
}
When i try to echo $dataupload in upload_gambar function, it works as an array. But its returning as a single data to send() function. Any idea?
Because you are sending just single file
$mariupload = $this->upload_gambar($_FILES['gambar']);
change this to
$mariupload = $this->upload_gambar($_FILES);
then in your upload_gambar handle it as foreach
You have a return inside the loop. A return will interrupt the loop as the function will not continue to be executed. Try to feed up an array instead.
Replace
return $dataupload.'<br>';
With
$return[] = $dataupload;
And at the end of the function append:
return $return;
I finally found the solution. Thanks to Jovi Wang on Facebook (https://www.facebook.com/jovi.wang.96?fref=ufi) . This is the clean code:
function send(){
$try = $this->upload_multiple($_FILES['gambar']);
if(!$try = false){
$anu= $this->upload_multiple($_FILES['gambar']);
foreach($anu as $key => $anu2){
echo $anu2.'<br>';
}
}
public function upload_multiple($files)
{
// echo '<pre>';
// print_r($files);
// echo '</pre>';
$config['upload_path'] = 'path to upload';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 1024;
$config['overwrite'] = 0;
$config['encrypt_name'] = TRUE;
$this->load->library('upload');
$this->upload->initialize($config);
$data = array();
foreach($files['name'] as $key => $value)
{
if ( ! empty($files['name'][$key]))
{
$_FILES['gambar_single']['name'] = $files['name'][$key];
$_FILES['gambar_single']['name'] = $files['name'][$key];
$_FILES['gambar_single']['type'] = $files['type'][$key];
$_FILES['gambar_single']['tmp_name'] = $files['tmp_name'][$key];
$_FILES['gambar_single']['error'] = $files['error'][$key];
$_FILES['gambar_single']['size'] = $files['size'][$key];
if ($this->upload->do_upload('gambar_single'))
{
$data[] = $this->upload->data('file_name');
}
else
{
$data[] = $this->upload->display_errors();
}
}
else
{
$data[] = 'empty upload';
}
}
return $data;
}
I have a php script which upload a photo from an Ajax call, and i want to upload the photo two times, one with standard size and another one compressed. Does anyone know how to do it with this following code? I tried to compress the image with scripts that I found on stackoverflow, but I can't do it correctly. Everytime appears an black photo.
I got this code:
$data = array();
if (isset($_GET['files'])) {
$error = false;
$files = array();
$uniqid = uniqid();
$uploaddir = '../../images/'.$uniqid;
foreach($_FILES as $file) {
if (move_uploaded_file($file['tmp_name'], $uploaddir.basename($file['name']))) {
$files[] = $uploaddir.$file['name'];
} else {
$error = true;
}
}
$data = ($error) ? array('error' = > 'There was an error uploading your files') : array('files' = > $files);
} else {
$arr - > image = $file['name'];
$_SESSION['image'] = "img-".$file['name'];
$arr - > ok = "ok";
$data = array('success' = > 'Form was submitted', 'formData' = > $file['name']);
}
Thank you all!
Try using basename() also in your $files[] array insertion:
Change this:
foreach($_FILES as $file) {
if (move_uploaded_file($file['tmp_name'], $uploaddir.basename($file['name']))) {
$files[] = $uploaddir.$file['name'];
} else {
$error = true;
}
}
To this:
foreach($_FILES as $file) {
if (move_uploaded_file($file['tmp_name'], $uploaddir.basename($file['name']))) {
$files[] = $uploaddir.basename($file['name']);
} else {
$error = true;
}
}
I have 2 functions in a controller.
That connects to the form and does validation.
A callback function for image upload.
This only inserts path to one image in db. If I echo print_r from call back function it show all images but from validation function just one.
// Some Validation
} else {
$this->fileupload_check();
$data['uploadedimage'] = $this->upload->data();
$image_name = $data['uploadedimage']['file_name'];
//echo '<pre>';
//print_r($this->upload->data());
$data['uploadedimage'] = $image_name;
$this->load->model('admin/model_users');
if($query = $this->model_users->insert_property_details($data)) {
redirect('dashboard/property-successfully-posted');
}
And callback image upload:
public function fileupload_check() {
$number_of_files = sizeof($_FILES['uploadedimages']['tmp_name']);
$files = $_FILES['uploadedimages'];
for($i=0;$i<$number_of_files;$i++) {
if($_FILES['uploadedimages']['error'][$i] != 0) {
$this->form_validation->set_message('fileupload_check', 'At least 1 image needs to be uploaded in jpeg, png or gif format only.');
return FALSE;
}
}
$this->load->library('upload');
$config['upload_path'] = FCPATH . 'uploads/';
$config['allowed_types'] = 'gif|jpg|png';
for ($i = 0; $i < $number_of_files; $i++) {
$_FILES['uploadedimage']['name'] = $files['name'][$i];
$_FILES['uploadedimage']['type'] = $files['type'][$i];
$_FILES['uploadedimage']['tmp_name'] = $files['tmp_name'][$i];
$_FILES['uploadedimage']['error'] = $files['error'][$i];
$_FILES['uploadedimage']['size'] = $files['size'][$i];
$this->upload->initialize($config);
if ($this->upload->do_upload('uploadedimage')) {
$data['uploadedimage'] = $this->upload->data();
$image_name = $data['uploadedimage']['file_name'];
$data['uploadedimage'] = $image_name;
} else {
$this->form_validation->set_message('fileupload_check', $this->upload->display_errors());
return FALSE;
}
}
return TRUE;
}
I tried to pass the value from 'Callback image upload' function to validation function but it only passes one image not all which are selected.
Try this coding ...
public function fileupload_check() {
$Error_bool = TRUE;
$Error = '';
$number_of_files = sizeof($_FILES['uploadedimages']['tmp_name']);
$files = $_FILES['uploadedimages'];
for($i=0;$i<$number_of_files;$i++) {
if($_FILES['uploadedimages']['error'][$i] != 0) {
$Error_bool = FALSE;
$Error = 'At least 1 image needs to be uploaded in jpeg, png or gif format only.';
}
}
$this->load->library('upload');
$config['upload_path'] = FCPATH . 'uploads/';
$config['allowed_types'] = 'gif|jpg|png';
for ($i = 0; $i < $number_of_files; $i++) {
$_FILES['uploadedimage']['name'] = $files['name'][$i];
$_FILES['uploadedimage']['type'] = $files['type'][$i];
$_FILES['uploadedimage']['tmp_name'] = $files['tmp_name'][$i];
$_FILES['uploadedimage']['error'] = $files['error'][$i];
$_FILES['uploadedimage']['size'] = $files['size'][$i];
$this->upload->initialize($config);
if ($this->upload->do_upload('uploadedimage')) {
$data['uploadedimage'] = $this->upload->data();
$image_name = $data['uploadedimage']['file_name'];
$data['uploadedimage'] = $image_name;
} else {
$Error_bool = FALSE;
$Error = $this->upload->display_errors();
}
}
if($Error_bool == FALSE) {
$this->form_validation->set_message('fileupload_check', $Error);
return FALSE;
} else {
return TRUE;
}
}
The easiest way is by creating a session and send the $data in it and access that session in another function .
You can destroy the session after that if you want destroy the session
I had to make few changes to it to make it work,
I included the whole callback function code directly in FORM function, And removed the callback function completely.
This also helped in one other thing to. Earlier in case of form validation error images were being uploaded to upload folder but now they don't.
I had to make little change in do_upload (by adding $i & implode) which helped me insert multiple images path in db row.
The Whole Script Can Be Found At SOLVED
I need some assistance with how to save path for multiple images saved in 'upload' folder in a table. I am totally lost here.
I have this app which uploads multiple pictures perfect, but they are not linked with db anywhere.
This is controller which is actually a callback_ function:
private $_uploaded;
public function fileupload_check() {
$number_of_files = sizeof($_FILES['uploadedimages']['tmp_name']);
$files = $_FILES['uploadedimages'];
for($i=0;$i<$number_of_files;$i++) {
if($_FILES['uploadedimages']['error'][$i] != 0) {
$this->form_validation->set_message('fileupload_check', 'At least 1 image needed.');
return FALSE;
}
}
$this->load->library('upload');
$config['upload_path'] = FCPATH . 'uploads/';
$config['allowed_types'] = 'gif|jpg|png';
for ($i = 0; $i < $number_of_files; $i++) {
$_FILES['uploadedimage']['name'] = $files['name'][$i];
$_FILES['uploadedimage']['type'] = $files['type'][$i];
$_FILES['uploadedimage']['tmp_name'] = $files['tmp_name'][$i];
$_FILES['uploadedimage']['error'] = $files['error'][$i];
$_FILES['uploadedimage']['size'] = $files['size'][$i];
$this->upload->initialize($config);
if ($this->upload->do_upload('uploadedimage')) {
$this->_uploaded[$i] = $this->upload->data();
} else {
$this->form_validation->set_message('fileupload_check', $this->upload->display_errors());
return FALSE;
}
}
return TRUE;
}
How do I save the path for each image in db if I have a field in tabel 'images_path'?
Thanks in advance.
In if condition Try this code.
$data['upload_data'] = $this->upload->data();
$image_name = $data['upload_data']['file_name'];
and if you print $data like this:
echo "<pre>"; print_r($data);
you will get whole array which includes path , name , image tyepe etc.