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.
Related
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 save the thumbnail image to the directory, but it is not working. I am able to save the original image and save info to database, but for some reason the thumbnail is not saving to the thumbs directory I create. The directory has read/write access. I will also note I am using Dropzone.js.
Here is my partial code:
if ( ! $this->upload->do_upload('file'))
{
//upload failed, echo back negative response to dropzone.js
}
else
{
//upload success, upload file(s)
$image_data = $this->upload->data();
$img_config['source_image'] = '/uploads/videos/'.$image_data['file_name'];
$img_config['new_image'] = '/uploads/videos/thumbs/'.$image_data['file_name'];
$img_config['create_thumb'] = TRUE;
$img_config['maintain_ratio'] = TRUE;
$img_config['width'] = 251;
$img_config['height'] = 180;
$this->load->library('image_lib', $img_config);
$this->image_lib->resize();
$file = array(
'user_id' => $this->my_auth->logged_in(),
'img_name' => $image_data['raw_name'],
'thumb_name' => $image_data['raw_name'].'_thumb',
'ext' => $image_data['file_ext'],
'upload_date' => time()
);
$this->upload_model->add_image($file);
$data['img_success'] = '<div class="alert alert-success" id="imgSuccess">Your image <strong>' . $image_data['file_name'] . '</strong> was successfully uploaded!</div>';
$this->load->view('templates/frontend/front_header', $data);
$this->load->view('templates/frontend/front_navbar');
$this->load->view('frontend/upload_images', $data);
$this->load->view('templates/frontend/front_footer', $data);
}
I am able to successfully save the original image, just not the thumbnail. Can anyone see anything wrong here?
I added this to my __construct:
$this->original_path = realpath(APPPATH.'../uploads/images');
$this->thumbs_path = realpath(APPPATH.'../uploads/images/thumbs');
and changed the source_image and new_image configs:
$img_config['source_image'] = $image_data['full_path'];
$img_config['new_image'] = $this->thumbs_path;
This seems to have fixed it.
I am trying to upload file with type svg in Codeigniter. But i could not do it with codeigniters built in upload library. I have tried to do it by adding this line in congig > mimes.php file
'svgz' => 'image/svg+xml',
'svg' => 'image/svg+xml',
but still no solution. I am getting this error -
The filetype you are attempting to upload is not allowed.
here is my code
$dir = 'uploads/svg/';
$this->load->library('upload');
$config['file_name'] = $result . '.svg';
$config['upload_path'] = $dir;
$config['allowed_types'] = 'image/svg+xml';
$this->load->library('upload');
$this->upload->initialize($config);
$this->upload->do_upload();
Can anyone help me please?
thanks in advance
First of all, check the mime type of your image here
Then,
Open mimes.php from the config folder
then find the array element of index same as your image extension from the array,
add the mime type of your image to the array
In my case, I added 'image/svg':
'svg' => array('image/svg+xml', 'application/xml', 'text/xml', 'image/svg')
You need to put a list of allowed file extensions in $config['allowed_types'], separated by |, not MIME type. Try this -
$config['allowed_types'] = 'svg';
$config['allowed_types'] = 'image/svg+xml';
This should be:
$config['allowed_types'] = 'svg';
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;
I am using the following code to upload and extract a ZIP-archive in PHP using CodeIgniter.
$config['upload_path'] = 'backups/';
$config['allowed_types'] = 'zip';
$config['file_name'] = $project_id;
$this->load->library('upload', $config);
if ( file_exists('backups/' . $project_id . '.zip') ) {
try {
delete_file('backups/' . $project_id . '.zip');
} catch (exception $e) {
// do nothing...
}
}
if ( $this->upload->do_upload('zip_archive') ) {
$upload_data = $this->upload->data();
$file_name = 'backups/'.$upload_data['file_name'];
$this->sync_model->extract_zip_archive($project_id, $file_name, TRUE);
echo "Success";
} else {
echo $this->upload->display_errors('<p>','</p>');
}
I get the error though "The filetype you are attempting to upload is not allowed." even though it is a valid .zip file I am uploading. (It's size does not exceed the maximum allowed)
This problem also occurs with every filetype I've tried that's not an image.
Any pointers?
Cheers!
$config['allowed_type']
Should be a list of mime types, but it just so happens that sometimes just the file extension will work (this is the case with images at least).
Try:
$config['allowed_type'] = 'application/zip';
Although these are also mime types for zip:
application/x-zip
application/x-zip-compressed
application/octet-stream
application/x-compress
application/x-compressed
multipart/x-zip
I solved it using the code provided by "kofic" here: http://ellislab.com/forums/viewthread/113029/
You can keep your 'allowed_types' as 'zip', but making sure that you extend the mime types that are considered 'zip'.
I changed line 54 of my 'application/config/mimes.php' to:
'zip' => array(
'application/x-zip',
'application/zip',
'application/x-zip-compressed',
'application/octet-stream',
'application/x-compress',
'application/x-compressed',
'multipart/x-zip'),
(Credit to Matthew Rapati for the list of MIME types)
The truth is that once you allow 'application/octet-stream', the uploaded file could be pretty much anything so part of the purpose of restricting the allowed types vanishes. But since CI forces you to set it to something, I think it is still better than:
$config['allowed_types'] = '*';
Which also works.