Multiple files upload with array name in CodeIgniter - php

i've problem with my code here.
The case is, i have multiple input file form, i need to get name of current array (which contain database id) and upload the file from it.
Here's in my views
foreach($result2 as $result3)
{
print "
<tr>
<td><input type=file name=baddismantling[".$result3['iddeployment']."] size=20></td>
</tr>
";
}
so, there'll be various of upload form and name too, but it always unique.
and after user click submit button, that form will call function in my controller, and here's
the function that handled it
private function _ready_to_start($sessiondata)
{
if($post['doSubmitDismantlingAction'] == 'uploadbad')
{
while ($fruit_name = current($_FILES['baddismantling']['name']))
{
if($fruit_name != false)
{
//GET ID FROM DEPLOYMENT DB
$deploymentid = key($_FILES['baddismantling']['name']);
$file = "baddismantling[$deploymentid]";
if(!$this->upload->do_upload($file))
{
$check3[] = $this->upload->display_errors()." target: ".$file;
}
$check2 = $this->upload->data();
}
next($_FILES['baddismantling']['name']);
}
//SHOW RESULT WHILE UPLOADING
print implode($check3);
}
}
as you see,i've successful with getting the
$deploymentid = 1
as i wish...but, browser give me :
You did not select a file to upload. target: baddismantling[1]
anyone can give me suggestion about that problem?or there's something wrong with my code?
as note :
there's no problem with configuration $this->upload-> because i've
successfull with a single upload form only

thanks for your time with my question, and i've "luckily" got my own solving from question above with this code :
$check3 = array();
$count = 0;
$check = ($_FILES == true ? count($_FILES['baddismantling']['name']) : (0));
while ($fruit_name = ($check == true && $check > 0 ? current($_FILES['baddismantling']['name']) : (false)) || $count <= $check){
$count++;
if($fruit_name == true && key($_FILES['baddismantling']['name']) == true)
{
$iddeployment = key($_FILES['baddismantling']['name']);
/*THIS PART WAS HELPING ME*/if($_FILES['baddismantling']['name'][$iddeployment] == true)
{
$_FILES['userfile']['name'] = $_FILES['baddismantling']['name'][$iddeployment];
$_FILES['userfile']['type'] = $_FILES['baddismantling']['type'][$iddeployment];
$_FILES['userfile']['tmp_name'] = $_FILES['baddismantling']['tmp_name'][$iddeployment];
$_FILES['userfile']['error'] = $_FILES['baddismantling']['error'][$iddeployment];
$_FILES['userfile']['size'] = $_FILES['baddismantling']['size'][$iddeployment];
if (!$this->upload->do_upload())
{
$check3[] = $this->upload->display_errors();
}
else
{
$uploaddata = $this->upload->data();
$result['badfilename'] = $uploaddata['full_path'];
$result['id'] = $iddeployment;
$sql = $this->sql->updatedismantlingBAD($result);
}
}
}
next($_FILES['baddismantling']['name']);}

Related

Check if Two Videos are the Same using php

I have search many time but i did not find any solution so in this case i can not post any code. sorry for this.
I have faced a problem that how can i check that 2 or more video same like i have media folder and many video upload in this folder then when i upload new video then need to check that video already exit or not.
1. if i have video demo.mp4 then when i will try to upload same video then give error
2. if i change video name like demo.mp4 to demo1.mp4 then i will give same error cause video name different but video content same
3. if i upload video demo5.mp4 then show me no error
i already checked image compare using
include('compareImages.php');
$new_image_name = $uploadfile_temp;
$compareMachine = new compareImages($new_image_name);
$image1Hash = $compareMachine->getHasString();
$files = glob("uploads/*.*");
$check_image_duplicate = 0;
for ($i = 0; $i < count($files); $i++) {
$filename = $files[$i];
$image2Hash = $compareMachine->hasStringImage($filename);
$diff = $compareMachine->compareHash($image2Hash);
if($diff < 10){
$check_image_duplicate = 1;
//unlink($new_image_name);
break;
}
}
but i can not compare video. someone help me
Tested and works fine, code taken from :http://php.net/manual/en/function.md5-file.php#94494 not mine.
<?php
define('READ_LEN', 4096);
if(files_identical('demo.mp4', 'demo1.mp4'))
echo 'files identical';
else
echo 'files not identical';
// pass two file names
// returns TRUE if files are the same, FALSE otherwise
function files_identical($fn1, $fn2) {
if(filetype($fn1) !== filetype($fn2))
return FALSE;
if(filesize($fn1) !== filesize($fn2))
return FALSE;
if(!$fp1 = fopen($fn1, 'rb'))
return FALSE;
if(!$fp2 = fopen($fn2, 'rb')) {
fclose($fp1);
return FALSE;
}
$same = TRUE;
while (!feof($fp1) and !feof($fp2))
if(fread($fp1, READ_LEN) !== fread($fp2, READ_LEN)) {
$same = FALSE;
break;
}
if(feof($fp1) !== feof($fp2))
$same = FALSE;
fclose($fp1);
fclose($fp2);
return $same;
}
?>

