doc and pdf upload possibility (Features)

by Eelco @, Monday, November 02, 2009, 12:24 (5286 days ago)
edited by Eelco, Monday, November 02, 2009, 12:52

Dear reader,

I'm in really need of the possibility of uploading .doc and .pdf files. I've tried to change the vars within upload_image.inc/function.inc, but I can't get it to work.
Is there anyone who is willing to help or get me to the right direction?

Thanks in advance,

Eelco

locked
16188 views
Avatar

doc and pdf upload possibility

by Micha ⌂, Monday, November 02, 2009, 13:07 (5286 days ago) @ Eelco

Hi,

you have to modify the upload_image.inc.php. If the uploaded file is not an image, the function getimagesize() return false an the uploaded file will not be stored.

Regards
Micha

--
applied-geodesy.org - OpenSource Least-Squares Adjustment Software for Geodetic Sciences

locked
14638 views

doc and pdf upload possibility

by Eelco @, Monday, November 02, 2009, 13:20 (5286 days ago) @ Micha

Thanks for your fast reply. I already stumbled on that one, still have a hard time figuring out the right modifications. I tried to use the "filesize" instead, but it still gives me an false statement.

locked
14485 views
Avatar

doc and pdf upload possibility

by Micha ⌂, Monday, November 02, 2009, 13:41 (5286 days ago) @ Eelco

Hi,

can you show your changes?

Regards
Micha

--
applied-geodesy.org - OpenSource Least-Squares Adjustment Software for Geodetic Sciences

locked
14442 views

doc and pdf upload possibility

by Eelco @, Monday, November 02, 2009, 13:50 (5286 days ago) @ Micha

Milo,

Still tweaking around -i'd got the whole thing crashed, so i changed everything back to normal.
The line i did change is around:

unset($errors);
 $image_info = filesize($_FILES['probe']['tmp_name']);
    #$image_info = getimagesize($_FILES['probe']['tmp_name']);

If you can help me with this, I'm more then thankful and I will buy you a cake (or beer if you like), really...

<?php
if(!defined('IN_INDEX'))
 {
  header('Location: ../index.php');
  exit;
 }
 
// upload folder:
$uploaded_images_path = 'images/uploaded/';
$images_per_page = 5;
 
