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
Related
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
My teacher,he was uploading and retrieving images from folder with ID recognition. It works with database.
So, when we upload an image, it will be stored in a folder with it's name changing automatically with ID.
Example: The upload form must be on "yourwebsite.com/yourpages/3144242" so the image will be stored as "3144242" and blablabla the same for other pages with ID
This is how he upload and retrieve it
<?php
$ff = './assets/images/kegiatan/'.$_SESSION['idkeg'].'.jpg';
if( file_exists( $ff ) ) {
<img src="<?= base_url()?>assets/images/kegiatan/<?=$_SESSION['idkeg']?>.jpg" />
<?php
}
?>
Retrieve images part:
if( file_exists( $ff ) ) {
<img src="<?= base_url()?>assets/images/kegiatan/<?=$_SESSION['idkeg']?>.jpg" />
<?php
}
?>
However, I change it a bit so I don't have to use a database and $_SESSION things. With this:
<?php
$ff = './assets/images/kegiatan/'.$this->input->post('img_name').'.jpg';
?>
<form id="form" class="kart" action="<?= base_url()?>gallery/upload2" method="post" enctype="multipart/form-data">
<div class="form-group">
<input type="text" class="form-control" name="img_name" required="required">
</div>
<div class="form-group">
<input type="file" class="form-control" name="foto">
</div>
<div class="form-group">
<button type="submit" class="btn btn-common" value="Update"><i class="fa fa-upload fa-2x"> Upload</i></button>
</div>
</form>
It works uploading images into folder "kegiatan" (means activity) with simply write the names of images in a form. The img_name that you have input, now become the name of that image.
Now I have a little problem to retrieve that images. How?
mistake in your form I realized is asking user to enter image name and get image from folder by using that input value.
First of all, I haven't tried upload an image with codeigniter but I will answer reference to php itself.
When we get an image as input from user by form, then we use "move_uploaded_file" function in php and we send temp_file, path with file name arguments to this function to save the file user selected in the form we prepared.
move_uploaded_file(string $filename , string $target);
// $filename: I use $_FILES['getFileUpload']['tmp_name'] for this argument
// $target: I define path with file name, eg: uploads/files/new_file_25.jpg for jpg image
So, you souldn't ask user to enter file name, because you will deifne it. Or, you must use "img_name" that you asked user to enter as file name which is most probably won't satisfy your ID prediction.
I hope this will help you.
I have been working on a registration form for opencart for a while now. I think I am nearly there in how I have coded it.
However I just do not know where I am going wrong with it.
It is a standard form with text input fields and I just now need a file to be uploaded also on registration. I have added the upload button and added controller code just not working.
This is what I have so far:
Template file:
<legend>Documents</legend>
<p>Please upload as .pdf to ensure a smooth upload, thank you.</p>
<div class="form-group required">
<label class="col-sm-2 control-label" for="input-ast">AST:</label>
<div class="col-sm-10">
<input type="file" name="ast" value="<?php echo "AST"; ?>" placeholder="<?php echo $entry_ast; ?>" id="input-ast" class="form-control" />
</div>
</div>
Controller:
$data['ast'] = '';
$uploads_dir = 'tenant/upload/'; // set you upload path here
if (is_uploaded_file($this->request->files['ast']['tmp_name'])) {
move_uploaded_file($this->request->files['ast']['tmp_name'],$uploads_dir.$this->request->files['ast']['name']);
$data['ast'] = $this->request->files['ast']['name'];
}
Any input would be greatly appreciated
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);
}
I am trying to upload an image using laravel4 and I've successfully managed to upload a file from a form and store it in a directory of my choice in the public folder. However the issue I am running into is viewing the images. I am developing in Netbeans and when I try to open my newly uploaded image I am told the image is corrupted.
This is my form:
<form action="{{ action('BookController#handleCreate') }}" method="post" role="form" enctype="multipart/form-data">
<div class="form-group">
<label>Name</label>
<input type="text" class="form-control" name="name" />
</div>
<div class="form-group">
<label>Description</label>
<input type="textarea" class="form-control" name="desc" />
</div>
<!-- Img upload -->
<input type="file" name="img"/>
<input type="submit" value="Create" class="btn btn-primary" />
Cancel
</form>
This is my controller:
public function handleCreate(){
$book = new Book;
$book->book_name = Input::get('name');
$book->book_desc = Input::get('desc');
$destinationPath = public_path().'/uploads/covers/';//Set up destination path
$file = Input::file('img');//Get the file
$extension = $file->getClientOriginalExtension();//Get the extension
$filename = str_random(12).".".$extension;//Create a filename
$upload_success = $file->move($destinationPath, $filename);//Move the file to the destination
$pathToFile = '/uploads/covers/'.$filename;
echo $pathToFile;
//If successful.....
if($upload_success){
$book->book_cover = $filename;//store value in db
$book->save();//Save the book
return Redirect::action('BookController#index');
}else{//Else return to form with an error message....
return Response::json('error', 400);
}
}
This is the path to my image
/uploads/covers/VFBDJPqEdI6P.jpg
My plan is that I will store this path in the database and use it to display the image on the screen.
I don't know why but whilst I am uploading the files they are being corrupted and I'd appreciate if someone might suggest a way I could fix this.
Regards
I tried this and worked without losing any content
Storage::put($pathToFile,file_get_contents($request->file('img')->getRealPath()));