I have the following code in a controller function (this is CI):
if(!empty($_FILES['filename']) && is_uploaded_file($_FILES['filename']['tmp_name'])) {
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '1000';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$this->load->library('upload', $config);
$this->upload->overwrite = true;
if ( ! $this->upload->do_upload('filename')){
$error = array('error' => $this->upload->display_errors());
var_dump($error);
exit;
}
else {
$this->upload->do_upload('filename');
$upload_info = $this->upload->data();
var_dump($upload_info);
exit;
}
}
This should work, it return no errors, and returns the $upload_info array before I exit(); (exit is here for debugging). The $upload_info array suggests that the file has been passed to the correct path on the server. The folder permissions are set to 777 on this folder.
However, the file does not appear on the server! Could anyone suggest what the issue might be here. All the correct libraries have also been loaded.
You call for $this->upload->do_upload('filename') twice.
First time in if and then second time in else-branch.
You should remove second $this->upload->do_upload('filename').
Final code:
if ( ! $this->upload->do_upload('filename')){
$error = array('error' => $this->upload->display_errors());
var_dump($error);
exit;
}
else {
$upload_info = $this->upload->data();
var_dump($upload_info);
exit;
}
Related
I have a form including different inputs and file upload input. I want the users if they want to upload images then upload, but if they don't want to upload. Don't give an error.
if($_POST){
$config['upload_path'] = 'images/tmp';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 2048;
$config['min_width'] = 480;
$config['min_height'] = 360;
$this->load->library('upload', $config);
if (!$this->upload->do_upload('userfile')) {
$error = array('error' => $this->upload->display_errors());
//Sending this $error to view and if a user didnt upload image and just filled
//the other inputs it shows error. but i dont want that.
} else { }
} else {
redirect(site_url());
}
You are on right track. Just delete or comment this line
$error = array('error' => $this->upload->display_errors());
You might be send your $error to view file like below:
$this->load->view('upload_form', $error);
So Don't send your value to view file. Just call your view when image successfully uploaded below:
if ( ! $this->upload->do_upload('userfile') )
{
// $error = array('error' => $this->upload->display_errors());
/*Delete or comment this line. Do your other stuff here if image not uploaded.*/
}else{
$data = array('upload_data' => $this->upload->data());
$this->load->view('upload_success', $data);
/* This is example. You can do anything on successfully image upload.*/
}
You can refer codeigniter manual https://www.codeigniter.com/userguide3/libraries/file_uploading.html
$config['upload_path'] = 'images/tmp';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 2048;
$config['min_width'] = 480;
$config['min_height'] = 360;
$this->load->library('upload', $config);
//$rand_str="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
// $filename=md5(str_shuffle($rand_str.mt_rand()));
// $config['file_name']=$filename;
$this->upload->initialize($config);
if ( $this->upload->do_upload('userfile'))
{
//$data = array('upload_data' => $this->upload->data());
// $data["filename"]=$filename;
// $data["ad_id"]=$lid;
// $data["filename"]=$rand_name;
// $this->Ads_model->insert_image($data);
}
I solved my problem changing the Upload class to give no error. I commented the lines including "no selected files".
hi i'm trying to upload only image files using code igniter.I need to upload only png,jpg,jpeg files.but other type files also uploaded like .txt,.mp3 etc.my code is given below.please help me to solve this issue as immediately.Thanks to all in advance.
$config['upload_path'] = './avatar/';
$config['allowed_types'] = 'gif|jpeg|png|jpg';
$config['max_size'] = '1024mb';
$config['max_width'] = '3000';
$config['max_height'] = '3000';
$this->load->library('upload', $config);
if (!$this->upload->do_upload()) {
$ve['data'] = $this->upload->display_errors();
} else {
$this->upload->data();
}
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '100';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$this->load->library('upload', $config);
// Alternately you can set preferences by calling the initialize function. Useful if you auto-load the //class:
$this->upload->initialize($config);
if($this->upload->do_upload())
{
$data = array('upload_data' => $this->upload->data());
$this->load->view('upload_success',$data);
}else
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('file_view', $error);
}
I think you need to send in the user file/uploaded file while trying to validate it within the if condition.
if ( ! $this->upload->do_upload('userfile'))
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('upload_form', $error);
}
else
{
$this->upload->data();
}
I am uploading file in codeigniter using
$this->upload->do_upload('image')
but when file moves to particular path the file extension is changed (it's changed to lower case)
for example if I upload file "profile.JPG" it's changes to "profile.jpg"
please change CI system library
default in CI upload library $file_ext_tolower = FALSE.
.system\libraries\upload.php
public $file_ext_tolower = TRUE;
do_upload does this
$config['allowed_types'] = 'gif|jpg|png'
function do_upload()
{
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '100';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload())
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('upload_form', $error);
}
else
{
$data = array('upload_data' => $this->upload->data());
$this->load->view('upload_success', $data);
}
}
If you want to use same name / extension you send to save your file. You can use :
$upload_dir= $this->config->item("upload_dir");
$fileName = $_POST['sku_code'].".".$extension;
$filePath = $upload_dir.$fileName;
move_uploaded_file($_FILES["image-file"]["tmp_name"],$filePath );
Useful link :
https://ellislab.com/codeigniter/user-guide/libraries/file_uploading.html
We are trying to upload some files with Codeigniter, and for some reason this piece of code is not working.
Loading the library in "autoload" works perfectly, still, not the best solution, for the obvious reasons.
Curiously, is working in another website that I have.
<?php
// set options
$config['upload_path'] = FCPATH.$path;
$config['allowed_types'] = 'png|jpeg|jpg|gif';
$config['max_size'] = 2048;
$config['encrypt_name'] = TRUE;
$config['overwrite'] = TRUE;
$config['max_width'] = 0;
$config['max_height'] = 0;
// check if folder exists
if( ! is_dir($config['upload_path']))
#mkdir($config['upload_path'], 0755, true);
//load upload library
$this->load->library('upload', $config);
//check success of upload
if( ! $this->upload->do_upload($name))
return $this->upload->display_errors();
else
{
// do upload
}
?>
Here is the error:
A PHP Error was encountered
Severity: Notice
Message: Undefined property: xxx::$upload
Filename: core/xxx.php
Line Number: 424
Fatal error: Call to a member function do_upload() on null
First make sure $path is correct var dump($path) and check receiving it.
Second you did not have any {} in if part of do_upload and around the is directory
public function do_upload() {
$config['upload_path'] = FCPATH . 'uploads/';
$config['allowed_types'] = 'png|jpeg|jpg';
$config['max_size'] = 2048;
$config['encrypt_name'] = TRUE;
$config['max_size'] = '30000'; // Added Max Size
$config['overwrite'] = TRUE;
$config['max_width'] = 0;
$config['max_height'] = 0;
//load upload library
$this->load->library('upload', $config);
$this->upload->initialize($config);
// check if folder exists
if( ! is_dir($config['upload_path'])) {
#mkdir($config['upload_path'], 0755, true);
}
//check success of upload
$name = "userfile"; // Field name
if( ! $this->upload->do_upload($name)) {
$error = array('error' => $this->upload->display_errors());
$this->load->view('upload_form', $error);
} else {
$data = array('upload_data' => $this->upload->data());
$this->load->view('upload_success', $data);
}
}
Make sure on view use echo form_open_multipart('controller/do_upload')
http://www.codeigniter.com/userguide2/libraries/file_uploading.html
I just learn to save image data to database, those are filename and path. The path is appear on database, but not with filename. Whats the problem?
This is the controller,
function do_upload() {
$config['upload_path']='./uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '100';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$this->load->library('upload', $config);
$this->upload_model->upload($config);
if(!$this->upload->do_upload()){
$error = array('error' => $this->upload->display_errors());
$this->load->view('upload_form', $error);
} else {
$data = array('upload_data' => $this->upload->data());
$this->load->view('upload_success', $data);
}
}
and the model
function upload ($config) {
$config = $this->upload->data();
$upload_data = array(
'path' => $config['full_path'],
'nama_foto' => $config['file_name']
);
$this->db->insert('tb_picture', $upload_data);
}
and the table
what should i do?
thank you before.
before reading this please try it again yourself, or try any kind of viedotutorial that is on the web http://net.tutsplus.com/sessions/codeigniter-from-scratch/
controller function should look like this
function do_upload()
{
$config['upload_path']='./uploads/'; //needs to set uploads folder CHMOD 0644
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '100';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$config['overwrite'] = FALSE;
$config['remove_spaces'] = TRUE;
$field_name = "userfile"; //name tag in our HTML form in case you want to change it
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload($field_name)) //upload happens
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('upload_form', $error);
}
else
{
//succesful upload get data from upload and use them with our model
$upload_data = $this->upload->data();
$this->upload_model->upload($upload_data);
}
}
model function
function upload ($data) {
if (empty($data) || $data === FALSE) return FALSE;
$insert_data = array(
'path' => $data['full_path'],
'nama_foto' => $data['file_name']
);
$this->db->insert('tb_picture', $insert_data);
return $this->db->insert_id(); //returns last inserted ID
}
Please note that your model was totaly "wrong" you used upload function in it, try to pass data only to it so model can process it.