if(($settings['upload_images']==1 && isset($_SESSION[$settings['session_prefix'].'user_type']) && $_SESSION[$settings['session_prefix'].'user_type']>0)||($settings['upload_images']==2 && isset($_SESSION[$settings['session_prefix'].'user_id']))||($settings['upload_images']==3))
 {
  // upload:image:
  if(isset($_FILES['probe']) && $_FILES['probe']['size'] != 0 && !$_FILES['probe']['error'])
   {
    unset($errors);
 $image_info = filesize($_FILES['probe']['tmp_name']);
    #$image_info = getimagesize($_FILES['probe']['tmp_name']);

    if(!is_array($image_info) || $image_info[2] != 1 && $image_info[2] != 2 && $image_info[2] != 3 && $image_info[2] != 4 && $image_info[2] != 5) $errors[] = 'invalid_file_format';
 
    if(empty($errors))
     {
      if($_FILES['probe']['size'] > $settings['upload_max_img_size']*1000 || $image_info[0] > $settings['upload_max_img_width'] || $image_info[1] > $settings['upload_max_img_height'])
       {
        $compression = 10;
        $width=$image_info[0];
        $height=$image_info[1];
 
        #resize if too large:
        if($width > $settings['upload_max_img_width'] || $height > $settings['upload_max_img_height'])
         {
          if($width >= $height)
           {
            $new_width = $settings['upload_max_img_width'];
            $new_height = intval($height*$new_width/$width);
           }
          else
           {
            $new_height = $settings['upload_max_img_height'];
            $new_width = intval($width*$new_height/$height);
           }
         }        
        else
         {
          $new_width=$width;
          $new_height=$height;
         }
 
        $img_tmp_name = uniqid(rand()).'.tmp';
 
        for($compression = 100; $compression>1; $compression=$compression-10)
         {
          if(!resize_image($_FILES['probe']['tmp_name'], $uploaded_images_path.$img_tmp_name, $new_width, $new_height, $compression)) 
           { 
            $file_size = $_FILES['probe']['size']; // @filesize($_FILES['probe']['tmp_name']); 
            break; 
           }
          $file_size = @filesize($uploaded_images_path.$img_tmp_name);
          if($image_info[2]!=2 && $file_size > $settings['upload_max_img_size']*1000) break;
          if($file_size <= $settings['upload_max_img_size']*1000) break;
         }
        if($file_size > $settings['upload_max_img_size']*1000) 
         {
          $smarty->assign('width',$image_info[0]);
          $smarty->assign('height',$image_info[1]);
          $smarty->assign('filesize',number_format($_FILES['probe']['size']/1000,0,',',''));
          $smarty->assign('max_width',$settings['upload_max_img_width']);
          $smarty->assign('max_height',$settings['upload_max_img_height']);
          $smarty->assign('max_filesize',$settings['upload_max_img_size']);
          $errors[] = 'file_too_large';
         } 
        if(isset($errors))
         {
          if(file_exists($uploaded_images_path.$img_tmp_name))
           {
            @chmod($uploaded_images_path.$img_tmp_name, 0777);
            @unlink($uploaded_images_path.$img_tmp_name);
           }
         }
       }
     }
 
    if(empty($errors))
     {
      $filename = date("YmdHis").uniqid('');
      switch($image_info[2])
       {
        case 1:
         $filename .= '.gif';
         break;
        case 2:
         $filename .= '.jpg';
         break;
        case 3:
         $filename .= '.png';
         break;
  case 4:
         $filename .= '.doc';
         break;
  case 5:
         $filename .= '.pdf';
         break;
       }
      if(isset($img_tmp_name))
       {
        @rename($uploaded_images_path.$img_tmp_name, $uploaded_images_path.$filename) or $errors[] = 'upload_error';
        $smarty->assign('image_downsized',true);
        $smarty->assign('new_width',$new_width);
        $smarty->assign('new_height',$new_height);
        $smarty->assign('new_filesize',number_format($file_size/1000,0,',',''));
       }
      else
       {
        @move_uploaded_file($_FILES['probe']['tmp_name'], $uploaded_images_path.$filename) or $errors[] = 'upload_error';
       }
     }
    if(empty($errors))
     {
      @chmod($uploaded_images_path.$filename, 0644);
      $smarty->assign('uploaded_file',$filename);
     }
    else 
     {
      $smarty->assign('errors',$errors);
      $smarty->assign('form',true);
     } 
   }
 
  // delete image:
  elseif(isset($_REQUEST['delete']) && isset($_SESSION[$settings['session_prefix'].'user_type']) && $_SESSION[$settings['session_prefix'].'user_type']>0) 
   {
    if(empty($_REQUEST['delete_confirm']))
     {
      $smarty->assign('delete_confirm',true);
      $smarty->assign('delete',htmlspecialchars($_REQUEST['delete']));
      if(isset($_REQUEST['current'])) $smarty->assign('current',intval($_REQUEST['current']));
     }
    else
     {
      if(preg_match('/^([a-z0-9]+)\.(gif|jpg|png|doc|pdf)$/', $_REQUEST['delete']) && file_exists($uploaded_images_path.$_REQUEST['delete']))
       {
        @chmod($uploaded_images_path.$_REQUEST['delete'], 0777);
        @unlink($uploaded_images_path.$_REQUEST['delete']);
       }
      if(isset($_REQUEST['current'])) $bi = '&browse_images='.intval($_REQUEST['current']);
      else $bi = '&browse_images=1';
      header('Location: index.php?mode=upload_image'.$bi);
      exit;
     }
   }
 
  // browse uploaded images:
  elseif(isset($_GET['browse_images'])) 
   {
      $browse_images = intval($_GET['browse_images']);
      if($browse_images<1)$browse_images=1;
      $handle=opendir($uploaded_images_path);
      while ($file = readdir($handle))
       {
        if(preg_match('/\.jpg$/i', $file) || preg_match('/\.png$/i', $file) || preg_match('/\.gif$/i', $file) || preg_match('/\.doc$/i', $file) || preg_match('/\.pdf$/i', $file))
         {
          $images[] = $file;
         }
        }
      closedir($handle);
      if(isset($images))
       {
        rsort($images);
        $images_count = count($images);
        if($browse_images > ceil($images_count/$images_per_page)) $browse_images=ceil($images_count/$images_per_page);  
        $start = $browse_images * $images_per_page - $images_per_page;
        $show_images_to = $browse_images * $images_per_page;
        if($show_images_to>$images_count) $show_images_to = $images_count;
       }
      else $images_count = 0;
      $smarty->assign('current',$browse_images);
      if($browse_images*$images_per_page < $images_count) $smarty->assign('next',$browse_images+1);
      if($browse_images > 1) $smarty->assign('previous',$browse_images-1);
      $smarty->assign('browse_images',true);
      $smarty->assign('images_per_page',$images_per_page);
      if(isset($images)) $smarty->assign('images',$images);
      if(isset($start)) $smarty->assign('start',$start);
   }
 
  // display form to upload image:
  elseif(empty($_GET['browse_images']))
   {
    $smarty->assign('form',true);
   }
 
  if(empty($errors) && isset($_FILES['probe']['error'])) 
   {
    $smarty->assign('server_max_filesize', ini_get('upload_max_filesize'));
    $errors[] = 'upload_error_2';
    $smarty->assign('errors',$errors);
   }   
 
 }
 
$template = 'upload_image.tpl'; 
?>
 
locked
14613 views
Avatar

doc and pdf upload possibility

by Micha ⌂, Monday, November 02, 2009, 14:13 (5286 days ago) @ Eelco
edited by Micha, Monday, November 02, 2009, 14:57

Hi,

unset($errors);
$image_info = filesize($_FILES['probe']['tmp_name']);
#$image_info = getimagesize($_FILES['probe']['tmp_name']);

Okay, and $image_info is empty or false? Try $_FILES['probe']['size'] to get the filesize.

And then, you need a new switch point because the next restriction:

if(!is_array($image_info) || $image_info[2] != 1 && $image_info[2] 
!= 2 && $image_info[2] != 3 && $image_info[2] != 4 && $image_info[2] != 5) 
$errors[] = 'invalid_file_format';

is not true!

regards Micha

--
applied-geodesy.org - OpenSource Least-Squares Adjustment Software for Geodetic Sciences

locked
14622 views

doc and pdf upload possibility

by Eelco @, Monday, November 02, 2009, 15:43 (5286 days ago) @ Micha

Thanks again

      switch($image_info[2])
       {
        case 1:
         $filename .= '.gif';
         break;
        case 2:
         $filename .= '.jpg';
         break;
        case 3:
         $filename .= '.png';
         break;
  case 4:
         $filename .= '.doc';
         break;
  case 5:
         $filename .= '.pdf';
         break;
       }


I've changed the swtich to above, but still outputs false.

locked
14548 views
Avatar

doc and pdf upload possibility

by Micha ⌂, Monday, November 02, 2009, 16:40 (5286 days ago) @ Eelco

Hi,

I've changed the swtich to above, but still outputs false.

The switch in line 84ff? No you are to fast! We still standing at line 20:

$image_info = $_FILES['probe']['size'];

