PHP Codeigniter uploads failed with no errors - php

I want to upload a file using a registration form. I used File_Upload library of Codeigniter. But the file does not upload to the destination and no errors appears.
This is just part of my code (All of them are really huge)
Controller (seeker_register.php):
public function submit(){
...
$this->load->model('mseeker_register');
$user_id = $this->mseeker_register->register($data);
View (vseeker_register.php):
$attr = array("class" => 'form-horizontal seeker_register','id' => 'form-seeker-register');
echo form_open_multipart('seeker_register/submit',$attr);
...
<div class="col-sm-6 col-sm-offset-3">
<input name="Aks" type="file" class="fileinput" accept=".jpg, .jpeg">
</div>
Model (mseeker_register.php):
...
// Prepare Aks
$config = array(
'upload_path' => './img/users',
'allowed_types' => 'jpg|jpeg|JPG|JPEG',
'max_size' => '200',
'max_width' => '1024',
'max_height' => '768');
$this->upload->initialize($config);
$this->upload->do_upload('Aks');
$this->upload->display_errors();
exit();
...
This is $this->upload->data() output:
Array
(
[file_name] => Clipboard-2.jpg
[file_type] => image/jpeg
[file_path] => D:/khayyamkar.ir/www/img/users/
[full_path] => D:/khayyamkar.ir/www/img/users/Clipboard-2.jpg
[raw_name] => Clipboard-2
[orig_name] =>
[client_name] => Clipboard-2.jpg
[file_ext] => .jpg
[file_size] => 156.42
[is_image] => 1
[image_width] =>
[image_height] =>
[image_type] =>
[image_size_str] =>
)

When I scanned your code I realized that you you don't have a way to view the error(s).
To view what the error(s) are/is you need to use var_dump() or print_r() php method to view what are the error(s).
for example:
var_dump($this->upload->display_errors());
in your current code you need to change :
$this->upload->display_errors();
exit();
TO:
var_dump($this->upload->display_errors());
exit();
You need to identify the error first to come up with a solution.
:)

public function register() {
$config = array(
'upload_path' => './img/user',
'allowed_types' => 'jpg|jpeg|JPG|JPEG|png',
'max_size' => '200',
'max_width' => '1024',
'max_height' => '768');
$this->upload->initialize($config);
// you need to make sure that the upload path is existing
// and also check the folder permission to be rwxr-xr-x
// if you installed in a server where permission is required
// for you to create image inside the folder
// create a folder img/user in the root directory of the project
// where application or system located
// this will check
if ($this->upload->do_upload('Aks')) {
echo 'success'; die;
} else {
var_dump($this->upload->display_errors());die;
}
}
see File Upload CodeIgniter

Related

Codeigniter AJAX file upload not working

I am trying to upload a file to the same directory in which application folder is present
-application
-images
-system
-others...
The code that I am trying is
$config = array(
'upload_path' => "images",
'allowed_types' => "*",
'overwrite' => TRUE,
'max_size' => "2048000", // Can be set to particular file size , here it is 2 MB(2048 Kb)
'max_height' => "768",
'max_width' => "1024",
);
if(isset($_FILES["image"]["name"])) {
if (0 < $_FILES['image']['error']) {
echo 'Error Occurred During Upload Of File ' . $_FILES['image']['error'];
} else {
$this->load->library('upload', $config);
if(!$this->upload->do_upload('image')) {
echo $this->upload->display_errors(); //ALWAYS THIS LINE IS EXECUTING
}
else {
echo "FILE UPLOADED";
}
}
} else {
echo "Please Select A File";
}
The problem is that always the line having comment inside innermost if is executing and it says <p>The upload path does not appear to be valid.</p>.
When I tried to check whether that directory exists using is_dir, it also says that directory exists and also I tried giving it 777 file permission but still I am getting the same errors. I also tried most of the similar answers online but none worked.
Please HELP
Try this:
$config = array(
'upload_path' => "./images/",
'allowed_types' => "*",
'overwrite' => TRUE,
'max_size' => "2048000",
'max_height' => "768",
'max_width' => "1024",
);

Codeigniter file path is not stored properly in database

The file path of the file I'm trying to upload is not stored properly.
My only problem now is, instead of storing this path:
C:/xampp/htdocs/dev/csbrms/upload/file.xlsx
it only stores:
C:/xampp/htdocs/dev/csbrms/upl
this is my code
view.php
<input type = "file" name = "userfile" id="userfile"/>
controller.php
$upload_data = $this->upload->data();
$file_name = $upload_data['file_name'];
$data = array(...
'file_path' => $file_name,
);
model.php
function add_records($data){
try {
$sql = "CALL `sp_add_user_request`(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
$result = $this->csbrms->query($sql,$data); // $data included 3 param and binding & query to db
$this->csbrms->close();
} catch (Exception $e) {
echo $e->getMessage();
}
return $result;
}
You are using file_path to store your file info, whereas you probably want to use full_path which will include the path and filename.
$file = $upload_data['full_path'];
You can see all the return fields for the helper function in the docs here:
http://www.codeigniter.com/user_guide/libraries/file_uploading.html#CI_Upload::data
But here is the example return array with the fields you can use:
Array
(
[file_name] => mypic.jpg
[file_type] => image/jpeg
[file_path] => /path/to/your/upload/
[full_path] => /path/to/your/upload/jpg.jpg
[raw_name] => mypic
[orig_name] => mypic.jpg
[client_name] => mypic.jpg
[file_ext] => .jpg
[file_size] => 22.2
[is_image] => 1
[image_width] => 800
[image_height] => 600
[image_type] => jpeg
[image_size_str] => width="800" height="200"
)
Hope that helps,
Paul.
Check your field size in database. The path is being truncated due to the size of the column/field in the table where the path is being stored.

