Welcome Guest, Not a member yet? Register   Sign In
upload form
#1

[eluser]Captain_Fluffy_Pants[/eluser]
trying to create a upload form that displays thumbnails of the images that has been uploaded
upload form
Code:
<html>
<head>
<title>Upload Form</title>
</head>
<body>
<form method="post" action="$_POST" enctype="multipart/form-data" />

<?php echo $error;?>

<?php echo form_open_multipart('upload/do_upload');?>

<input type="file" name="userfile" size="20" />

<br /><br />

&lt;input type="submit" value="upload" /&gt;

&lt;/form&gt;

&lt;/body&gt;
&lt;/html&gt;
upload success
Code:
&lt;?php echo $results;?&gt;
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Upload Form&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;

<h3>Your file was successfully uploaded!</h3>

<ul>
&lt;?php foreach ($upload_data as $item => $value):?&gt;
<li>&lt;?php echo $item;?&gt;: &lt;?php echo $value;?&gt;</li>
&lt;?php endforeach; ?&gt;
</ul>

<p>&lt;?php echo anchor('upload', 'Upload Another File!'); ?&gt;</p>

&lt;/body&gt;
&lt;/html&gt;
upload controller
Code:
&lt;?php

class Upload extends CI_Controller {

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

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

function do_upload()
{
$images_location = "images/";
$thumbs_location = "images/thumbs/";
$allowed_types = 'gif|jpg|png';
$thumb_width = 100;
$maximum_size =  50000000;
$results = "Click to upload";
$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);
}
}
my model
Code:
&lt;?php
class Create_thumb extends CI_Model{
function __construct()
{
  parent::__construct();
}
function create_thumbnail($source,$destination,$thumb_width){
  $size = getimagesize($source);
  $width = $size[0];
  $height = $size[1];
  $x = 0;
  $y = 0;
  if($width> $height){
   $x = ceil(($width - $height / 2);
   $width = $height;
    }elseif ($height> $width) {
     $y = ceil(($height - $width / 2);
      $height = $width;
      }
     $new_image = imagecreatetruecolor($thumb_width,$thumb_width)
                    or die('cannot install new gd image stream');
     $extension = get_image_extension('$source');
     if ($extension=='jpg' || $extension=='jpeg')
       $image = imagecreatefromjpeg($source);
     if ($extension=='gif')
       $image = imagecreatefromgif($source);
     if ($extension=='png')
       $image = imagecreatefrompng($source);
     imagecopyresampled($new_image,$image,0,0,$x,$y,$thumb_width,$thumb_width,$width,$height);
     if ($extension=='jpg' || $extension=='jpeg')
      imagejpeg($new_image,$destination);
     if ($extension=='gif')
      imagegif($new_image,$destination);
     if ($extension=='png')
      imagepng($new_image,$destination);
    }
    //get extensions
    function get_image_extension($name){
     $name = strtolower($name);
     $i = strrpos($name,".");
     if (!$i)( return ""; )
      $l = strlen($name) - $i;
      $extension = substr($name,$i+l,$l);
      return $extension;
    }//end get extendsion
    function random_name($length){
     $characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
     $name = "";

     for ($i = 0; $i < $length;$i++){
      $name .=$characters[mt_rand(0, strlen($characters) - 1)];
     }
     return "image-".$name;
    }

}
my 2nd model
Code:
&lt;?php
if($_POST){
if($_FILES['images']['name'] == ""){
$results = "Sorry but you forgot something!";
else{
  $size=filesize($_FILES['image']['tmp_name']);
  $filename = stripslashes($_FILES['image']['name']);
  $extension = get_image_extension($filename);
  if($size > $maximum_size){
   $results="your file size is big";
  }
  else
   if((extension != "jpg")&&
      (extension != "jpeg")&&
      (extension != "png")&&
      (extension != "gif")) {
    $results = 'wrong image type</center>';
   }else{
    $image_random_name= random_name(15),".",$extension;
     $copy = @copy($_FILES['image']['tmp_name'], $images_location,$image_random_name);
     if (!$copy){
      $results = "Try useing a different image that hasnt already been posted on lulzlab";
     }
     else{
      create_thumbnail($images_location,$image_random_name,$thumbs_location,$image_random_name,$thumb_width);
      $results = "image has been uploaded"
     }
   }

     }
still not working help?
#2

[eluser]LuckyFella73[/eluser]
Can you be more specific about what doesn't work? Do you
get error messages?

Btw, why don't you use the build in image lib to resize/ create thumb?




Theme © iAndrew 2016 - Forum software by © MyBB