codeigniter upload file redirects to unknown link - php

this is my controller
public function upload_file(){
$this->load->helper('form');
$this->load->helper('url');
// set path to store uploaded files
$config['upload_path'] = './uploads/';
// set allowed file types
$config['allowed_types'] = 'pdf';
// set upload limit, set 0 for no limit
//$config['max_size'] = 0;
// load upload library with custom config settings
$this->load->library('upload', $config);
if($this->upload->do_upload()){
$this->add_view();
}else{
print_r('x');
exit();
}
}
and my view is
echo form_open_multipart('PDFuploads/upload');
the problem is i don't know why it redirected into this link
http://localhost/TLC_HR/PDFuploads/upload

Give the correct controller and action here
echo form_open_multipart('controller/action');
and you mentioned your controller action as upload_file()

$this->upload->do_upload('userfile')
userfile is missing. input type = "file" name = "userfile"

Check your 'base_url' in application/config/config.php

Related

How to Display Existing User Pictures In Codeigniter.?

I have a complete php script which i bought from codecanyon and the Author does not support costum work... it was made with Codeigniter framework and its working fine and perfectly but the script does not support
Ability for User to Upload Profile Image
Ability for uploaded image be shown in profile page.
3 Ability to ReUpload and or delete image.....
I have search google for it and could not find answers to this.
I have tried Creating >Upload-Controller > Upload View > Upload
Model, which only upload to a specified folder in a specific url e.g site.com/uploadimage.
I want to ask if there is any way to integrate this feature to the system... Am a php newbie.
Many Thank in Advance...
I hope this can help you.
Create this folder path for uploading the image : app_data/images/account/
// Your Controller
Class User extends CI_Controller
{
// This upload method
function upload_user_photo()
{
$path = 'app_data/images/account/';
if(!file_exists($path))
{
mkdir($path, 0777, true);
}
$file_name = increment_string('image_profile', '-'); //use codeigniter string helper
$config['upload_path'] = $path;
$config['file_name'] = $file_name;
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '100';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$this->load->library('upload', $config);
//Take an action if photo has been uploaded
if($this->upload->do_upload('image'))
{
$upload_data = $this->upload->data();
// View response from upload data
echo "<pre>";
print_r ($upload_data);
echo "</pre>";
// Array key is column name of your table
$data =
[
'user_id' => 'insert our user id',
'photo' => $upload_data['file_name']
];
//Take action save to your model
$this->load->model('your_model_name');
$this->your_model_name->save_profile_image($data);
}
}
}
#Model
Class Your_model_name extends CI_Model
{
function save_profile_image($data)
{
$this->db->insert($data);
}
}
Read more about codeigniter upload setting preferences

Codeigniter file upload code not working

Controller Code:
if(isset($_FILES['imageupload']) && $_FILES['imageupload']['size'] > 0){
echo 'inside if';
if ( ! $this->upload->do_upload('imageupload'))
{
echo 'inside if';
$error = array('error' => $this->upload->display_errors());
}
else
{
echo 'inside else';
$upload_data = $this->upload->data();
$file_name[] = $upload_data['file_name'];
}
}
View Code:
<input type="file" class="default" id="imageupload" name="imageupload">
ERROR: Call to a member function do_upload() on a non-object
Hoow can I resolve this error please help me.
Did you load the library upload on your controller? I guess the problem of your code is lacking of this code $this->load->library('upload', $config); try to check this documentation hope it will help you. https://www.codeigniter.com/userguide3/libraries/file_uploading.html
The error indicates that the upload library is not loaded. You need to load the upload library somewhere and in the constructor of the controller is one possible choice. Add the following to the controller code.
function __construct()
{
parent::__construct();
$this->load->library('upload');
}
The other place to load this library is in application/config/autoload.php using the $autoload['libraries'] item.
$autoload['libraries'] = array('upload');
Other libraries can be loaded at the same time. See the comments in autoload.php for more details.
Add upload library before you could call do_upload with config
$config['upload_path'] = '/path/';
$config['allowed_types'] = '*';
$config['file_name'] = 'file_name';
$config['overwrite'] = TRUE|FALSE;
$this->load->library('upload', $config);

CodeIgniter doesn't load upload library

