Codeigniter large video is uploading fine but audio is not uploading - php

In my controller I am trying to add audio and video, I changed my php.ini file and extended max_file_size, post_size etc.
When I upload a video of 25M, it is successfully getting upload
but when I try to upload an audio file of 4MB then it got failed.
Then again I tried to upload an audio file of 433KB, it got successfully upload.
HERE is my code below and I am not able to find what is the reason its not working
if(!empty($_FILES['audio_upload']['name'])){
$config['upload_path'] = 'uploads/';
$config['allowed_types'] = 'mp3|wmv|mp4';
$config['max_size'] = '0';
$config['file_name'] = $this->input->post('sub_category_name');
//Load upload library and initialize configuration
$this->load->library('upload',$config);
$this->upload->initialize($config);
if($this->upload->do_upload('audio_upload')){
$uploadData = $this->upload->data();
$audio_upload = $uploadData['file_name'];
}
else{
$audio_upload = '';
}
}
if( !empty($audio_upload)){
$media_link = 'uploads/'.$audio_upload;
}
else{
$media_link = $this->input->post('media_link');
}
//**************************************END
//**************************************Video Upload
if(!empty($_FILES['video_upload']['name'])){
$config['upload_path'] = 'uploads/';
$config['allowed_types'] = 'mp3|wmv|mp4';
$config['max_size'] = '0';
$config['file_name'] = $this->input->post('sub_category_name');
//Load upload library and initialize configuration
$this->load->library('upload',$config);
$this->upload->initialize($config);
if($this->upload->do_upload('video_upload')){
$uploadData = $this->upload->data();
$video_upload = $uploadData['file_name'];
}
else{
$video_upload = '';
}
}
if( !empty($video_upload)){
$video_link = 'uploads/'.$video_upload;
}
else{
$video_link = $this->input->post('media_link');
}

Related

Some images cannot be uploaded into codeigniter site

I am developing a website for my university project. I have to upload some images to the site. SO I used below mentioned code. Few images cannot be uploaded to the website. I don't know the reason. Please help me. Thank you
function upload_file($filename){
$config['file_name'] = time().$_FILES[$filename]['name']; // append time to filename
$config['upload_path'] = './assets/images/adsimages';
$config['allowed_types'] = 'gif|jpg|jpeg|png|GIF|JPG|PNG|JPEG';
$config['max_size'] = '2048000';
$this->load->library('upload', $config);
$this->upload->initialize($config);
$uploaded = $this->upload->do_upload($filename);
if ( ! $uploaded ){
$error = array('error' => $this->upload->display_errors());
$file = 'no_image.jpg'; // default file
}else{
$upload_data = $this->upload->data();
$file = $upload_data['file_name']; // uploaded file name
}
return $file; // return filename
}

How to encrypt image name and save into database?

I can save the image name into database without any problem but when I try to encrypt the name only the image path gets the encrypted name but the database not
public function fileUpload(){
$clientes = $this->pm->getAllProducts();
foreach ($clientes as $cliente) {
$teste = $cliente->id;
}
if(!empty($_FILES['file']['name'])){
// Set preference
$config['upload_path'] = "uploads/";
$config['allowed_types'] = 'jpg|jpeg|png|gif';
$config['max_size'] = '5024'; // max_size in kb
$config['encrypt_name'] = TRUE;
$config['file_name'] = $_FILES['file']['name'];
$this->upload->initialize($config);
//Load upload library
$this->load->library('upload',$config);
$data = array(
'pub_id' => $teste,
'url' => $_FILES['file']['name']
);
$this->pm->addProduct($data);
// File upload
if($this->upload->do_upload('file')){
// Get data about the file
$uploadData = $this->upload->data();
}
}
}
You can encrypt file name with config
$config['encrypt_name'] = TRUE;
Or
$file= uniqid().time().$_FILES["file"]['name'];
$config['file_name'] = $file;

Files not getting stored in correct folder

I have a form in CodeIgniter that allows the user to upload 2 separate files. At the backend I want these files to get stored in the different folder. In the controller i have written the upload code
public function upload()
{
/**Start uploading file**/
$config['upload_path'] = './assets/file/.';
$config['allowed_types'] = 'gif|jpg|png|doc|txt';
$config['max_size'] = 1024 * 8;
$config['encrypt_name'] = TRUE;
$this->load->library('upload', $config);
if (!$this->upload->do_upload('file'))
{
$error = array('error' => $this->upload->display_errors());
echo $error;
}
else
{
$data = $this->upload->data();
echo $file = $data['file_name']; //name of file
}
/**End uploading file**/
/**Start uploading img**/
$config2['upload_path'] = './assets/img/.';
$config2['allowed_types'] = 'gif|jpg|png|doc|txt';
$config2['max_size'] = 1024 * 8;
$config2['encrypt_name'] = TRUE;
$this->load->library('upload', $config2);
if (!$this->upload->do_upload('img'))
{
$error1 = array('error' => $this->upload->display_errors());
echo $error1;
}
else
{
$data1 = $this->upload->data();
echo $img = $data1['file_name']; //name of img
}
/**End uploading img**/
}
The images are getting uploaded but they are getting uplaoded to same folder. Can anyone please tell how i can make the files get saved in seperate folders
in the second load , you need to initialiaze the loaded library because the load method don't initialize it :
$this->upload->initialize($config2);

