i have spend days trying to make image file i uploaded shows in its folder but when i upload an image file it goes to database but not displaying in folder i dont know why.
public function additems()
{
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 106660;
$config['max_width'] = 1026664;
$config['max_height'] = 76668;
$this->load->library('upload', $config);
$this->load->view('templates/dashboard_head.php');
$this->load->view('admin/create_product');
$this->load->view('templates/admin_sidebar');
$this->load->view('templates/admin_footer');
$this->load->library('upload', $config);
if($this->input->post('submit'))
{
//get form's data and store in local varable
$name=$this->input->post('name');
$price=$this->input->post('price');
$description=$this->input->post('discription');
$code = $this->input->post('code');
$data = array('upload_data' => $this->upload->data());
$image = $_FILES['image']['name'];
//call saverecords method of Hello_Model and pass variables as parameter
$this->admin->insert_products($name, $price, $description,$code,$image);
echo "<script>
window.alert('added succesfully');
</script>";
}
The image gets assigned a temporary name on upload. You have to move it using something like this:
$uploadDirectory = './uploads';
move_uploaded_file( $_FILES['image']['tmp_name'], $uploadDirectory );
I am teaching myself codeigniter so I used the image uploading class with storing the name in SQL and storing the image on a folder in the server. When you call the name from the server it will display from the file.
So that works fine, but I was curious on overwriting images so I checked the codeignter documentation on uploading files and they have a encrypt_file file function. So that would fix a overwrite issue, well now since I used it the file name that is stored in my database is the original file while, the image that is stored in my folder on the server has a encrypted file name, so there for the photo does no display.
I have went through codeigniter's documentation, trying to load another filename variable and so on. I tried also initializing the config each time also.
The controller the Library Upload is auto.
if($this->form_validation->run()=== FALSE) {
$this->load->view('templates/header');
$this->load->view('posts/create', $data);
$this->load->view('templates/footer');
}
else{
//Upload image
$config['upload_path'] = 'assets/images/posts';
$config['allowed_types'] = 'gif|jpg|png';
$config['encrypt_name'] = TRUE; //TURN ON
$config['max_size'] = 0;
$config['max_width'] = 0;
$config['max_height'] = 0;
$this->upload->initialize($config);
if(!$this->upload->do_upload('userfile')){
$errors = array('error'=>$this->upload->display_errors());
$post_image = 'noimage.jpg';
}else {
$data = array('upload_data' =>$this->upload->data());
$post_image = $_FILES['userfile']['name'];
}
$this->Post_Model->create_post($post_image);
redirect('http://localhost/CI/');
}
}
MODEL
public function create_post($post_image){
$slug = url_title($this->input->post('title'));
$data=array('title' => $this->input->post('title'),
'slug' => $slug,
'body' => $this->input->post('body'),
'category_id' => $this->input->post('category_id'),
'post_image' =>$post_image
);
form
<img src="assets/images/posts/<?php echo $post['post_image'];?>">
First, Add the following configuration line of code to avoide overwrite $config['overwrite'] = false
Second, Give actual values to max_size, max_width and max_height not zero
$config['max_size'] = 1000;
$config['max_width'] = 430;
$config['max_height'] = 430;
$config['overwrite'] = false;
And where the file is uploading do some changes
if(!$this->upload->do_upload('userfile')){
$errors = array('error'=>$this->upload->display_errors());
$post_image = 'noimage.jpg';
}else {
$data = $this->upload->data();
$post_image = $data['file_name'];
}
I want to replace/overwrite my image upload, which works. But the file have copied end up with another extension.
This is my controller
function add(){
if (isset($_POST['submit'])) {
$id = $this->input->post('nisn', TRUE);
$config['upload_path'] = './upload/';
$config['allowed_types'] = 'jpg|jpeg|png';
$config['max_size'] = 1024;
$config['file_name'] = $id;
$config['overwrite'] = TRUE;
$this->load->library('upload', $
$this->upload->do_upload('userfile');
$upload = $this->upload->data();
print_r($upload);
die;
}
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');
}
I'm trying to add time as the prefix of the image name along with the original name when uploading, But I couldn't figure it out. Please help me with the following code to add a prefix to my original file name when uploading.
<?php
class Upload extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->helper(array('form', 'url'));
}
function index()
{
$this->load->view('upload_form', array('error' => ' ' ));
}
function do_upload()
{
$config['upload_path'] = 'Public/uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '1024';
$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 can encrypt file name with use of CI native option:
$config['encrypt_name'] = TRUE;
OR
You can do it with your own code:
$new_name = time().$_FILES["userfiles"]['name'];
$config['file_name'] = $new_name;
For some reasons, consecutive calls to the do_upload function doesn't work. It sticks to the first filename set by the first function call
$small_photo_url = $this->upload_photo('picture_small', $this->next_id.'_small ');
$medium_photo_url = $this->upload_photo('picture_medium', $this->next_id.'_medium');
$large_photo_url = $this->upload_photo('picture_large', $this->next_id.'_large ');
The filenames will all be "00001_small", "00001_small1", "00001_small2"
given the following configurations
function upload_photo($field_name, $filename)
{
$config['upload_path'] = 'Public/uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '1024';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$config['file_name'] = $filename;
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload())...
I think it's because this line doesn't work the second time you call it. It does not set the configurations again
$this->load->library('upload', $config);
==========================================================================
Solution to the problem faced during consecutive do_upload function calls:
// re-initialize upload library
$this->upload->initialize($config);
/* Conf */
$new_name = time().$_FILES["userfiles"]['name'];
$config['file_name'] = $new_name;
$config['upload_path'] = './upload/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '';
$config['max_width'] = '';
$config['max_height'] = '';
$config['file_ext_tolower'] = TRUE;
/* Load the upload library */
$this->load->library('upload', $config);
For what you are exactly looking for, this worked for me:
$path = $_FILES['image']['name'];
$newName = "<Whatever name>".".".pathinfo($path, PATHINFO_EXTENSION);
when you use do_upload() function its rename file name if already exists and upload file
System->libraries->Upload.php
default function of do_upload() return true or false
replace
return $this->upload_path.$this->file_name;
and in another file
<input type='file' name='userfile'/>
<?php
$upload_file_name = $this->upload->do_upload('userfile');
$finalname;
if (!$upload_file_name) {
$finalname = 'default.png';
} else {
$split_data_upload_file_name = explode("/", $upload_file_name);
foreach ($split_data_upload_file_name as $i => $value) {
$finalname = $value;
}
}
?>
$config['file_name'] = $new_name;
Just add it align with the config codes.
This should be after the upload.
Process the filename you want here:
$a=$this->upload->data();
rename($a['full_path'],$a['file_path'].'file_name_you_want');
Try This:
$config['file_name'] = "yourCustomFileName";
If your $config is with this code: $config['encrypt_name'] = TRUE;
the name of your file will be forced to rename with a encrypt name, just remove the code.
Solution 1: Upload file with new customized and specific name (Note: this answer is applicable when you are submitting a unique name of a record, for example student name is a unique value in a record then you will use $_POST['student_name'])
$config['file_name'] = str_replace(' ', '_', $_POST['anyUniqueValue']);
//any Other unique value that you are using to submit with this file
$config['upload_path'] = './folderName/';
$config['encrypt_name'] = FALSE;
Solution 2: Upload file with new unique name
$config['file_name'] = time();
$config['upload_path'] = './folderName/';
$config['encrypt_name'] = FALSE;
Solution 3: Upload file with new unique name using codeigniter's default option
$config['encrypt_name'] = TRUE;
if you do multiple file upload, maybe you can try update library Upload.php
on system/libraries/Upload.php. On function do_upload()
$this->file_name = $this->_prep_filename($_file['name']);
to
$this->file_name = time().'_'.$this->_prep_filename($_file['name']);