I'm having a problem with loading a CodeIgniter library,
I've already built three (3) websites with CodeIgniter, so I consider myself familiar with this framework.
The problem is only with the upload lib. I've tried with multiple functions and it still doesn't work.
I've even tried with the CodeIgniter example
In google I can't find any answers, does anyone have an idea what it could be?
class Admin extends CI_Controller {
public function __construct(){
parent::__construct();
$this->load->library('session');
$this->load->library('v');
$this->load->model('admin_model');
}
public function upload_a_thing($set = null){
$this->load->helper(array('form', 'url'));
if(!$set){
$this->load->view('admin/upload_a_thing');
}else{
$this->load->library('Upload');
if (!$this->upload->do_upload('userfile')){
echo $this->upload->display_errors();
} else {
$file = $this->upload->data();
// $product = $this->admin_model->get_products($id);
$newFilePath = $config['upload_path'].'try.jpg';
$file['file_name'] = str_replace(" ","_",$file['file_name']);
rename($config['upload_path'].$file['file_name'], $newFilePath);
}
}
}
CodeIgniter Error
Undefined property: Admin::$upload
PHP error
Fatal error: Call to a member function do_upload() on a non-object
Edited for Spary
$config['upload_path'] = './media/imgs/products/';
$this->load->library('upload',$config);
if (!$this->upload->do_upload('userfile')){
echo $this->upload->display_errors();
}
If your form looks like this:
<input type="file" class="input" name="logo" />
then call do_upload() like this:
do_upload('logo');
If it looks like this:
<input type="file" class="input" name="userfile" />
then call it like this:
do_upload();
do something like this
if (!$this->upload->do_upload('userfile')){
echo $this->upload->display_errors();
}
Your code...
$this->load->library('Upload');
As per all examples in the documentation, upload should be in all lower-case and most likely the reason the library is not loading.
Additionally, if your $config array is missing, you're not following the example code in the documentation:
$config['upload_path'] = './uploads/'; // <- preferences
$this->load->library('upload', $config); // <- load library and set configuration
OR
$this->load->library('upload'); // <- load library
$config['upload_path'] = './uploads/'; // <- preferences
$this->upload->initialize($config); // <- set configuration
OR
// 'upload' library is "auto-loaded" // <- load library
$config['upload_path'] = './uploads/'; // <- preferences
$this->upload->initialize($config); // <- set configuration
From the chart of preferences, they all seem optional, except for upload_path...
Default Value: None
Description: The path to the folder where the upload should be placed. The folder must be writable and the path can be absolute or relative.
I'm not sure how CodeIgniter could know where to upload the file when there's no upload_path because there is no default and your entire configuration array is missing.

Upload File with Related Extension Filter

I have problems on my upload page. When I try to upload file, the extension that can be selected set by "all type", but I've tried to set my allowed type config to jpg. This is my controller :
public function upload_file($noArsip)
{
$config['upload_path'] = './assets/img/arsip/';
$config['allowed_types'] = 'jpg'; // --> how to make the allowed type only image extension?
$this->load->library('upload', $config);
$this->upload->do_upload('video');
$data_upload_files = $this->upload->data();
//proses database
$image = $data_upload_files['file_name'];
$data = array(
'video'=>$image
);
$this->daftar_arsip_m->updateData($noArsip,$data);
//$this->load->view("upload_foto_v", $data);
redirect('upload_foto');
}
i dont want to use validation when the file is selected, i just want it to filter the file for the related extension, like image or video related type. thanks for your help
Separate extensions by |
$config['allowed_types'] = 'gif|jpg|png';
PS: Extensions are not case sensitive, codeigniter will take care of Upper case formats.
Reference
To filter the browse window, just add accept="image/gif,image/jpeg,image/png" as attribute to the input tag.
Example:
<input type="file" accept="image/gif,image/jpeg,image/png">
Live demo

How to validate uploads as a group in CodeIgniter, so they all either upload or fail

Firstly, I'm not asking how to perform a multi-file upload using CodeIgniter, but rather how to prevent any/all files from being uploaded if the last one in line fails (due to filetype, size, etc.). Basically I'm looking to use CI's file validation up front (before anything is uploaded), and if everything looks good, perform all uploads.
I have a form that uploads two files. If the first file passes validation and the do_upload() method returns TRUE, but the second file fails, the first file was still uploaded -- how can I prevent this?
Here's my code:
// Controller
function upload_files()
{
// Configure first file upload
$config['upload_path'] = 'public/mp3/';
$config['overwrite'] = FALSE;
$config['allowed_types'] = 'mp3';
$config['max_size'] = '20000';
$config['remove_spaces'] = TRUE;
$this->upload->initialize($config);
// Perform first file upload
if ($this->upload->do_upload('mp3'))
{
// Store upload file info for later use
$mp3_info = $this->upload->data();
// Configure second file upload
$config['upload_path'] = 'public/doc/';
$config['overwrite'] = FALSE;
$config['allowed_types'] = 'doc|docx';
$config['max_size'] = '3000';
$config['remove_spaces'] = TRUE;
$this->upload->initialize($config);
// Perform second file upload
// Even if this fails, the first file was still uploaded
if ($this->upload->do_upload('doc'))
{
// Store upload file info for later use
$doc_info = $this->upload->data();
// Perform database interactions here
}
else
{
echo $this->upload->display_errors();
}
}
else
{
echo $this->upload->display_errors();
}
}
No dice! You can't validate an upload from php or codeigniter unless you execute the upload. How can php validate something it doesn't yet have access to?
You have to do some validation up-front with javascript. If all files pass the js validation, then upload the files and complete validation with php/codeigniter.

Categories