PHP | Upload Excel into SQL Databse - Getting Error Undefined Offset

Okay, I am getting a Undefiend Offset Error on [1] and [2] which should be $Event_Date and $Event_Organization.
The weird thing is that I also have it set to display the row and the data entered for said row. It even displays this data correctly via echo.
Another strange thing is I have a second CSV upload area on the site that is a carbon copy that work correctly. The only differance is the $_GET ($Type) for the url passed Variable.
Maybe I should change it to a $_POST instead? They only thing $Type does is determine if it is a Bulk Entry or Single Entry.
Edit 1
I changed it from a $_GET to a $_POST by adding a hidden field in the form on the prior page, and still received the same error.
$Type = $_GET["Type"];
if($Type == 'Bulk')
{
if ($_FILES['Excel']['size'] > 0)
{
//get the csv file
$file = $_FILES['Excel']['tmp_name'];
$handle = fopen($file,"r");
//loop through the csv file and insert into database
$column_headers = array();
$row_count = 0;
$Count = 1;
while (($data = fgetcsv($handle, 100000, ",")) !== FALSE)
{
if ($row_count != 0)
{
//user input
$Event_Name = $data[0];
$Event_Date = $data[1];
$Event_Organization = $data[2];
echo ' '.$Event_Name.' '.$Event_Date.' '.$Event_Organization.'<br> <br>';
//Add Account Informaion | Login
$Create_Event_Query ='INSERT INTO Event(Event_Name,
Event_Date, Event_Organization)
VALUES(?,?,?)';
$Create_Event_stmt = $conn->prepare($Create_Event_Query);
$Create_Event_stmt->bind_param('sss',$Event_Name,$Event_Date,$Event_Organization);
$Create_Event_stmt->execute();
if(!$Create_Event_stmt)
{
Echo'<h2>Try Again</h2>';
}
else
{
echo'Enter Row: '.$row_count.'<br>';
echo ' '.$Event_Name.' '.$Event_Date.' '.$Event_Organization.'<br> <br>';
}
$Create_Event_stmt->close();
$row_count = $row_count + 1;
}
else
{
$column_headers = $data;
$row_count = $row_count + 1;
}
}
Echo'<h2>GO Back</h2>';
}
else
{
}
}

can't redirecting after clicking submit button

I'm trying to upload a edited csv file, but after submission it do not redirect
with valid message. Actually if any error in the edited csv file it should give the error msg, otherwise success msg.
I want to upload the mark sheet of a particular subject, if the entered mark is not in numeric or it is exceeding the maximum mark valid messages will be displayed and redirect back to my view page.
function import_academic_excel()
{
$optional_index = 0;
if ($this->input->post('submit'))
{
$config['upload_path'] = './application/temp_upload/';
$config['allowed_types'] = 'csv';
$this->load->library('upload', $config);
if (!$this->upload->do_upload('userfile'))
{
$data = array('error' => $this->upload->display_errors());
$this->session->set_flashdata('msg_excel','Choose a .csv file to upload.');
redirect(base_url().'import_evaluation_sheet');
}
else
{
$count_numeric = 0;
$count_maximum = 0;
foreach($result as $field)
{
if($standard == 11 || $standard == 12)
{
if($evaluation_name == 'ta1' || $evaluation_name == 'ta2' || $evaluation_name == 'ta3')
{
if($subject_name == 'physics' || $subject_name == 'chemistry' || $subject_name == 'computer_science')
{
$theory_name=$subject_name.'_theory';
$practical_name=$subject_name.'_practical';
if($field[$theory_name] == '' )
{
$field[$theory_name]= 0;
}
if($field[$practical_name] == '' )
{
$field[$practical_name]= 0;
}
if($field[$theory_name] != 'ab')
{
if(!is_numeric($field[$theory_name]))
{
$count_numeric = $count_numeric + 1;
}
if($field[$theory_name] > $theory_mark)
{
$count_maximum = $count_maximum + 1;
}
}
}
}
}
}
}
if($count_numeric > 0)
{
$this->session->set_flashdata('msg_excel','Please check the marks entered in the file, some marks are not numeric.');
redirect(base_url().'import_evaluation_sheet');
}
if($count_maximum > 0)
{
$this->session->set_flashdata('msg_excel','Please check the marks entered in the file, some marks exceeds the maximum marks.');
redirect(base_url().'import_evaluation_sheet');
}
$update_result= $this->data->update_chapter($result,$subject_name,$evaluation_name,$optional_index,$standard,$tool_id);
$this->session->set_flashdata('msg_excel_success','Updated data successfully');
redirect(base_url().'import_evaluation_sheet');
}
}
The loops are working, but it can't enter to the last two if loops.
If I am entering valid csv file it will reflect in the data, but success msg can't displayed.