In the following restriction you check if (!is_array($image_info) || ... ), because getimagesize() returns an array. See Your code above, you do not use getimagesize(), you are using $_FILES['probe']['size'].

Thus $image_info is not an array, it is a simple integer --> $errors[] = 'invalid_file_format';.

Use a function like the following to check the extension:

function endsWith( $str, $sub ) {   
  return ( substr( $str, strlen( $str ) - strlen( $sub ) ) === $sub );
}

Warning: !!!This function only check the extension string!!!!

We start in line 20

 
unset($errors);
$image_info = getimagesize($_FILES['probe']['tmp_name']);
 
$isAddionalAcceptedFile = false; // a simple flag
// we want zip and pdf files
if (endsWith($_FILES['probe']['name'], ".zip") || endsWith($_FILES['probe']['name'], ".pdf") {
  // it is an file that ends with zip or pdf --> true
  $isAddionalAcceptedFile = true;
}
elseif(!is_array($image_info) || $image_info[2] != 1 && $image_info[2] != 2 && $image_info[2] != 3) $errors[] = 'invalid_file_format';

the next changes are at line 112ff:

 
//....
else
       {
        @move_uploaded_file($_FILES['probe']['tmp_name'], $uploaded_images_path.$filename) or $errors[] = 'upload_error';
       }
     }
 
//Move to directory
if ($isAddionalAcceptedFile) {
  unset($errors); 
  @move_uploaded_file($_FILES['probe']['tmp_name'], $uploaded_images_path.$_FILES['probe']['name']) or $errors[] = 'upload_error';
 
}
 
 
 
    if(empty($errors))
     {
      @chmod($uploaded_images_path.$filename, 0644);
      $smarty->assign('uploaded_file',$filename);
     }
    else 
     {
      $smarty->assign('errors',$errors);
      $smarty->assign('form',true);
     } 
   }

I do not try it out! Maybe, it is a quick & dirty solution!

regards Milo

--
applied-geodesy.org - OpenSource Least-Squares Adjustment Software for Geodetic Sciences

locked
14396 views

doc and pdf upload possibility

by Eelco, Tuesday, November 03, 2009, 09:20 (5285 days ago) @ Micha

