CodeIgniter mp4 file upload is not working - php

i am trying to upload a mp4 file with CodeIgniter but it is not working.
it shows me an error message.
you didn't select a file to upload
and the same code is working fine with .png type images.
i changed my php.ini max upload size and post data size as per my requirement.I also added mime type for mp4 into mime.php file of the CodeIgniter:
'mp4' => 'video/mp4',
i also tried
'mpeg4' => 'video/mpeg4',
here is my controller code
function do_upload()
{
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'mp4|flv|png';
$config['max_size'] = '500000';
$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);
}
}
so please suggest some solution for this issue.
-Thanks

There are 2 possibilities:
Do you name input field like this?
<input type="file" name="userfile" />
If not, you have to specify in.
$this->upload->do_upload()
the name of file input.
2.Is your form like this?
<form name="myForm" method="post" action="controller/function" enctype="multipart/form-data">
You can read more here http://codeigniter.com/user_guide/libraries/file_uploading.html

i got the solution the code is correct and working fine.
this the issue with particular mp4 file.
i need to check the mime type of that file.

Related

CodeIgniter upload file

I have written code for uploading a file in CodeIgniter.
Here is the view file code
<form method='post' type="get" enctype="multipart/form-data" action="" class="apply_form" id="apply_form">
<input type="file" name="submit_resume" id="submit_resume" class="submit_resume" value="browse"/>
Here is the controller file code
$this->load->helper('url');
$this->load->helper('html');
$this->load->helper('form');
$this->load->library('upload');
$config['upload_path'] = $this->base_url.'/uploads/';
$config['allowed_types'] = 'docx|doc|pdf';
$config['max_size'] = '1000';
//$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);
}
But nothing is uploading in the uploads folder.
Cant figure out what the issue really is!
Some changes I would do:
$config['upload_path'] = './uploads/';
Mention only the relative path and not absolute path.
if ( ! $this->upload->do_upload('submit_resume')) //here see the file name
{
$error = array('error' => $this->upload->display_errors());
print_r( $error );die;
}
Just to view the error you are getting.
You must have the correct permission set in your uploads folder too.
The permission of the folder in which the file is getting uploaded might be the issue.
$config['upload_path'] = './uploads/';
For changing the permission of above folder, use following command in your terminal -
chmod -R 750 /uploads/
Explanation:
chmod 750 changes to permission to read and write for the user.
-R makes is recursive.

Codeigniter server root

I am trying to upload video in server using Codeigniter, In the localhost i can do it, but when in the server it does not work. Even though do_upload() function returns true.
When i'm testing the server root, print_r($_SERVER['DOCUMENT_ROOT']); it returns
/var/www/mjpp/data/www/supps.mydomainname.com
Also the
$updata = $this->upload->data(); returns
[file_path] => /var/www/mjpp/data/www/supps.mydomainname.com/public/video
Can you tell me why /var/www/mjpp/data/ is appearing before my main domain name , and is it the cause of any error for uploading video?
Thank you in advance, sorry for my bad english.
/var/www/mjpp/data/www/supps.mydomainname.com is absolute location of your document root in a system in general. However you do not usually have access to anything above your user dir.
public/video or /var/www/mjpp/data/www/supps.mydomainname.com/public/video (as absolute path) is what you pass to Upload library as upload_path. This is where the library will try yo upload the files. This folder should exist and be writeable (have 0777 permissions on it for example).
No need to use Document Root, Codeigniter itself having configurations when file uploading.
Here is sample code for image uploading,
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);
}
}
You just need to change the configuration($config array), for your issue you should use the $config['upload_path'].
Follow codigniter file upload tutorial
Dont use $_SERVER['DOCUMENT_ROOT'] in CI, use URL Helper
$this->load->helper('url');
echo base_url();

Image Type Validation in codeigniter

I am new to Codeigniter. Working on an form validation having image in the form. Using Module MVC extension in CodeIgniter.
//for upooading image I found this file upload snippet
$config['upload_path'] = './uploads/audio_files/';
$config['allowed_types'] = 'gif|jpg|jpeg|png';
$config['max_size'] = '2048';
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload($file_name))
{
$error = array('error' => $this->upload->display_errors());
$file1 = '';
}
else
{
$upload_data = $this->upload->data();
$file1 = $upload_data['file_name'];
}
This is working fine. File which is not jpeg | gif is not uploaded. but user dont get any error message. I want the user to show that particular error when other image type is uploaded. Please help
I tried this but failed.
try 1 : $this->template->load_partial('admin/admin_master', 'admin/form', $error);
try 2. : loading view with $error
Thanks
Can't you associate a view with an error message? For instance:
After
$file1 = '';
You put:
$this->load->view('error_view', $error, $file1);
You would have to make a separate view page called 'error_view.php'.

Codeigniter File uplaod

Hi I am trying to use the codigniter do_upload function below:
But get this error: "You did not select a file to upload." I do select a file and when i pass the name i can see the name but still can not upload...thanks in advance for the help.
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())
{
$data = array('error' => $this->upload->display_errors());
print_r($data);
exit;
//$this->load->view('clients_page', $error);
}
else
{
$data = array('upload_data' => $this->upload->data());
print_r($data);
exit;
}
}
I assume you're not naming your <input type="file" /> the default userfile. It would be nice to see your upload view also.
change this
$this->upload->do_upload()
to this
$this->upload->do_upload('audioRef')
I do select a file and when i pass the name i can see the name but still can not upload
This is often because you've omitted to set your form to submit as multipart/form-data. Forms default to application/x-www-form-urlencoded, which can't contain file uploads; in this case you just get the filename instead.
<form method="post" action="..." enctype="multipart/form-data">
Incidentally,
$config['allowed_types'] = 'gif|jpg|png';
if you must use filename extension to check file type (and it's inadvisable, but that's what CodeIgniter offers, unfortunately), please also allow jpeg and any other likely extensions.
$config['upload_path'] = './uploads/';
If that uploads folder is within the webroot (you'll be serving files directly out of it), that's potentially a cross-site-scripting vulnerability. Unfortunately it is possible to upload files that look like images but in fact contain HTML, including <script>.
Doing user-uploaded-content in a safe way is hard. See eg this question for background.

Can't upload Image CodeIgniter, filetype error

I have write below code :
function upld_logo()
{
$config['upload_path'] = './images/';
$config['allowed_types'] = 'gif|jpg|png';
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload())
{
$error = array('error' => $this->upload->display_errors());
$data['msg']='Sorry! Cant Upload' . $error['error'];
$data['main_content']='message';
$this->load->view('template',$data);
}
else
{
//$data = array('upload_data' => $this->upload->data());
$names = $this->upload->data();
$k=$names['file_name'];
$data['msg']='Your Logo Successfully Uploaded. ';
$data['main_content']='message';
$this->load->view('template',$data);
}
}
When I upload Image file with name logo.gif it show error message :
The filetype you are attempting to upload is not allowed.
Find this code in your system->libraries->Upload.php line 200
$this->file_type = preg_replace("/^(.+?);.*$/", "\\1", $this->file_type);
and replace with this:
$this->file_type = preg_replace("/^(.+?);.*$/", "\\1", $_FILES[$field]['type']);
Yes I also got the same error.
And if u change the $[allowed_types] = "*" (allowed all files)
you will find that upload is success but your image file don't have a type!
I think there's something wrong with the CI library maybe?
I have spent some time with the same issue. You might find codeigniters default upload library is using a couple of deprecated PHP functions to find the files mime type.... Good luck, I'm still trying to patch it myself .

Categories