Accessing array values created in foreach loops

I am trying to display an error if a file does not upload. The crux of my problem is that my $status() array doesn't return the values I append in an if statement within a foreach loop.
Can someone enlighten me as to what is going on and what I am doing wrong? I'd like the status values in the foreach loop to append and return outside the loop.
Code below.
$status = array();
// Check that file within size restrictions, then upload.
foreach($request->getFiles() as $file)
{
$file_dir = sfConfig::get("sf_data_dir") . '/retail/order/' . $order_id . '/';
if (!is_dir($file_dir))
{
mkdir($file_dir, 0777, true);
}
$maxsize = 332027;
if(($file['size'] >= $maxsize) || ($file['size'] == 0))
{
$status[] = 0;
}
else
{
move_uploaded_file($file['tmp_name'], $file_dir . $file['name']);
$attachment = new Attachment();
$attachment->setName($file['name']);
$attachment->setPath($file_dir);
$attachment->setSize($file['size']);
$attachment->setCreatedTs('now');
$attachment->save();
$app_attachment = new ApendedAttachment();
$app_attachment->setAttachmentId($attachment->getAttachmentId());
$app_attachment->setWebOrderId($order_id);
$app_attachment->setCreatedTs('now');
$app_attachment->save();
$status[] = 1;
}
}
if (in_array(1, $status))
{
$this->getUser()->setFlash('success', 'Thank you. Your cart has been saved.');
}
else
{
$this->getUser()->setFlash('error', 'Please upload files less than 20MB.');
}
$this->redirect('order/detail?id=' . $order->getWebOrderId());
You are not assigning values to the status array correctly. You probably want to increment an index as you go through the array of files:
$i = 0;
for each(){
...
$status[$i] = 1;
...
$i++;
}
Sorry for the bad code format. Doing this on my mobile device.

Strange PHP multiple file upload issue

Before you asked, I did look at all the similar topics and did not not find the solution to my problem. When I try to upload multiple files, say 4, 3 files are uploaded. I am really sure the loop is correct, but i could be wrong. Self-taught PHP newbie.
The code is as follows:
if(isset($_POST['submit']) && $_POST['submit']=="Upload"){
if(count($_FILES['image_filename']['name']) == 0)
{
$form->seterror("fileupload"," * At least one file required.");
}
if($form->num_errors == 0)
{
$directory="../../images/properties";
$ref='';
//Loop through each file
for($i=0; $i<count($_FILES['image_filename']['name']); $i++)
{
if($_FILES['image_filename']['name'][$i] != "")
{
//Get the temp file path
$filename = $_FILES['image_filename']['name'][$i]; // filename stores the value
$filename = str_replace(" ","_",$filename);// Add _ inplace of blank space in file name, you can remove this line
$filename = stripslashes($filename); // strip file_name of slashes
$filename = str_replace("'","",$filename); //remove quotes
$filesize = $_FILES['image_filename']['size'][$i];
$filetype = $_FILES['image_filename']['type'][$i];
$filetemp = $_FILES['image_filename']['tmp_name'][$i];
//echo $filename.'<br>';
$filework->file_upload($_FILES['image_filename']['name'][$i],$directory,$_FILES['image_filename']['tmp_name'][$i]); // this is my File upload class
$ref=$_FILES['image_filename']['name'][$i];
if($countImg == 0) {
if($i==0) {
$main='1';
} else {
$main='0';
}
} else {
$main='0';
}
$q="INSERT INTO property_images(property_id,image_filename,image_reference,main_image)".
" VALUES (".$_GET['pid'].",'".$_FILES['image_filename']['name'][$i]."','".$ref."','".$main."')";
//echo $q.'<br>';
$database->query($q) or die(mysql_error());
$x++;
}
}
}
}
[/code]
Isn't the loop skipping the last file?
Try changing this:
for($i=0; $i<count($_FILES['image_filename']['name']); $i++)
to:
for($i=0; $i<=count($_FILES['image_filename']['name']); $i++)

Categories