adding timestamp while uploading image file is not working - php

I am developing a ad site that anyone able to upload 3 images.
So I am coding to upload that 3 images by adding timestamp in front of their file name to make them unique. I use codeigniter framework and attaching the code below.
when I submitting form data, localhost server shows that images have been saved correctly with some code infornt of image filename. But problem is there are no images saved in relevant image folder in that name and I cannot retrieve that images in next preview advertisement page.
I don't know much about php or codeignitor. Your help is highly appreciated.
$config['upload_path'] = './assets/images/adsimages';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 5120;
$this->load->library('upload',$config);
$this->upload->initialize($config);
if (!$this->upload->do_upload()){
$errors = array('error' => $this->upload->display_errors());
$post_image = 'no_image.jpg';
}
else {
$data = array('upload_data' => $this->upload->data());
$post_image1 = time().$_FILES['userfile1']['name'];
$post_image2 = time().$_FILES['userfile2']['name'];
$post_image3 = time().$_FILES['userfile3']['name'];
}
$result = $this->post_model->adregister($post_image1,$post_image2,$post_image3);

You can append time to 'filename' in config of upload library
$config['file_name'] = time().$_FILES['userfile1']['name'];
Or if you want unique name for all files the simply add
$config['encrypt_name'] = TRUE;
then
$this->load->library('upload', $config);

Try this one:-
$path = pathinfo($_FILES["userfile1"]["name"]);
$image_path = $path['filename'].'_'.time().'.'.$path['extension'];

I've written a possible solution for your code. You haven't shared your full code so you'll have to fill in the gaps yourself and maybe make some changes here and there; Comments are mentioned wherever necessary. See if it helps you.
public function your_function_name(){
// your-code
// your-code
// check if file is uploaded in field1
if(!empty($_FILES['userfile1']['name'])){
// call function to upload file
$userfile1 = $this->upload_file('userfile1');
}
// check if file is uploaded in field2
if(!empty($_FILES['userfile2']['name'])){
$userfile2 = $this->upload_file('userfile2');
}
// check if file is uploaded in field3
if(!empty($_FILES['userfile3']['name'])){
$userfile3 = $this->upload_file('userfile3');
}
$result = $this->post_model->adregister($userfile1, $userfile2, $userfile3);
}
// function to upload file
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'] = 5120;
$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
}

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 do you upload image into database in Code igniter?

I've been trying to upload an image to my database (Note: I'm using Codeigniter 3) but every time I try to upload the image to the database it doesn't store it's filename and the image itself and only stores 1 bit in BLOB var type.
$photo = file_get_contents($_FILES['image']['tmp_name];
this->db->select('*')
[...]
if(empty($response){
$data['entry_phone_no'] = $this->input->post('phone_no');
$data['entry_email'] = $this->input->post('email');
$data['entry_firstname'] = $this->input->post('first_name');
$data['entry_lastname'] = $this->input->post('last_name');
$data['entry_photo'] = $photo;
$data['entry_photo_name'] = $photo;
In my model.php I have the setup above and it saves the other data (the phone num, email etc). I know I am missing some code at the "$photo" part but can anyone please point it out?
You can upload the image on folder called "Uploads" and save that filename into database as below:
Views.php:
<input class="my-set_3" type="file" name="chatbot_profile" id="chatbot_profile" required>
Controller.php:
if (!empty($_FILES['chatbot_profile']['name']))
{
$config['upload_path'] = './uploads';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 1000000;
$config['file_name'] = time();
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload('chatbot_profile'))
{
$error = array('error' => $this->upload->display_errors());
}
else
{
$data = array('upload_data' => $this->upload->data());
$file_name = $data['upload_data']['file_name'] ;
// Store this filename into database
// Your database code will be placed here
// OR call model mathod from here
}
}
Note: uploads folder will created by us and outside the application
folder.

image file not showing in upload folder

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 );

Database Reference Image Upload Wrong filename stored

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'];
}

Upload problem with PHP: Thumbnail picking up the image name

I have the following model(codeigniter) code to upload image and thumbnail.
However the result of image path becomes, images/comfort_big.jpg and images/comfort_big.jpg.jpg.
The thumbnail image picks up the name of image and add .jpg.
I am uploading images/confort_thumb.jpg for thumb.
Can anyone tell me what's wrong please?
if ($_FILES){
$config['upload_path'] = './images/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '200';
$config['remove_spaces'] = true;
$config['overwrite'] = false;
$config['max_width'] = '0';
$config['max_height'] = '0';
$this->load->library('upload', $config);
if (strlen($_FILES['image']['name'])){
if(!$this->upload->do_upload('image')){
$this->upload->display_errors();
exit();
}
$image = $this->upload->data();
if ($image['file_name']){
$data['image'] = "images/".$image['file_name'];
}
}
if (strlen($_FILES['thumbnail']['name'])){
if(!$this->upload->do_upload('thumbnail')){
$this->upload->display_errors();
exit();
}
$thumb = $this->upload->data();
if ($thumb['file_name']){
$data['thumbnail'] = "images/".$thumb['file_name'];
}
}
}
I had a quick look at File Uploading Class in the user guide
In the preferences tables it states:
Note:The filename should not include a file extension.
So my guess is that you get the user to name the file
$config['file_name'] = "User defined";
or remove the extension programmatically. Since you know and list the extensions already you could do
$types = array(".gif",".jpg",".png");
$image['file_name'] = str_replace($types , "", $image['file_name'] );
Your problem is that when you create a thumb you are passing the full image.
So it will add another extension to it.
Use Below code to remove extension from file name.
<?php
$filename=$image['file_name'];
$file_name_with_dot = substr($filename,0, strrpos($filename, '.') + 1);
$only_file_name = substr($file_name_with_dot,0,strlen($file_name_with_dot)-1);
?>
Now pass this $only_file_name variable to your thumb function..
It will be better to use automatic thumb creation from main image.
Hope this will help for you.

Categories