I got a weird problem when uploading images via "upload" library.
When i have huge pics, like 1,8mb big it always uploads them upside down.
Otherwise it gets uploaded normally.
my model for upload looks like this
function do_upload() {
$this->gallery_path = realpath(APPPATH . '../public/images');
$config = array(
'upload_path' => $this->gallery_path,
'allowed_types' => 'jpg|jpeg|gif|png',
'max_size' => 2000
);
$this->load->library('upload',$config);
$this->upload->do_upload();
$image_data = $this->upload->data();
//to db
$filename=$image_data['file_name'];
$this->db->set('name', $filename);
$this->db->insert('images');
}
could anyone please just explain why it gets like this? I've tried to increase max size to see if it would help but it didn't.
Would be more than thankful for some help :)
Try setting your config for upload:
$config['max_size'] = 0;
Related
I'm trying to create a thumbnail for the image that will be uploaded to my server. Refer to https://www.codeigniter.com/userguide3/libraries/image_lib.html#processing-an-image . After the creation, I want to save the thumbnail to another folder since both the original image and the thumbnail will be saved in the same location.
So, I try using move_uploaded_file() function but it unable to move the file. I'm not really sure if my code is correctly done. Check it out.
Belwo is the my code :
if($_FILES['file_name']['size'] != 0){
$config['upload_path'] = FCPATH.MEDIA_LIB_URI.'facts';
$config['allowed_types'] = 'jpg|jpeg|png|gif';
$config['max_size'] = 0;
$newFileName = removeExt($_FILES['file_name']['name']).'-'.uniqueId(); // renaming the file name without the ext
$new_name = $newFileName.getExt($_FILES['file_name']['name']); // new file name with the ext
$config['file_name'] = $new_name;
$this->load->library('upload',$config);
$this->upload->initialize($config);
if($this->upload->do_upload('file_name')){
$uploadData = $this->upload->data();
$file_name = $uploadData['file_name'];
$this->load->library('image_lib');
$configer = array(
'image_library' => 'gd2',
'source_image' => $uploadData['full_path'],
'create_thumb' => TRUE,
'maintain_ratio' => TRUE,
'width' => 950,
'height' => 950,
);
$this->image_lib->clear();
$this->image_lib->initialize($configer);
$this->image_lib->resize();
$thumbName = $newFileName.'_thumb'.getExt($_FILES['file_name']['name']); // getting the exact thumbnail name that been created by codeigniter
move_uploaded_file($thumbName, FCPATH.MEDIA_LIB_URI.'facts/thumbnail/'.$new_name);
return $file_name;
} else {
return $this->upload->display_errors();
}
}
Hope this will help you :
Use new_image in your $configer to change the folder path : $config['new_image'] = '/path/to/new_image.jpg';
Note :
If only the new image name is specified it will be placed in the same folder as the original
If only the path is specified, the new image will be placed in the destination with the same name as the original.
If both the path and image name are specified it will placed in its own destination and given the new name.
Your code should be like this :
if($this->upload->do_upload('file_name'))
{
$uploadData = $this->upload->data();
$file_name = $uploadData['file_name'];
$this->load->library('image_lib');
$configer = array(
'image_library' => 'gd2',
'source_image' => $uploadData['full_path'],
'create_thumb' => TRUE,
'maintain_ratio' => TRUE,
'width' => 950,
'height' => 950,
'new_image' => FCPATH.MEDIA_LIB_URI.'facts/thumbnail/'
);
$this->image_lib->clear();
$this->image_lib->initialize($configer);
$this->image_lib->resize();
return $file_name;
}
else
{
return $this->upload->display_errors();
}
For more : https://www.codeigniter.com/user_guide/libraries/image_lib.html#CI_Image_lib::resize
You found the right solution using rename or copy.
The reason it is not working with move_uploaded_file is because this function will only move the uploaded file which temporary name equals "tmp_name" in the $_FILES array.
The reason for this behavior is explained in the PHP manual: "This function checks to ensure that the file designated by filename is a valid upload file (meaning that it was uploaded via PHP's HTTP POST upload mechanism). If the file is valid, it will be moved to the filename given by destination.
This sort of check is especially important if there is any chance that anything done with uploaded files could reveal their contents to the user, or even to other users on the same system."
As per PHP manual for POST method uploads:
$_FILES['userfile']['name'] = The original name of the file on the client machine.
$_FILES['userfile']['tmp_name'] = The temporary filename of the file in which the uploaded file was stored on the server.
End quote
"tmp_name" is a unique server generated key so there is no filename duplication in the files system.
In your code, the $thumbnail string you built does not match "tmp_name" from the $_FILES array.
The reason you got no error message is because, again from PHP manual but for move_uploaded_file this time: "If filename is not a valid upload file, then no action will occur, and move_uploaded_file() will return FALSE."
Therefore, if you capture the result of move_uploaded_file in your code like $moveResult = move_uploaded_file(… you will end up with $moveResult === false. It is always a good idea to check the result of such an operation in order to react if something goes wrong.
I'm using CI latest version. Got error while uploading a JPG files. I use the code from here https://www.codeigniter.com/userguide3/libraries/file_uploading.html and a little bit from Multiple files upload in Codeigniter
The Controller:
$config = array(
'upload_path' => 'path to upload',
'allowed_types' => 'jpg|gif|png|jpeg',
'overwrite' => 0,
'max_size' => 1024,
);
$this->load->library('upload', $config);
$this->upload->initialize($config); // Make sure it has been initialized
if (!$this->upload->do_upload('gambar1')){
$error = array('error' => $this->upload->display_errors());
return $error;
}else{ echo 'success'; }
The View:
<?php echo form_open(base_url().'report/send', 'method="post" enctype="multipart/form-data"', $hidden);?>
<input type="file" class="hidden" name="gambar1"></form>
When I'm trying to upload JPG files, it gives me The filetype you are attempting to upload is not allowed.
Any idea?
This might help
'allowed_types' => 'jpg|gif|png|jpeg|JPG|PNG',
print errors and see what it returns after this
SOLUTION:
I've try to upload it to server. But it still gives me the same error. Then i found this: Codeigniter : The filetype you are attempting to upload is not allowed. Yesterday it was fine
"The most recent time I have had this is when I upgraded my core Codeiginter version from 2.x to 3.x. I updated the contents of the system directory, but not application/config. I didn't spot the fact that the application/config/mimes.php file is slightly different - the newer version returns an array whereas the old one included it as a var. The fix was to copy in the newer version of mimes.php"
I had the same problem and I figured out that the error came from the uploaded pictures.
If they were originally .png files and I changed them myself to .jpg, I would get this kind of error. The same happened when changing the mime type of any file.
What I did was put back the original mime type and it worked just fine.
i am faced same problem.......and find too much solution but no any solution worked for me........then i was try to use different method to upload and that is worked......CI Version is 3.1.10
here is your solution
$report_pdf = "";
if (!empty($_FILES) && isset($_FILES["report_pdf"])) {
$ext = pathinfo($_FILES['report_pdf']['name'], PATHINFO_EXTENSION);
$new_name = 'report_pdf_' . time() . '_' . $this->get_random_string(4) . '.' . $ext;
$file_url = "uploads/" . $new_name;
$config['file_name'] = $new_name . "." . $ext;
$config['upload_path'] = "uploads/";
$config['allowed_types'] = '*';
$this->load->library('upload', $config);
$_FILES['userfile2']['name'] = $new_name . "." . $ext; //$_FILES['songs']['name'][$i];
$_FILES['userfile2']['type'] = $_FILES['report_pdf']['type'];
$_FILES['userfile2']['tmp_name'] = $_FILES['report_pdf']['tmp_name'];
$_FILES['userfile2']['error'] = $_FILES['report_pdf']['error'];
$_FILES['userfile2']['size'] = $_FILES['report_pdf']['size'];
$this->upload->initialize($config);
if ($this->upload->do_upload('userfile2')) {
$finfo = $this->upload->data();
$report_pdf = $file_url;
} else {
$error = $this->upload->display_errors();
$result = array(
'msg' => strip_tags($error)
);
}
}
For PDF, CI3 mimes array don't have "application/octet-stream" in the config/mimes.php file. Add "application/octet-stream" in the pdf key. Works fine.
I am trying to upload a file in CodeIgniter when on live server. It is working perfectly on localhost but when I try to upload it on live server the upload library is not working, can you please tell me what I am doing wrong? Do I need to change something in php.ini of live server?
Here is my code:
public function do_upload()
{
$config = [
'upload_path' => './uploads',
'allowed_types' => 'jpg|gif|png|jpeg',
'max_size' => 1100000000,
'max_width' => 102400000,
'max_height' => 76800000,
];
// print_r($config);
// exit();
$upload= $this->load->library('upload');
$this->load->library('upload', $config);
$this->upload->initialize($config);
// var_dump($config);
var_dump($upload);
// exit();
if(!$this->upload->do_upload('imagefile'))
{
$upload_error = $this->upload->display_errors();
$this->load->view('site/timeline/timeline_profile',compact('upload_error'));
}
else
{
$post = $this->input->post();
unset($post['submit']);
$upload_data = $this->upload->data();
$file_name=$_FILES['imagefile'];
$this->load->model('Timeline_model');
$this->Timeline_model->upload_model($post,$file_name);
echo $image_path= base_url("uploads/".$upload_data['raw_name'].$upload_data['file_ext']);
}
}
on var dump the value it is showing me null.
Please follow code in File Uploading Class exactly and it will work and make sure you have write permissions to upload folder in your server.
I am encountering a problem with the Codeigniter File Upload class that whenever I try to upload a csv file which is greater than 10MB it says "You did not select a file to upload" but I set already the max size for 20MB and even changed the php.ini file for the upload_max_filesize, memory_limit and the post_max_size but the error is still the same..
Here's the code for the file upload
function upload(){
$config = array(
'upload_path' => './csv_uploads',
'allowed_types' => 'csv',
'max_size' => '20000' // 20MB
);
$filename = "csv_file";
$this->load->library('upload');
$this->upload->initialize($config);
if ( ! $this->upload->do_upload($filename))
{
$data = array('success' => false, 'error' => $this->upload->display_errors());
echo json_encode($data);
}
else
{
$filedata = $this->upload->data();
$data = array(
'success' => true,
);
echo json_encode($data);
}
}
Any help is much appreciated.. Thank you :-)
Thank you guys after searching for some answers.. I managed to do it.. I adjusted the execution time just like #DemoUser said and changed some configurations in php.ini file and restarted my apache and voila it's working now.
I followed this tutorial on how to upload a photo in a codeigniter folder:
http://codeigniter.com/user_guide/libraries/file_uploading.html
Can anybody tell me how can I store the path of the photo I just uploaded in my db?
Let's say have table called photo_paths
thanks in advance
This assumes you have an Images table.
Configure the file upload as follows:
$config['upload_path'] = './images/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '1000000';
$config['overwrite'] = TRUE;
$config['remove_spaces'] = TRUE;
$config['encrypt_name'] = FALSE;
$this->load->library('upload', $config);
Then on successful upload insert the file information into the database.
$insert_data = array(
'id_fk' => $this->input->post('page_id'),
'imgfilename' => $upload_info['file_name'],
'imgfilepath' => $upload_info['file_path']
);
$this->db->insert('images', $insert_data);
$upload info is retrived from the file_uploader class on successful upload using the following:
$upload_info = $this->upload->data();
If you want to see exactly what is returned:
echo var_dump($upload_info);
For me, since most of my uploaded images are about the same thing (for example, product images) I might just store the filename (e.g. productshot1.jpg) in the database, since I know where it is anyway (for example, I might keep all product images in "/images/products/"). That way if you need to change something later, it's not so difficult.