import CSV : Undefined Index in codeigniter - php

I want to import CSV file in MySQL using CodeIgniter, I am defined names and right functions(hope so), but getting the undefined index of my file I m finding solutions and also getting one but won't work.
Controller
$file_data = $this->csvimport->get_array($_FILES['csvfile']['name']);
foreach($file_data as $row)
{
$data[] = array(
'Hall_Ticket_No' => $row['Hall_Ticket_No'],
'Name' => $row['Name'],
'Course' => $row['Course']
);
}
$data['query'] = $this->ExamModel->insertBlock($data);
HTML
<div class="form-group col-lg-12 col-xs-12">
<label for="csv_file">Select excle (CSV) file:</label>
<input type="file" name='csvfile' accept=".csv" class="form-control" id="csv_file" required="">
</div>
Error
Severity: Notice
Message: Undefined index: csvfile
Filename: controllers/Exam.php
Line Number: 20
Erros image

Make sure that in your form.. you put the enctype.
eg:
<form method="post" enctype="multipart/form-data" action="index.php"></form>
To check if files are successfully updated upon submitting the form. use print_r to see results.
print_r($_FILES);

<form action="{UPLOAD-URL}" method="post" enctype="multipart/form-data">
<div class="form-group col-lg-12 col-xs-12">
<label for="csv_file">Select excle (CSV) file:</label>
<input type="file" name='csvfile' accept=".csv" class="form-control" id="csv_file" required="">
<input type="submit" value="Upload Image" name="submit">
</div>
<input type="submit" value="Upload Image" name="submit">
</form>
Try above code by replacing {UPLOAD-URL} which works for me. Basically I have added form element with enctype attribute to your code.
Using JS
function readSingleFile(evt) {
//Retrieve the first (and only!) File from the FileList object
var f = evt.target.files[0];
if (f) {
var r = new FileReader();
r.onload = function(e) {
var contents = e.target.result;
alert( "Got the file.n"
+"name: " + f.name + "n"
+"type: " + f.type + "n"
+"size: " + f.size + " bytesn"
+ "starts with: " + contents.substr(1, contents.indexOf("n"))
);
}
r.readAsText(f); //Reading the file
//Add AJAX code here to submit the file
} else {
alert("Failed to load file");
}
}
document.getElementById('csvfile').addEventListener('change', readSingleFile, false);

This error could happen if the CSV file is formatted as "UTF-8 with BOM". Open the CSV file in Notepad and see the formatting in the bottom right corner.
If creating the CSV files with Excel, make sure when saving to use the "CSV (MS-DOS) (*.csv)" option and NOT the "CSV (UTF-8)".
See Whats the difference between utf-8 and utf-8 without bom

Related

Undefined index when data with file path updating?

I have a file upload form it works perfect when new file upload.But index error when update. At update data, i want that if new file select it will upload new file and update the data with that file path(which worked perfect) but when updating (just want to update old data) it gives index error. i am using codeigniter.
html form :
<section>
<label class="label" for="FileUpload">
Activity Image File</label>
<label for="file" class="input input-file">
<div class="button">
<input id="fileToUploadactivity" name="fileToUploadactivity" type="file" onchange="updateInput(this.value)" > <!-- onchange is use to put the file name on below input field -->
Browse
</div>
<input type="text" name="SelectedFileName" id="SelectedFileName" value="<?php echo $value['file_path']; ?>" readonly>
in php
//if (is_uploaded_file($_FILES['fileToUploadactivity']['tmp_name'])){ // also error in here
$newfile = $_FILES["fileToUploadactivity"]["tmp_name"];
if (!empty($newfile)) {
// new file uploding code
}else{
// old data upload
}
error
Message: Undefined index: fileToUploadactivity

Using PHPExcelReader to parse file and store values in array - File not found

I am using PHPExcelReader to parse an excel file (chosen by the user from their local machine). PHP will take those values and store them in an array and then reorganize it and display it on the screen in the form of a table.
However, after I browse for a file, once I click "Submit", I get the error: File not found
Here is the HTML:
<div class="row-fluid" style="margin-top:15px">
<h2>Step 1: Import the file</h2>
<p>Once uploaded, the window will display a preview of the document. Please check to make sure the column
headers match the data before uploading.</p>
<form action="../scripts/previewFile.php" method="post" enctype="multipart/form-data">
<input type="file" name="fileToUpload" />
<input type="submit" value="Preview File" name="submit">
<div id="filePreview" style="height: 500px; overflow: scroll;">
</div>
</form>
</div>
The previewFile.php file that is called:
function previewFile()
{
$filePath = $_FILES['file']['tmp_name'];
$excel = new Spreadsheet_Excel_Reader; // creates object instance of the class
$excel->read($filePath);
}
UPDATE
The error was a 404 for the PHP file "previewFile.php" that is called as an action.
Not sure what's wrong with my URL "../scripts/previewFile.php"
Here's my schema:
root
|_views
|_default
|_assets
|_scripts
|_previewFile.php
|_templates
|_step1.php <- the html file with the input tags
Your input name is fileToUpload, and you are looking in the $_FILES array for file, so it should be as follows:
function previewFile()
{
$filePath = $_FILES['fileToUpload']['tmp_name'];
$excel = new Spreadsheet_Excel_Reader; // creates object instance of the class
$excel->read($filePath);
}