Thanks a bunch, mate!
Unfortunately it's output an error at line 22; unexpected '{' -but there's nothing wrong with that {. Is it? Should be an error somewhere else...

if (endsWith($_FILES['probe']['name'], ".zip") || endsWith($_FILES['probe']['name'], ".pdf") {

<?php
if(!defined('IN_INDEX'))
 {
  header('Location: ../index.php');
  exit;
 }
 
// upload folder:
$uploaded_images_path = 'images/uploaded/';
$images_per_page = 5;
 
if(($settings['upload_images']==1 && isset($_SESSION[$settings['session_prefix'].'user_type']) && $_SESSION[$settings['session_prefix'].'user_type']>0)||($settings['upload_images']==2 && isset($_SESSION[$settings['session_prefix'].'user_id']))||($settings['upload_images']==3))
 {
  // upload:image:
  if(isset($_FILES['probe']) && $_FILES['probe']['size'] != 0 && !$_FILES['probe']['error'])
   {
unset($errors);
$image_info = getimagesize($_FILES['probe']['tmp_name']);
 
$isAddionalAcceptedFile = false; // a simple flag
// we want zip and pdf files
if (endsWith($_FILES['probe']['name'], ".zip") || endsWith($_FILES['probe']['name'], ".pdf") {
  // it is an file that ends with zip or pdf --> true
  $isAddionalAcceptedFile = true;
}
elseif(!is_array($image_info) || $image_info[2] != 1 && $image_info[2] != 2 && $image_info[2] != 3) $errors[] = 'invalid_file_format';
 
    if(empty($errors))
     {
      if($_FILES['probe']['size'] > $settings['upload_max_img_size']*1000 || $image_info[0] > $settings['upload_max_img_width'] || $image_info[1] > $settings['upload_max_img_height'])
       {
        $compression = 10;
        $width=$image_info[0];
        $height=$image_info[1];
 
        #resize if too large:
        if($width > $settings['upload_max_img_width'] || $height > $settings['upload_max_img_height'])
         {
          if($width >= $height)
           {
            $new_width = $settings['upload_max_img_width'];
            $new_height = intval($height*$new_width/$width);
           }
          else
           {
            $new_height = $settings['upload_max_img_height'];
            $new_width = intval($width*$new_height/$height);
           }
         }        
        else
         {
          $new_width=$width;
          $new_height=$height;
         }
 
        $img_tmp_name = uniqid(rand()).'.tmp';
 
        for($compression = 100; $compression>1; $compression=$compression-10)
         {
          if(!resize_image($_FILES['probe']['tmp_name'], $uploaded_images_path.$img_tmp_name, $new_width, $new_height, $compression)) 
           { 
            $file_size = $_FILES['probe']['size']; // @filesize($_FILES['probe']['tmp_name']); 
            break; 
           }
          $file_size = @filesize($uploaded_images_path.$img_tmp_name);
          if($image_info[2]!=2 && $file_size > $settings['upload_max_img_size']*1000) break;
          if($file_size <= $settings['upload_max_img_size']*1000) break;
         }
        if($file_size > $settings['upload_max_img_size']*1000) 
         {
          $smarty->assign('width',$image_info[0]);
          $smarty->assign('height',$image_info[1]);
          $smarty->assign('filesize',number_format($_FILES['probe']['size']/1000,0,',',''));
          $smarty->assign('max_width',$settings['upload_max_img_width']);
          $smarty->assign('max_height',$settings['upload_max_img_height']);
          $smarty->assign('max_filesize',$settings['upload_max_img_size']);
          $errors[] = 'file_too_large';
         } 
        if(isset($errors))
         {
          if(file_exists($uploaded_images_path.$img_tmp_name))
           {
            @chmod($uploaded_images_path.$img_tmp_name, 0777);
            @unlink($uploaded_images_path.$img_tmp_name);
           }
         }
       }
     }
 
    if(empty($errors))
     {
      $filename = date("YmdHis").uniqid('');
      switch($image_info[2])
       {
        case 1:
         $filename .= '.gif';
         break;
        case 2:
         $filename .= '.jpg';
         break;
        case 3:
         $filename .= '.png';
         break;
  case 4:
         $filename .= '.doc';
         break;
  case 5:
         $filename .= '.pdf';
         break;
       }
      if(isset($img_tmp_name))
       {
        @rename($uploaded_images_path.$img_tmp_name, $uploaded_images_path.$filename) or $errors[] = 'upload_error';
        $smarty->assign('image_downsized',true);
        $smarty->assign('new_width',$new_width);
        $smarty->assign('new_height',$new_height);
        $smarty->assign('new_filesize',number_format($file_size/1000,0,',',''));
       }
//....
else
       {
        @move_uploaded_file($_FILES['probe']['tmp_name'], $uploaded_images_path.$filename) or $errors[] = 'upload_error';
       }
     }
 
//Move to directory
if ($isAddionalAcceptedFile) {
  unset($errors); 
  @move_uploaded_file($_FILES['probe']['tmp_name'], $uploaded_images_path.$_FILES['probe']['name']) or $errors[] = 'upload_error';
 
}
 
 
 
    if(empty($errors))
     {
      @chmod($uploaded_images_path.$filename, 0644);
      $smarty->assign('uploaded_file',$filename);
     }
    else 
     {
      $smarty->assign('errors',$errors);
      $smarty->assign('form',true);
     } 
   }
 
 
  // delete image:
  elseif(isset($_REQUEST['delete']) && isset($_SESSION[$settings['session_prefix'].'user_type']) && $_SESSION[$settings['session_prefix'].'user_type']>0) 
   {
    if(empty($_REQUEST['delete_confirm']))
     {
      $smarty->assign('delete_confirm',true);
      $smarty->assign('delete',htmlspecialchars($_REQUEST['delete']));
      if(isset($_REQUEST['current'])) $smarty->assign('current',intval($_REQUEST['current']));
     }
    else
     {
      if(preg_match('/^([a-z0-9]+)\.(gif|jpg|png|doc|pdf)$/', $_REQUEST['delete']) && file_exists($uploaded_images_path.$_REQUEST['delete']))
       {
        @chmod($uploaded_images_path.$_REQUEST['delete'], 0777);
        @unlink($uploaded_images_path.$_REQUEST['delete']);
       }
      if(isset($_REQUEST['current'])) $bi = '&browse_images='.intval($_REQUEST['current']);
      else $bi = '&browse_images=1';
      header('Location: index.php?mode=upload_image'.$bi);
      exit;
     }
   }
 
  // browse uploaded images:
  elseif(isset($_GET['browse_images'])) 
   {
      $browse_images = intval($_GET['browse_images']);
      if($browse_images<1)$browse_images=1;
      $handle=opendir($uploaded_images_path);
      while ($file = readdir($handle))
       {
        if(preg_match('/\.jpg$/i', $file) || preg_match('/\.png$/i', $file) || preg_match('/\.gif$/i', $file) || preg_match('/\.doc$/i', $file) || preg_match('/\.pdf$/i', $file))
         {
          $images[] = $file;
         }
        }
      closedir($handle);
      if(isset($images))
       {
        rsort($images);
        $images_count = count($images);
        if($browse_images > ceil($images_count/$images_per_page)) $browse_images=ceil($images_count/$images_per_page);  
        $start = $browse_images * $images_per_page - $images_per_page;
        $show_images_to = $browse_images * $images_per_page;
        if($show_images_to>$images_count) $show_images_to = $images_count;
       }
      else $images_count = 0;
      $smarty->assign('current',$browse_images);
      if($browse_images*$images_per_page < $images_count) $smarty->assign('next',$browse_images+1);
      if($browse_images > 1) $smarty->assign('previous',$browse_images-1);
      $smarty->assign('browse_images',true);
      $smarty->assign('images_per_page',$images_per_page);
      if(isset($images)) $smarty->assign('images',$images);
      if(isset($start)) $smarty->assign('start',$start);
   }
 
  // display form to upload image:
  elseif(empty($_GET['browse_images']))
   {
    $smarty->assign('form',true);
   }
 
  if(empty($errors) && isset($_FILES['probe']['error'])) 
   {
    $smarty->assign('server_max_filesize', ini_get('upload_max_filesize'));
    $errors[] = 'upload_error_2';
    $smarty->assign('errors',$errors);
   }   
 
 }
 
$template = 'upload_image.tpl'; 
?>
locked
14732 views
Avatar

doc and pdf upload possibility

by Micha ⌂, Tuesday, November 03, 2009, 10:50 (5285 days ago) @ Eelco

Hi,

close the brace at the end of that line:

if (endsWith($_FILES['probe']['name'], ".zip") || endsWith($_FILES['probe']['name'], ".pdf")) {

Sorry, I do not check the code in use.

break;
case 4:
$filename .= '.doc';
break;
case 5:
$filename .= '.pdf';
break;
}

You can delete your new cases, because they are out of reach.

}
//....
else

You can delete my comment-placeholder (//...), too. ;-)

Do you define the new function endsWith anywhere?

regards Micha

--
applied-geodesy.org - OpenSource Least-Squares Adjustment Software for Geodetic Sciences

locked
14612 views

doc and pdf upload possibility

by Eelco, Wednesday, November 04, 2009, 09:55 (5284 days ago) @ Micha

Hello Milo,

Thank you so much for your help.
Unfortunately after trying to upload a PDF it gives an error, like: "Fatal error: Call to undefined function endsWith() in -blablabla- on line 22 -I still use the same code, but closed the brace on line 22.
So the function doesn't get defined, but it really gets to mucg from here. My basic php knowledge isn't enough to tackle this...

- Eelco

locked
14443 views

doc and pdf upload possibility

by Eelco, Wednesday, November 04, 2009, 15:21 (5284 days ago) @ Eelco

Hi,

Okay, i've got rid of the error (eg, "upload_error"). I now get a nice error, so the code is correct.

Should been omething within

//....
else
       {
        @move_uploaded_file($_FILES['probe']['tmp_name'], $uploaded_images_path.$filename) or $errors[] = 'upload_error';
       }
     }
 
//Move to directory
if ($isAddionalAcceptedFile) {
  unset($errors); 
  @move_uploaded_file($_FILES['probe']['tmp_name'], $uploaded_images_path.$_FILES['probe']['name']) or $errors[] = 'upload_error';
 
}


but i'm stuck for now.
Below the complete code.

<?php
if(!defined('IN_INDEX'))
 {
  header('Location: ../index.php');
  exit;
 }
 
// upload folder:
$uploaded_images_path = 'images/uploaded/';
$images_per_page = 5;
 
if(($settings['upload_images']==1 && isset($_SESSION[$settings['session_prefix'].'user_type']) && $_SESSION[$settings['session_prefix'].'user_type']>0)||($settings['upload_images']==2 && isset($_SESSION[$settings['session_prefix'].'user_id']))||($settings['upload_images']==3))
 {
  // upload:image:
  if(isset($_FILES['probe']) && $_FILES['probe']['size'] != 0 && !$_FILES['probe']['error'])
   {
unset($errors);
$image_info = getimagesize($_FILES['probe']['tmp_name']);
 
 //check
 
 function endsWith( $str, $sub ) {   
  return ( substr( $str, strlen( $str ) - strlen( $sub ) ) === $sub );
}
 
$isAddionalAcceptedFile = false; // a simple flag
// we want zip and pdf files
if (endsWith($_FILES['probe']['name'], ".zip") || endsWith($_FILES['probe']['name'], ".pdf")) {
  // it is an file that ends with zip or pdf --> true
  $isAddionalAcceptedFile = true;
}
elseif(!is_array($image_info) || $image_info[2] != 1 && $image_info[2] != 2 && $image_info[2] != 3) $errors[] = 'invalid_file_format';
 
    if(empty($errors))
     {
      if($_FILES['probe']['size'] > $settings['upload_max_img_size']*1000 || $image_info[0] > $settings['upload_max_img_width'] || $image_info[1] > $settings['upload_max_img_height'])
       {
        $compression = 10;
        $width=$image_info[0];
        $height=$image_info[1];
 
        #resize if too large:
        if($width > $settings['upload_max_img_width'] || $height > $settings['upload_max_img_height'])
         {
          if($width >= $height)
           {
            $new_width = $settings['upload_max_img_width'];
            $new_height = intval($height*$new_width/$width);
           }
          else
           {
            $new_height = $settings['upload_max_img_height'];
            $new_width = intval($width*$new_height/$height);
           }
         }        
        else
         {
          $new_width=$width;
          $new_height=$height;
         }
 
        $img_tmp_name = uniqid(rand()).'.tmp';
 
        for($compression = 100; $compression>1; $compression=$compression-10)
         {
          if(!resize_image($_FILES['probe']['tmp_name'], $uploaded_images_path.$img_tmp_name, $new_width, $new_height, $compression)) 
           { 
            $file_size = $_FILES['probe']['size']; // @filesize($_FILES['probe']['tmp_name']); 
            break; 
           }
          $file_size = @filesize($uploaded_images_path.$img_tmp_name);
          if($image_info[2]!=2 && $file_size > $settings['upload_max_img_size']*1000) break;
          if($file_size <= $settings['upload_max_img_size']*1000) break;
         }
        if($file_size > $settings['upload_max_img_size']*1000) 
         {
          $smarty->assign('width',$image_info[0]);
          $smarty->assign('height',$image_info[1]);
          $smarty->assign('filesize',number_format($_FILES['probe']['size']/1000,0,',',''));
          $smarty->assign('max_width',$settings['upload_max_img_width']);
          $smarty->assign('max_height',$settings['upload_max_img_height']);
          $smarty->assign('max_filesize',$settings['upload_max_img_size']);
          $errors[] = 'file_too_large';
         } 
        if(isset($errors))
         {
          if(file_exists($uploaded_images_path.$img_tmp_name))
           {
            @chmod($uploaded_images_path.$img_tmp_name, 0777);
            @unlink($uploaded_images_path.$img_tmp_name);
           }
         }
       }
     }
 
    if(empty($errors))
     {
      $filename = date("YmdHis").uniqid('');
      switch($image_info[2])
       {
        case 1:
         $filename .= '.gif';
         break;
        case 2:
         $filename .= '.jpg';
         break;
        case 3:
         $filename .= '.png';
         break;
   }
      if(isset($img_tmp_name))
       {
        @rename($uploaded_images_path.$img_tmp_name, $uploaded_images_path.$filename) or $errors[] = 'upload_error';
        $smarty->assign('image_downsized',true);
        $smarty->assign('new_width',$new_width);
        $smarty->assign('new_height',$new_height);
        $smarty->assign('new_filesize',number_format($file_size/1000,0,',',''));
       }
//....
else
       {
        @move_uploaded_file($_FILES['probe']['tmp_name'], $uploaded_images_path.$filename) or $errors[] = 'upload_error';
       }
     }
 
//Move to directory
if ($isAddionalAcceptedFile) {
  unset($errors); 
  @move_uploaded_file($_FILES['probe']['tmp_name'], $uploaded_images_path.$_FILES['probe']['name']) or $errors[] = 'upload_error';
 
}
 
 
 
    if(empty($errors))
     {
      @chmod($uploaded_images_path.$filename, 0644);
      $smarty->assign('uploaded_file',$filename);
     }
    else 
     {
      $smarty->assign('errors',$errors);
      $smarty->assign('form',true);
     } 
   }
 
 
  // delete image:
  elseif(isset($_REQUEST['delete']) && isset($_SESSION[$settings['session_prefix'].'user_type']) && $_SESSION[$settings['session_prefix'].'user_type']>0) 
   {
    if(empty($_REQUEST['delete_confirm']))
     {
      $smarty->assign('delete_confirm',true);
      $smarty->assign('delete',htmlspecialchars($_REQUEST['delete']));
      if(isset($_REQUEST['current'])) $smarty->assign('current',intval($_REQUEST['current']));
     }
    else
     {
      if(preg_match('/^([a-z0-9]+)\.(gif|jpg|png|doc|pdf)$/', $_REQUEST['delete']) && file_exists($uploaded_images_path.$_REQUEST['delete']))
       {
        @chmod($uploaded_images_path.$_REQUEST['delete'], 0777);
        @unlink($uploaded_images_path.$_REQUEST['delete']);
       }
      if(isset($_REQUEST['current'])) $bi = '&browse_images='.intval($_REQUEST['current']);
      else $bi = '&browse_images=1';
      header('Location: index.php?mode=upload_image'.$bi);
      exit;
     }
   }
 
  // browse uploaded images:
  elseif(isset($_GET['browse_images'])) 
   {
      $browse_images = intval($_GET['browse_images']);
      if($browse_images<1)$browse_images=1;
      $handle=opendir($uploaded_images_path);
      while ($file = readdir($handle))
       {
        if(preg_match('/\.jpg$/i', $file) || preg_match('/\.png$/i', $file) || preg_match('/\.gif$/i', $file) || preg_match('/\.doc$/i', $file) || preg_match('/\.pdf$/i', $file))
         {
          $images[] = $file;
         }
        }
      closedir($handle);
      if(isset($images))
       {
        rsort($images);
        $images_count = count($images);
        if($browse_images > ceil($images_count/$images_per_page)) $browse_images=ceil($images_count/$images_per_page);  
        $start = $browse_images * $images_per_page - $images_per_page;
        $show_images_to = $browse_images * $images_per_page;
        if($show_images_to>$images_count) $show_images_to = $images_count;
       }
      else $images_count = 0;
      $smarty->assign('current',$browse_images);
      if($browse_images*$images_per_page < $images_count) $smarty->assign('next',$browse_images+1);
      if($browse_images > 1) $smarty->assign('previous',$browse_images-1);
      $smarty->assign('browse_images',true);
      $smarty->assign('images_per_page',$images_per_page);
      if(isset($images)) $smarty->assign('images',$images);
      if(isset($start)) $smarty->assign('start',$start);
   }
 
  // display form to upload image:
  elseif(empty($_GET['browse_images']))
   {
    $smarty->assign('form',true);
   }
 
  if(empty($errors) && isset($_FILES['probe']['error'])) 
   {
    $smarty->assign('server_max_filesize', ini_get('upload_max_filesize'));
    $errors[] = 'upload_error_2';
    
locked
14509 views
Avatar

doc and pdf upload possibility

by Micha ⌂, Wednesday, November 04, 2009, 18:02 (5284 days ago) @ Eelco

Hi,

Do you define the new function endsWith anywhere?

;-)

I now get a nice error, so the code is correct.

Can you change the error message in the script?

 
//Move to directory
if ($isAddionalAcceptedFile) {
   unset($errors); 
   @move_uploaded_file($_FILES['probe']['tmp_name'], $uploaded_images_path.$_FILES['probe']['name']) or $errors[] = 'upload_error '.$uploaded_images_path.$_FILES['probe']['name'];

regards Micha

--
applied-geodesy.org - OpenSource Least-Squares Adjustment Software for Geodetic Sciences

locked
14439 views

doc and pdf upload possibility

by Eelco, Friday, November 06, 2009, 15:15 (5282 days ago) @ Micha

Sorry for my late reply; very busy here at the moment. But we're getting closer... The files are uploaded, but no extension is given to the file, so the file is renamed like "2009452523".
Not sure if the error has something to do with not getting a thumbnail of the pdf -could be just be an icon or something- or that that problem lays within the missing extension.

<?php 
//Move to directory
if ($isAddionalAcceptedFile) {
   unset($errors); 
   @move_uploaded_file($_FILES['probe']['tmp_name'], $uploaded_images_path.$_FILES['probe']['name']) or $errors[] = 'upload_error '.$uploaded_images_path.$_FILES['probe']['name'];
 
} ?>
locked
14728 views
Avatar

doc and pdf upload possibility

by Micha ⌂, Friday, November 06, 2009, 16:05 (5282 days ago) @ Eelco
edited by Auge, Friday, April 29, 2016, 12:13

Hi,

check this modification.

 
<?php
if(!defined('IN_INDEX'))
 {
  header('Location: ../index.php');
  exit;
 }
 
// upload folder:
$uploaded_images_path   = 'images/uploaded/';
$images_per_page = 5;
$isAddionalAcceptedFile = false; // a simple flag
 
// see: http://www.php.net/manual/de/function.move-uploaded-file.php#94126
$rEFileTypes = 
  "/^\.(zip|rar|ace|jar|doc|docx|txt|rtf|pdf|xls|xlsx| 
        ppt|pptx){1}$/i"; 
 
 
if(($settings['upload_images']==1 && isset($_SESSION[$settings['session_prefix'].'user_type']) && $_SESSION[$settings['session_prefix'].'user_type']>0)||($settings['upload_images']==2 && isset($_SESSION[$settings['session_prefix'].'user_id']))||($settings['upload_images']==3))
 {
  // upload:image:
  if(isset($_FILES['probe']) && $_FILES['probe']['size'] != 0 && !$_FILES['probe']['error'])
   {
    unset($errors);
    $image_info = getimagesize($_FILES['probe']['tmp_name']);
    if (preg_match($rEFileTypes, strrchr(trim($_FILES['probe']['name']), '.')) && $_FILES['probe']['size'] <= $settings['upload_max_img_size']*1000) {
  $filename = preg_replace( 
      array("/\s+/", "/[^-\.\w]+/"), 
      array("_", ""), 
      trim($_FILES['probe']['name'])
  );
  $filename = date("YmdHis").uniqid('')."_".$filename;
  @move_uploaded_file ( 
     $_FILES['probe']['tmp_name'], 
     $uploaded_images_path.$filename
  ) or $errors[] = 'upload_error';
  $isAddionalAcceptedFile = true;
 }
 elseif(!is_array($image_info) || $image_info[2] != 1 && $image_info[2] != 2 && $image_info[2] != 3) 
  $errors[] = 'invalid_file_format';
 
    if(empty($errors) && !$isAddionalAcceptedFile)
     {
      if($_FILES['probe']['size'] > $settings['upload_max_img_size']*1000 || $image_info[0] > $settings['upload_max_img_width'] || $image_info[1] > $settings['upload_max_img_height'])
       {
        #$compression = 10;
        $width=$image_info[0];
        $height=$image_info[1];
 
        // resize if too large:
        if($width > $settings['upload_max_img_width'] || $height > $settings['upload_max_img_height'])
         {
          if($width >= $height)
           {
            $new_width = $settings['upload_max_img_width'];
            $new_height = intval($height*$new_width/$width);
           }
          else
           {
            $new_height = $settings['upload_max_img_height'];
            $new_width = intval($width*$new_height/$height);
           }
         }        
        else
         {
          $new_width=$width;
          $new_height=$height;
         }
 
        $img_tmp_name = uniqid(rand()).'.tmp';
 
        for($compression = 100; $compression>1; $compression=$compression-10)
         {
          if(!resize_image($_FILES['probe']['tmp_name'], $uploaded_images_path.$img_tmp_name, $new_width, $new_height, $compression)) 
           { 
            $file_size = $_FILES['probe']['size']; // @filesize($_FILES['probe']['tmp_name']); 
            break; 
           }
          $file_size = @filesize($uploaded_images_path.$img_tmp_name);
          if($image_info[2]!=2 && $file_size > $settings['upload_max_img_size']*1000) break;
          if($file_size <= $settings['upload_max_img_size']*1000) break;
         }
        if($file_size > $settings['upload_max_img_size']*1000) 
         {
          $smarty->assign('width',$image_info[0]);
          $smarty->assign('height',$image_info[1]);
          $smarty->assign('filesize',number_format($_FILES['probe']['size']/1000,0,',',''));
          $smarty->assign('max_width',$settings['upload_max_img_width']);
          $smarty->assign('max_height',$settings['upload_max_img_height']);
          $smarty->assign('max_filesize',$settings['upload_max_img_size']);
          $errors[] = 'file_too_large';
         } 
        if(isset($errors))
         {
          if(file_exists($uploaded_images_path.$img_tmp_name))
           {
            @chmod($uploaded_images_path.$img_tmp_name, 0777);
            @unlink($uploaded_images_path.$img_tmp_name);
           }
         }
       }
     }
 
    if(empty($errors) && !$isAddionalAcceptedFile)
     {
      $filename = date("YmdHis").uniqid('');
      switch($image_info[2])
       {
        case 1:
         $filename .= '.gif';
         break;
        case 2:
         $filename .= '.jpg';
         break;
        case 3:
         $filename .= '.png';
         break;
       }
      if(isset($img_tmp_name))
       {
        @rename($uploaded_images_path.$img_tmp_name, $uploaded_images_path.$filename) or $errors[] = 'upload_error';
        $smarty->assign('image_downsized',true);
        $smarty->assign('new_width',$new_width);
        $smarty->assign('new_height',$new_height);
        $smarty->assign('new_filesize',number_format($file_size/1000,0,',',''));
       }
      else
       {
        @move_uploaded_file($_FILES['probe']['tmp_name'], $uploaded_images_path.$filename) or $errors[] = 'upload_error';
       }
     }
    if(empty($errors))
     {
      @chmod($uploaded_images_path.$filename, 0644);
      $smarty->assign('uploaded_file',$filename);
     }
    else 
     {
      $smarty->assign('errors',$errors);
      $smarty->assign('form',true);
     } 
   }
 
  // delete image:
  elseif(isset($_REQUEST['delete']) && isset($_SESSION[$settings['session_prefix'].'user_type']) && $_SESSION[$settings['session_prefix'].'user_type']>0) 
   {
    if(empty($_REQUEST['delete_confirm']))
     {
      $smarty->assign('delete_confirm',true);
      $smarty->assign('delete',htmlspecialchars($_REQUEST['delete']));
      if(isset($_REQUEST['current'])) $smarty->assign('current',intval($_REQUEST['current']));
     }
    else
     {
      if(preg_match('/^([a-z0-9]+)\.(gif|jpg|png)$/', $_REQUEST['delete']) && file_exists($uploaded_images_path.$_REQUEST['delete']))
       {
        @chmod($uploaded_images_path.$_REQUEST['delete'], 0777);
        @unlink($uploaded_images_path.$_REQUEST['delete']);
       }
      if(isset($_REQUEST['current'])) $bi = '&browse_images='.intval($_REQUEST['current']);
      else $bi = '&browse_images=1';
      header('Location: index.php?mode=upload_image'.$bi);
      exit;
     }
   }
 
  // browse uploaded images:
  elseif(isset($_GET['browse_images'])) 
   {
      $browse_images = intval($_GET['browse_images']);
      if($browse_images<1)$browse_images=1;
      $handle=opendir($uploaded_images_path);
      while ($file = readdir($handle))
       {
        if(preg_match('/\.jpg$/i', $file) || preg_match('/\.png$/i', $file) || preg_match('/\.gif$/i', $file))
         {
          $images[] = $file;
         }
        }
      closedir($handle);
      if(isset($images))
       {
        rsort($images);
        $images_count = count($images);
        if($browse_images > ceil($images_count/$images_per_page)) $browse_images=ceil($images_count/$images_per_page);  
        $start = $browse_images * $images_per_page - $images_per_page;
        $show_images_to = $browse_images * $images_per_page;
        if($show_images_to>$images_count) $show_images_to = $images_count;
       }
      else $images_count = 0;
      $smarty->assign('current',$browse_images);
      if($browse_images*$images_per_page < $images_count) $smarty->assign('next',$browse_images+1);
      if($browse_images > 1) $smarty->assign('previous',$browse_images-1);
      $smarty->assign('browse_images',true);
      $smarty->assign('images_per_page',$images_per_page);
      if(isset($images)) $smarty->assign('images',$images);
      if(isset($start)) $smarty->assign('start',$start);
   }
 
  // display form to upload image:
  elseif(empty($_GET['browse_images']))
   {
    $smarty->assign('form',true);
   }
 
  if(empty($errors) && isset($_FILES['probe']['error'])) 
   {
    $smarty->assign('server_max_filesize', ini_get('upload_max_filesize'));
    $errors[] = 'upload_error_2';
    $smarty->assign('errors',$errors);
   }   
 
 }
 
$template = 'upload_image.tpl'; 
?>
 

regards Micha

--
applied-geodesy.org - OpenSource Least-Squares Adjustment Software for Geodetic Sciences

locked
15855 views

doc and pdf upload possibility

by Eelco, Monday, November 09, 2009, 09:17 (5279 days ago) @ Micha

Wow! Working like a charm, mate. Thanks!

One thing i'm stumbling with is the insert thingy.

Instead of using"[img][/img]", a pdf or zip should be clickable trough the use of "[link][/link]" . I think it should be modified within the code around line 172, but the smarty-environment is getting me nuts.

Another mod that will be nice is displaying an icon for uploads other than images.

I hope someone can help with this. If this is done, I think we have a really nice mod for this beauty of a forum.

locked
14356 views
Avatar

doc and pdf upload possibility

by Micha ⌂, Monday, November 09, 2009, 16:29 (5279 days ago) @ Eelco

Hi,

Instead of using"[img][/img]", a pdf or zip should be clickable trough the use of "[link][/link]" . I think it should be modified within the code around line 172, but the smarty-environment is getting me nuts.

Make a copy of the upload_image.tpl and modify the BB-Code. Against the uploaded file, you load the image or your new template file.

I think we have a really nice mod for this beauty of a forum.

This is one possible solution but I think, this one is NOT the best way!

Regards Micha

--
applied-geodesy.org - OpenSource Least-Squares Adjustment Software for Geodetic Sciences

locked
14461 views

doc and pdf upload possibility

by Eelco, Tuesday, November 10, 2009, 09:48 (5278 days ago) @ Micha

I think we have a really nice mod for this beauty of a forum.

This is one possible solution but I think, this one is NOT the best way!

Alright, everything is working fine. Since I'll work with a closed group within the forum, this solution is more then okay. Thanks Micha.

locked
14520 views

doc and pdf upload possibility

by Wendell, Saturday, July 09, 2011, 16:38 (4672 days ago) @ Micha

Make a copy of the upload_image.tpl and modify the BB-Code. Against the uploaded file, you load the image or your new template file.

Could you elaborate on this a bit? Let's say I am allowing PDF uploads. How would I modify the BBCode to show link code instead of img code?

Thanks for your help.

locked
13228 views

。。。can't run 。。

by qwb, Saturday, November 07, 2009, 02:50 (5282 days ago) @ Eelco

- No text -

locked
14247 views

ok/ can run.but upload file is not Good

by qwb, Saturday, November 07, 2009, 04:08 (5282 days ago) @ Eelco

- No text -

locked
14305 views
Avatar

ok/ can run.but upload file is not Good

by Micha ⌂, Saturday, November 07, 2009, 11:28 (5281 days ago) @ qwb

Hi,

that is not a helpful message! What do you mean with "not Good"?

regards
Micha

--
applied-geodesy.org - OpenSource Least-Squares Adjustment Software for Geodetic Sciences

locked
14422 views

ok/ can run.but upload file is not Good

by qwb, Sunday, November 08, 2009, 13:02 (5280 days ago) @ Micha

I'm sorry my English not good.:no:

my mean is, the user can not manage to upload attachments, which I hope can develop a better attachment upload feature, I am looking forward to, love my litte form

locked
14456 views
Avatar

ok/ can run.but upload file is not Good

by Micha ⌂, Sunday, November 08, 2009, 15:39 (5280 days ago) @ qwb

Hi,

you try out this code?

regards Micha

--
applied-geodesy.org - OpenSource Least-Squares Adjustment Software for Geodetic Sciences

locked
14489 views

RSS Feed of thread