CodeIgniter file upload validation of ppt and pptx files

$config = array(
'upload_path' => 'resources/lectures',
'allowed_types' => 'pdf|zip|doc|docx|ppt|pptx',
'remove_spaces' => TRUE,
'overwrite' => FALSE,
'max_size' => 0,
);
This is my config array for file upload, and all files are uploading fine except for ppt and pptx. I checked the error message using the display_errors() method, it said 'The filetype you are attempting to upload is not allowed'.
This is a bug of Codeigniter.
Open application/config/mime.php
and replace line no 33 (probably) with the following line:
'ppt' => array('application/powerpoint', 'application/vnd.ms-powerpoint', 'application/vnd.ms-office', 'application/msword'),
and add the following line to the array:
'pptx' => array('application/vnd.openxmlformats-officedocument.presentationml.presentation', 'application/zip', 'application/msword','application/vnd.ms-powerpoint'),

CodeIgniter uploading files no file

Ok try to upload file for hours but i get error,
You did not select a file to upload.
my code is in CI
$this->config = array(
'upload_path' => dirname($_SERVER["SCRIPT_FILENAME"])."/uploads/",
'upload_url' => base_url()."uploads/",
'allowed_types' => "gif|jpg|png|jpeg|pdf|doc|xml",
'overwrite' => TRUE,
'max_size' => "1000KB",
'max_height' => "768",
'max_width' => "1024"
);
$this->load->library('upload', $this->config);
if($this->upload->do_upload('logo'))
{
echo "file upload success";
}
else
{
echo $this->upload->display_errors();
}
in view i have
<input type="file" name="logo"/>
when i print_r $_POST i get
Array ( [name_srpski] => tyre [name_english] => Client nametre [logo] => cipele-plava_1.jpg )
Where could be error its very important
Try the following in config:
'upload_path' => FCPATH . "/uploads/", // or use "./uploads/" instead
'max_size' => "1000", // remove the kb from the string. it requires only the number
Try changing $this->load->library('upload', $this->config); to
$this->load->library('upload');
$this->upload->initialize( $this->config );
Also the form type should be multipart
<form method="post" action="some_action" enctype="multipart/form-data" />

How to code multiple file upload using CodeIgniter?

I want to have a multiple file upload code.
For example:
Koala.jpg
Penguins.jpg
Jellyfish.jpg
There is input text where the user can set the new name of the image.
The user will now upload the images and the inputted text for new image name is "Animals"
Now, what I want is when this uploaded the output should be Animals1.jpg, Animals2.jpg, Animals3.jpg.
The problem is when I tried to upload all these images, only one image is uploading.
I tried to make research and applied some codes on my program, but still not working.
Controller
public function do_upload() {
$config = array(
'image_library' => 'gd2',
'file_name' => $this->input->post('finame'),
'upload_path' => './public/img/uploads',
'upload_url' => base_url().'public/img/uploads',
'allowed_types' => 'gif|jpg|jpeg',
'max_size' => '1024KB',
'max_width' => '1024',
'max_height' => '768',
'maintain_ratio'=> TRUE,
'overwrite' => false,
);
$this->load->library('upload', $config);
if (!$this->upload->do_upload()) {
$error_msg = "<div class='alert alert-error'>".$this->upload->display_errors()."</div>";
$error = array('error' => $error_msg);
}
else {
$upload_data = $this->upload->data();
$data['thumbnail_name'] = $upload_data['raw_name']. '_thumb' .$upload_data['file_ext'];
$file_array = array(
'image' => $data['thumbnail_name'],
'image_name' => $upload_data['file_name'],
//'description' => "",
'date_created' => date('Y-m-d H:i:s', now()),
'date_modified' => date('Y-m-d H:i:s', now()),
'author' => $this->session->userdata('username'),
'size' => $upload_data['file_size'],
'type' => $upload_data['image_type'],
'width' => $upload_data['image_width'],
'height' => $upload_data['image_height'],
//'document_name' => $field,
//'department' => $field2,
//'notes' => "",
);
$this->session->set_userdata('image_print', $file_array);
$this->load->database();
$this->db->insert('tbl_image', $file_array);
$data = array('upload_data' => $this->upload->data());
$user_level['records']=$this->user_model->get_records();
$this->load->view('v_dashboard/page/header_view', $user_level);
$this->load->view('v_document/upload/upload_result_view', $data);
$this->load->view('v_dashboard/page/footer_view');
}
}
I have this on my HTML
<label for="file"><strong>Select File To Upload:</strong></label>
<input type="file" name="userfile[]" multiple class="btn transcolor btn-file"/>
<br/><br/>
By the way, I'm using BLOB on my database.
I tried to refer to this links
Ellislab
GitHub
StackOverflow
StackOverflow
CodingLikeASir
You have to run the for loop till the count of the uploaded image file.
Like this:
for ($i=0; $i < count($_FILES['userfile']['name']); $i++)
{
// function to add the image name one by one into database.
}

Categories