$_FILE textfield name index is not recognized

I am trying to store an image into an mysql database but in the ajax page $_FILE['textfield_Name']['temp_name'] is not recognizing it. Help me to solve this problem.
This is my file page code
<form class="form-horizontal signUPForm" method="post" action="most_wanted_ajax.php">
<label for="images">Upload Image</label>
<input type="file" name="images">
<br>
<button type="submit" id="add" value="Submit" >Add Most Wanted</button>
</form>
my jquery code is:
$("#add").click(function (e){
$.post($(".signUPForm").attr("action"),$(".signUPForm").serializeArray(),function(res){
if(res!=null){
alert("mosted wanted added");
}
else
{
alert("Somthing is fishy");
}
});
});
and here is my most_wanted_ajax.php code
<?php
if(getimagesize($_FILES['images']['tmp_name'])==FALSE)
{}
else
{
$image=addslashes($_FILES['images']['tmp_name']);
$image= file_get_contents($image);
$image= base64_encode($image);
$con=mysqli_connect("localhost","root","","ecopsweb");
$insertSQL="insert into most_wanted(images) values('".$image."')";
$rs= mysqli_query($con, $insertSQL);
$id= mysqli_insert_id($con);
header("location: missing_person_info.php");
}
?>
in the most_wanted_ajax.php page give two errors
Notice: Undefined index: images in
C:\wamp\www\eCopsWeb\adminModule\most_wanted_ajax.php on line 3
Warning: getimagesize(): Filename cannot be empty in
C:\wamp\www\eCopsWeb\adminModule\most_wanted_ajax.php on line 3
Add enctype="multipart/form-data"> to your form tag.This value is required when you are using forms that have a file upload control
It would be
<form class="form-horizontal signUPForm" method="post" action="most_wanted_ajax.php" `enctype="multipart/form-data"`>

My $_FILES['file']['name'] is producing gibberish

I am uploading files with the following code using Bootstrap as my front-end framework.
<form method="POST" enctype="multipart/form-data" action= "php/up-load.php" role="form"
<div class="form-group">
<label for="file" class="col-sm-5 control-label">
Select file(Compressed format)
</label>
<div>
<input type="hidden" name="MAX_FILE_SIZE" value="1000000000"/>
<input type="file" id="file" name="file" accept=".zip, .rar"/>
</div>
</div>
<div>
<button type="submit" class="btn btn-primary" name="upload" id="upload">Send</button>
</div>
</form>
My php code is;
# $original=$_FILES['file']['name'];
# $kiss=pathinfo($original, PATHINFO_EXTENSION);
$allowed_extensions = array(".zip","rar","bzip2","iso","gz","rz","7z","tar","tgz","bz2","tbz2","lzma","tlz");
$result = in_array ("$kiss", $allowed_extensions);
if (!$result)
{
// Wrong file type
}
else
{
//proceed..
}
My problem is that the
$_FILES['file']['name'];
is returning gibberish such as php7vy8X9 and phpY8wQVR . Have tried everything. What could be the problem?
$_FILES["file"]["name"] should be returning the original name of your file, what you are reporting should be stored inside $_FILES["file"]["tmp_name"]
The whole $_FILES array should look like this:
echo $_FILES["file"]["name"]; // Original Name
echo $_FILES["file"]["tmp_name"]; // Name (and location) given to the file by PHP
echo $_FILES["file"]["type"]; // File/mime type
echo $_FILES["file"]["size"]; // Total bytes
echo $_FILES["file"]["error"]; // Any error code which is returned during transfer

Attachments added through JavaScript function are not sent

I am using the following loop to send/attach multiple attachments to my email message.
foreach(array_keys($_FILES['attachment']['name']) as $key) {
$source = $_FILES['attachment']['tmp_name'][$key];
$filename = $_FILES['attachment']['name'][$key]; // original filename from the client
$mail->AddAttachment($source, $filename);
}
But only the first attachment i made is sended.
The form code is
<form method="POST" action="<?php $PHP_SELF ?>" enctype="multipart/form-data">
<input type="file" name="attachment[]" id="attachment" size="30"
onchange="document.getElementById('moreUploadsLink').style.display = 'block';" />
<div id="moreUploads"></div>
<div id="moreUploadsLink" style="display:none;">
Attach another File</div>
<input name="submit" type="submit" value="submit" />
</form>
When one click on Attach another file, another File Upload button is shown, through a JavaScript function:
<script type="text/javascript">
var upload_number = 1;
var attachmentlimit = 5;
function addFileInput() {
var d = document.createElement("div");
var file = document.createElement("input");
file.setAttribute("type", "file");
file.setAttribute("name", "attachment"+upload_number);
d.appendChild(file);
document.getElementById("moreUploads").appendChild(d);
upload_number++;
if(upload_number == attachmentlimit) {
document.getElementById('moreUploadsLink').style.display='none';
}
}
</script>
Only the file attached through the first FileUpload Button is attached and sended, not the others.
Help.
The problem is that you are setting the name attribute of your new input element to attachment1, attachment2, etc. You should be setting this name to attachment[], just like the original input you created.
If you still don't get all your attachments, you can try a var_dump($_FILES) in your PHP script to ensure that you are getting all the files the way you expect them.

Categories