image file not showing in upload folder - php

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

Related

adding timestamp while uploading image file is not working

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
}

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;

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

Codeigniter - Need help uploading picture to both folder and database

I need help uploading the picture to the folder and database. It seems I can only send it to the database, but not upload it to the folder. Is there anything wrong with the code?
admin.php (controller)
public function input_siswa(){
$config['upload_path'] = './gambarfolder/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '100';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$this->load->library('upload');
$this->upload->initialize($config);
$location=base_url().'/gambarfolder/';
$pict=$location.$data_imge;
$nama_siswa = $this->input->post('nama_siswa');
$nis = $this->input->post('nis');
$id_jurusan = $this->input->post('id_jurusan');
$data = array(
'nama_siswa'=>$nama_siswa,
'nis'=>$nis,
'id_jurusan'=>$id_jurusan,
'gambar'=>$pict
);
$this->m_model->create('siswa',$data);
redirect(base_url('index.php/admin/tampil_siswa'));
}
Upload image in codeigniter using upload library.
public function input_siswa(){
$config['upload_path'] = './gambarfolder/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '100';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$location=base_url().'/gambarfolder/';
$pict=$location.$data_imge;
$this->load->library('upload', $config);
if ($this->upload->do_upload('userfile'))
{
$nama_siswa = $this->input->post('nama_siswa');
$nis = $this->input->post('nis');
$id_jurusan = $this->input->post('id_jurusan');
$data = array(
'nama_siswa'=>$nama_siswa,
'nis'=>$nis,
'id_jurusan'=>$id_jurusan,
'gambar'=>$pict
);
$this->m_model->create('siswa',$data);
redirect(base_url('index.php/admin/tampil_siswa'));
// redirect succsess page when image upload succsessfully.
}else{
$error = array('error' => $this->upload->display_errors());
$this->load->view('upload', $error);
// return image upload error.
}
}
first check your folder gambarfolder is in the root of your codigniter application
second check in your form tag you add enctype="multipart/form-data"
and add that lines for upload image to gambarfolder folder
$this->load->library('upload', $config);
if($this->upload->do_upload('userfile'))
{
}
where userfile is the name of file tag like
<input type="file" name="userfile" size="20" />
and $config is your config array
and also check https://www.codeigniter.com/userguide3/libraries/file_uploading.html
You need to add below line after initialise config
$this->upload->do_upload('userfile')

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