change permission folder Codeigniter - php

I want to change the permission for a folder (big_pics) to mode 777.
function do_upload(){
$config['upload_path'] = './big_pics/';
$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());
foreach($error as $key => $value){
echo $value."<br>";
}
//$this->load->view('upload_form', $error);
}else{
echo "Well DOne";
$data = array('upload_data' => $this->upload->data());
$this->load->view('upload_success', $data);
}
}
The folder is positioned on root folder.
I use codeigniter for this project

You can change the permission of your file or folder by using this before upload.
$frdata = array('upload_data' => $this->upload->data()); // get data
$frfile = $frdata ['upload_data']['full_path']; // get file path
chmod($frfile, 0777); // CHMOD file
if(!$this->upload->do_upload('file')) {
echo $this->upload->display_errors();
} else {
$file_name = $this->upload->data();
}

Related

How to upload multipl file fiedls in codeigniter

I have form with 4 file fields and I want to upload these files properly.
My code is as follows :
public 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('userfile')) {
$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);
}
}
private function fileUpload($name, $config = []){
$config['upload_path'] = './uploads/';
$config['encrypt_name'] = TRUE;
$config['file_ext_tolower'] = TRUE;
$config['allowed_types'] = 'jpg|jpeg|png';
$this->upload->initialize($config);
if ( ! $this->upload->do_upload($name))
{
$error = array('error' => true, 'message' => $this->upload->display_errors());
return $error;
}
else
{
return array('error' => false, 'upload_data' => $this->upload->data());
}
}
Create this private function in the controller just call this function when you have to upload the file with the name of the file field if you want some other config just pass config array to as a next parameter
$data = $this->fileUpload('site_logo_dark')
if($data['error']){
echo data['message']; // error message while uplaoding an file if have any
}else{
echo $data['upload_data']['file_name']; // return you a name of the file which you just upload it you can save it to the database
}

Codeigniter multiple upload with different area name and rename the file before uploaded

I have a problem with multiple files upload with different area name and want to change for every area filename before it's upload.
This is HTML form.
<input type="file" placeholder="" name="profilPic"/>
<input type="file" placeholder="" name="topPic"/>
This is controller
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 100;
$config['max_width'] = 1024;
$config['max_height'] = 768;
//$config['file_name'] = $this->session->sersession["id"];
$this->load->library('upload', $config);
$profilPic = $this->upload->do_upload('profilPic');
if (!$profilPic){
$error = array('error' => $this->upload->display_errors());
$this->session->set_flashdata("error", "profil pic was not uploaded= ");
}else{
$data = array('upload_data' => $this->upload->data());
$this->session->set_flashdata("success", "profil picture was uploaded.");
}
$topPic = $this->upload->do_upload('topPic');
if (!$topPic){
$error = array('error' => $this->upload->display_errors());
$this->session->set_flashdata("error", "top pic was not uploaded" );
}else{
$data = array('upload_data' => $this->upload->data());
$this->session->set_flashdata("success", "this picture was uploaded.");
}
Note: The pictures are upload to directory. But i want to rename every file file name before uploaded like "userID_profil.jpg" and "userID_top.jpg"
You can set the $config['file_name'] before the second file upload using
$this->upload->initialize($config);
Of course, you also need to set it for the first file either with $this->load->library('upload', $config) or $this->upload->initialize($config).
Docs: https://www.codeigniter.com/userguide3/libraries/file_uploading.html#setting-preferences
I solved it.
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 100;
$config['max_width'] = 1024;
$config['max_height'] = 768;
if($_FILES["profilPic"]["name"]){
$config["file_name"] = $this->session->usersession["id"]."_profil.jpg";
$this->load->library('upload', $config);
$profilPic = $this->upload->do_upload('profilPic');
if (!$profilPic){
$error = array('error' => $this->upload->display_errors());
$this->session->set_flashdata("error", ".");
}else{
$profilPic = $this->upload->data("file_name");
$data = array('upload_data' => $this->upload->data());
$this->session->set_flashdata("success", ".");
}
}
if($_FILES["topPic"]["name"]){
$config["file_name"] = $this->session->usersession["id"]."_top.jpg";
if($_FILES["profilPic"]["name"]){
$this->upload->initialize($config);
}else{
$this->loadl->library('upload', $config);
}
$topPic = $this->upload->do_upload('topPic');
if (!$topPic){
$error = array('error' => $this->upload->display_errors());
$this->session->set_flashdata("error", "" );
}else{
$topPic = $this->upload->data("file_name");
$data = array('upload_data' => $this->upload->data());
$this->session->set_flashdata("success", ".");
}
}

Codeigniter Upload Image Choice

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".

Codeigniter : Filename didn't appear on database

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.

File permission with upload class of CodeIgniter

Hi I need to put a permission 777 to my uploaded files but I dont find any docs for uploaded files in codeigniter... Is it possible to put permission 777 with the upload class of codeigniter ??
$group_id = $this->input->post('group_id', TRUE);
// unlink('static/images/uploads/44');
// rmdir('static/images/uploads/45');
$dir = is_dir('../www/static/images/uploads/'.$group_id);
if($dir){
$config['upload_path'] = '../www/static/images/uploads/'.$group_id;
echo "test";
}
else{
mkdir('../www/static/images/uploads/'.$group_id, 0777);
$config['upload_path'] = '../www/static/images/uploads/'.$group_id;
}
$config['allowed_types'] = 'docx|pdf|doc|gif|jpg|png|tiff';
$config['max_size'] = '10000000';
$config['max_width'] = '999999';
$config['max_height'] = '99999';
$this->load->model('Teacher_model');
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload())
{
$error = array('error' => $this->upload->display_errors());
print_r($error);
//$this->load->view('school/teacher/upload_form', $error);
}
else
{
$data = array('upload_data' => $this->upload->data());
$name = $this->input->post('name', TRUE);
$path = $data['upload_data']['file_name'];
$group_id = $this->input->post('group_id', TRUE);
$this->Teacher_model->add_ressources($group_id,$name,$path);
redirect('school/teachers/#tabs_group_ressource'.$group_id, 'location', 301);
}
You can use chmod to change the permissions. Something like this may work.
After the do_upload you could try adding the following line
if(is_file($config['upload_path']))
{
chmod($config['upload_path'], 777); ## this should change the permissions
}
You can use chmod along with umask.
$old = umask(0);
$logofilepath = $config['upload_path'].$filename;
if(is_file($logofilepath))
{
chmod($logofilepath, 0777);
}
umask($old);

Categories