Codeigniter Upload Files/image only works when offline

I made a code to upload files on the web, when I was still using xampp, it always going well, but after I upload to hosting, file uploading not working..
here's my code :
MODEL (for updating database record)
function edit_logo_web()
{
$timestamp = date('YmdHi');
$img = $_FILES['userfile']['name'];
$logo = $timestamp."_".$img;
$data = array (
'logo' => $logo
);
$id = 1;
$this->db->where('site.id', $id);
if($this->upload->do_upload()){
$this->db->update('site', $data);}
else{
}
}
CONTROLLER (for uploading img/file to web directory)
function logo_web()
{
$timestamp = date('YmdHi');
$img = $_FILES['userfile']['name'];
$config['file_name'] = $timestamp."_".$img;
$config['upload_path'] ='asset/img/';
$config['allowed_types'] = 'gif|jpg|png|bmp|jpeg|png';
$config['max_size'] = '1024';
$config['max_width'] = '1600';
$config['max_height'] = '1200';
$config['remove_spaces'] = FALSE;
$this->load->library('upload', $config);
$data = array('upload_data' => $this->upload->data());
$this->m_cmm_bimbel->edit_logo_web();
echo "<script>
alert('Success !!');
window.location.href='logo_web';
</script>";
}
it Does very well in xampp localserver..
not in the hostingserver..
Any tips?

Codeigniter Upload multiple files different path to database

I have a problem with my multiple upload with codeigniter,
using image
another using pdf
When I uploaded the file uploaded twice and how to call different path to uploaded to database. this my code
Controller
public function upload(){
$catalog='catalog';
$userfile='userfile';
//for cover
$config['upload_path'] = './file/book/'; //Use relative or absolute path
$config['allowed_types'] = 'gif|jpg|png|';
$config['max_size'] = '100000';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$this->load->library('upload', $config);
$this->upload->initialize($config);
//$c=$this->upload->do_upload($userfile);
//for catalog
$config['upload_path'] = './file/book/pdf/'; //Use relative or absolute path
$config['allowed_types'] = 'pdf';
$config['max_size'] = '1000000';
$this->load->library('upload', $config);
$this->upload->initialize($config);
//$cat=$this->upload->do_upload($catalog);
if(!$this->upload->do_upload($catalog) && $this->upload->do_upload($userfile)){
$error = array('error' => $this->upload->display_errors());
$this->load->view('uploadds', $error);
}else{
$this->load->model("book_model");
$this->book_model->addnewbook();
redirect('book/book');
}
}
This model
function addnewbook(){
$fcat=array('upload_data' => $this->upload->data($userfile));
$fcatalog=array('upload_dataa' => $this->upload->data($catalog));
}
You need to handle multiple uploads independently. For this, you have to create separate custom objects for both uploads while loading the upload library. (See the code comments)
public function upload() {
// Cover upload
$config = array();
$config['upload_path'] = './file/book/';
$config['allowed_types'] = 'gif|jpg|png|';
$config['max_size'] = '100000';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$this->load->library('upload', $config, 'coverupload'); // Create custom object for cover upload
$this->coverupload->initialize($config);
$upload_cover = $this->coverupload->do_upload('cover');
// Catalog upload
$config = array();
$config['upload_path'] = './file/book/pdf/';
$config['allowed_types'] = 'pdf';
$config['max_size'] = '1000000';
$this->load->library('upload', $config, 'catalogupload'); // Create custom object for catalog upload
$this->catalogupload->initialize($config);
$upload_catalog = $this->catalogupload->do_upload('catalog');
// Check uploads success
if ($upload_cover && $upload_catalog) {
// Both Upload Success
// Data of your cover file
$cover_data = $this->coverupload->data();
print_r($cover_data);
// Data of your catalog file
$catlog_data = $this->catalogupload->data();
print_r($catlog_data);
} else {
// Error Occured in one of the uploads
echo 'Cover upload Error : ' . $this->coverupload->display_errors() . '<br/>';
echo 'Catlog upload Error : ' . $this->catalogupload->display_errors() . '<br/>';
}
}
Use the data on $cover_data['full_path'] and $catlog_data['full_path'] to update your database
$config['upload_path'] = 'frontend_assets/images/hospital';
$config['allowed_types'] = 'gif|jpg|png|jpeg|JPEG||JPG|PNG';
$this->load->library('upload', $config);
if($_FILES['logo']['name']){
$config['file_name'] = time() . $_FILES["logo"]['name'];
if (!$this->upload->do_upload('logo')) {
$this->upload->display_errors();
} else {
$upload = $this->upload->data();
$insert_data['logo'] = $config['upload_path'] . '/' . $upload['file_name'];
}
}
if($_FILES['bulding_photo']['name']){
$config['file_name'] = time() . $_FILES["bulding_photo"]['name'];
if (!$this->upload->do_upload('bulding_photo')) {
$this->upload->display_errors());
} else {
$upload = $this->upload->data();
$insert_data['bulding_photo'] = $config['upload_path'] . '/' . $upload['file_name'];
$this->image_size_fix($insert_data['bulding_photo'], $width = 200, $height = 200);
}
}

Categories