Marsha Marsha - 1 month ago 10
PHP Question

i am displaying pdf/doc/ppt icons in view codeigniter but not working properly

my view upload_success.php



<html>
<head>
<title>Upload Form</title>
<style type="text/css">
#gallery, #upload {
border: 1px solid #ccc;
margin: 10px auto;
width: 570px;
padding: 20px;
}
#blank_gallery{
font-family: Arial;
font-size: 18px;
font-weight: bold;
text-align: center;
}
.thumb{
float: left;
width: 150px;
height: 100px;
margin: 10px;
padding: 10px;
background-color: #ddd;

}
.thumb:hover{
outline: 1px solid #999;
}
img{
border: 0;
}
#gallery:after{
content: ".";
visibility: hidden;
display: block;
clear: both;
height: 0;
font-size: 0;
}
</style>
</head>
<body>
<div id="gallery">
<?php if(isset($images ) && count($images )):

foreach ($images as $image): ?>
<div class = "thumb">
<a href="<?php echo $image['full_path']; ?>">
<img src="<?php echo $icon; ?>" width="150px" height="100px"/>
</a>
</div>
<?php endforeach; else: ?>
<div id ="blank_gallery"><?php echo @$error;?></div>
<?php endif; ?>
</div>
<div id="upload">
<?php
echo form_open_multipart('upload/do_upload');
echo form_upload('userfile');
echo form_submit('upload', 'upload');
echo form_close()
?>
</div>
</body>
</html>


controller upload.php



<?php

class Upload extends CI_Controller {
var $icon ;

public function __construct()
{
parent::__construct();
$this->load->helper(array('form', 'url'));
}

public function index()
{
$this->load->view('upload_success', array('error' => ' ' ));
}

public function do_upload()
{
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'doc|docx|pdf|ppt|pptx';
$config['max_size'] = 0;
$config['max_width'] = 3024;
$config['max_height'] = 1768;

$this->load->library('upload', $config);

if ( ! $this->upload->do_upload('userfile'))
{
$error = array('error' => $this->upload->display_errors());

$this->load->view('upload_success', $error);
}
else {
$this->load->library('upload', $config);
$data = $this->upload->data();
$filename = $this->upload->data('file_name');
$filesize = $this->upload->data('file_size');
$filetype = $this->upload->data('file_ext');
// print_r($data);


switch ($filetype) {
case ".docx":
$icon = "\docss\images\word.png";
break;
case ".pdf":
$icon = "\docss\images\pdf.jpg";
break;
case ".pptx":
$icon = "\docss\images\ppt.png";
break;
default:
echo "sorry there was an error uploading your file!!!";
}


$confyg = array(
'source_image' => $data ['full_path'],
'new_image' => './uploads/',
'maintain_ration' => true,
'width' => 150,
'height' => 100
);

$this->load->model('upload_model');
$data['images'] = $this->upload_model->getaimage();
$data1 = array('icon' => $icon);
$this->load->library('image_lib', $confyg);
$this->image_lib->resize();
$this->load->view('upload_success', $data1);
$this->load->view('upload_success', $data);

}
}
}

?>


model upload_model.php



<?php
class Upload_model extends CI_Model {
function getaimage(){
$files = scandir('./uploads/');
$files = array_diff($files, array('.', '..' ));
$images = array ();
foreach ($files as $file){
$images [] = array(
'file_path' => base_url().'uploads/'.$file,
'full_path' => base_url().'uploads/'.$file
);
}
return $images;
}

}


Am able to display the correct icon for each extension but the problems comes when i upload a file for example a ppt file every other files in my display (div) takes that icon and i have pdf/doc/ppt files in my directory. I want every file to be displayed according to its icon. the switch condition is working properly since when i choose a doc/pdf/ppt file it displaya the icon but all the other files take that icon.
where am i going wrong?

Thanks in advance for your assistance.

Answer

After so many days i found a solution to my problem, i only had to insert the appropriate file icons path in database using the switch statement and then get the icon path from the database.

*in the controller *upload.php

    <?php

class Upload extends CI_Controller
{

    public function __construct()
    {
        parent::__construct();
        $this->load->helper(array('form', 'url'));
    }

    public function index()
    {
        $this->load->view('upload_success', array('error' => ' '));
    }

    public function do_upload()
    {

        $config['upload_path'] = './uploads/';
        $config['allowed_types'] = 'doc|docx|ppt|pptx|pdf|xls|xlsx';
        $config['max_size'] = 0;
        $config['max_width'] = 3024;
        $config['max_height'] = 1768;

        $this->load->library('upload', $config);

        if (!$this->upload->do_upload('userfile')) {
            $error = array('error' => $this->upload->display_errors());

            $this->load->view('upload_success', $error);
        } else {


            $data = array();

            $this->load->library('upload', $config);

            $fileData = $this->upload->data();
            $ext = $this->upload->data('file_ext');
            switch ($ext) {
                case ".docx":
                    $icon = "\docss\images\word.png";
                    break;
                case ".pdf":
                    $icon = "\docss\images\pdf.jpg";
                    break;
                case ".pptx":
                    $icon = "\docss\images\ppt.png";
                    break;
                default:
                    echo "sorry there was an error uploading your file!!!";
            }
            $uploadData['full_path'] = $fileData['full_path'];
            $uploadData['file_name'] = $fileData['file_name'];
            $uploadData['created'] = date("Y-m-d H:i:s");
            $uploadData['modified'] = date("Y-m-d H:i:s");
            $uploadData['file_icon'] = $icon; //the path of the approriate file icon to insert in db

        }



        if(!empty($uploadData)){
            //Insert file information into the database
           $this->db->insert('files',$uploadData);

            $confyg = array(
                'source_image' => $fileData ['full_path'],
                'new_image' => './uploads/thumbs/',
                'maintain_ration' => true,
                'width' => 150,
                'height' => 100
            );

            $this->load->library('image_lib', $confyg);
            $this->image_lib->resize();

            $this->load->model('upload_model');
            $data['icon'] = $this->upload_model->getaimage();

            $this->load->view('upload_success', $data);


        }
    }
}
?>

after that the code is working as i wished. hope it will assist another person wanting to upload and display appropriate file icons.