Home > osTicket > Multiple Attachments for Client-Side of osTicket v1.6 RC5

Multiple Attachments for Client-Side of osTicket v1.6 RC5

There are lots of questions and requests that asking about the feature at the title of this article. I successfully made the modification regarding this, and now it works properly in client-side of osTicket v1.6 RC5.

  1. First of all, download this file, extract it, there is one file inside named multifile.js, then copy it into your root directory of your osTicket, and also to your \scp\ sub directory.
  2. Open your \include\client\open.inc.php file, and find this code:

    102
    103
    104
    105
    106
    107
    
        <tr>
            <td>Attachment:</td>
            <td>
                <input type="file" name="attachment"><font class="error">&nbsp;<?=$errors['attachment']?></font>
            </td>
        </tr>

    then replace with this following code:

    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    
        <tr>
            <td>Attachment:</td>
            <td>
                <!-- <input type="file" name="attachment"><font class="error">&nbsp;<?=$errors['attachment']?></font> -->
                <input id="my_file_element" type="file" name="file_1" ><font class="error">&nbsp;<?=$errors['file_1']?></font>
            </td>
        <tr>
            <td>Files:</td>
            <td>
    <!-- This is where the output will appear -->
    <script type="text/javascript" src="multifile.js"></script>
    <div id="files_list"></div>
    <script>
    <!-- Create an instance of the multiSelector class, pass it the output target and the max number of files -->
    var multi_selector = new MultiSelector( document.getElementById( 'files_list' ), 0 );
    <!-- Pass in the file element -->
    multi_selector.addElement( document.getElementById( 'my_file_element' ) );
    </script>
            </td>
        </tr>
  3. Open your \include\class.ticket.php file, and find this code:

    823
    824
    
        //online based attached files.
        function uploadAttachment($file,$refid,$type){

    before the first line of that code above, please insert this following code:

    822
    823
    824
    825
    826
    827
    828
    829
    830
    831
    832
    833
    834
    835
    836
    837
    838
    839
    840
    841
    842
    843
    844
    845
    846
    847
    
        //online based attached files.
        function insertAttachment($ft,$fn,$fs,$refid,$type){
            global $cfg;
     
            if(!$ft || !$refid || !$type)
                 return 0;
     
            $dir=$cfg->getUploadDir();
            $rand=Misc::randCode(16);
            $fn=Format::file_name($fn);
            $filename=rtrim($dir,'/').'/'.$rand.'_'.$fn;
            if(move_uploaded_file($ft,$filename)){
                $sql ='INSERT INTO '.TICKET_ATTACHMENT_TABLE.' SET created=NOW() '.
                      ',ticket_id='.db_input($this->getId()).
                      ',ref_id='.db_input($refid).
                      ',ref_type='.db_input($type).
                      ',file_size='.db_input($fs).
                      ',file_name='.db_input($fn).
                      ',file_key='.db_input($rand);
                if(db_query($sql) && ($id=db_insert_id()))
                    return $id;
                //DB  insert failed!--remove the file..
                @unlink($filename);
            }
            return 0;
        }

    Find again this code:

    1241
    1242
    1243
    1244
    1245
    1246
    1247
    
                //Upload attachments...web based.
                if($_FILES['attachment']['name'] && $cfg->allowOnlineAttachments() && $msgid) {    
                    if(!$cfg->allowAttachmentsOnlogin() || ($cfg->allowAttachmentsOnlogin() && ($thisclient && $thisclient->isValid()))) {
                        $ticket->uploadAttachment($_FILES['attachment'],$msgid,'M');
                        //TODO: recover from upload issues?
                    }
                }

    after the last line of that code, please insert this following code:

    1249
    1250
    1251
    1252
    1253
    1254
    1255
    1256
    1257
    1258
    1259
    1260
    1261
    1262
    1263
    1264
    1265
    1266
    1267
    1268
    1269
    1270
    1271
    1272
    1273
    1274
    1275
    1276
    1277
    1278
    1279
    1280
    1281
    1282
    1283
    1284
    1285
    1286
    1287
    1288
    
                $totalsize=0;
                foreach($_FILES as $file) {
                  if ($file['name']!='') {
                    if(!$cfg->canUploadFileType($file['name'])) {          	
                      $errors['file_1'] .= 'Invalid file type [ '.$file['name'].' ].<br />';
                    }
                  }
                  if(!$cfg->allowOnlineAttachments()) {
                    $errors['file_1'] .= 'File [ '.$file['name'].' ] rejected.<br />';
                  }
                  $totalsize = $totalsize + $file['size'];
                }
                if($totalsize>$cfg->getMaxFileSize()) {
                  $errors['file_1'] .= 'File is too big ('.$totalsize.' bytes). Max '.$cfg->getMaxFileSize().' bytes allowed.<br />';
                }
     
                $uploaded = '';
                foreach($_FILES as $file) {
                  if ($file['name'] != NULL ) {
                    if(!$cfg->canUploadFileType($file['name'])) {
                      $error = '<b>Error</b>  '.$file['type'].' File type '.$file['name'].' ignored.';
                      echo $error;
                    } else {
                      $dir=$cfg->getUploadDir();
                      $rand=Misc::randCode(16);
                      $file['name']=Format::file_name($file['name']);
                      $upload_file=rtrim($dir,'/').'/'.$rand.'_'.$file['name'];
                      if (file_exists($upload_file)) {
                        $error = '<b>Error</b> '.$file['name'].' - already exist.';
                        echo $error;
                      } elseif ($ticket->insertAttachment($file['tmp_name'],$file['name'],$file['size'],$msgid,'M')!=0) {
                        $uploaded = '<b>Uploaded</b> '.$file['name'].'.';
                        echo $uploaded;
                      } else {
                        $error = '<b>Error</b> Upload failed '.$file['name'].' - Try again.';
                        echo $error;										
                      }
                    }
                  }
                }
  4. Open your \include\client\viewticket.inc.php file, and find this code:

    131
    132
    133
    134
    
                <div align="left">
                    Attach File<br><input type="file" name="attachment" id="attachment" size=30px value="<?=$info['attachment']?>" /> 
                        <font class="error">&nbsp;<?=$errors['attachment']?></font>
                </div>

    then replace with this following code:

    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    
    <!--            <div align="left">
                    Attach File<br><input type="file" name="attachment" id="attachment" size=30px value="<?=$info['attachment']?>" /> 
                        <font class="error">&nbsp;<?=$errors['attachment']?></font>
                </div>
    -->
     
        <div align="left">
            Attachment:<br />
                <input id="my_file_element" type="file" name="file_1" ><font class="error">&nbsp;<?=$errors['file_1']?></font>
            <br />Files:
    <!-- This is where the output will appear -->
    <script type="text/javascript" src="multifile.js"></script>
    <div id="files_list"></div>
    <script>
    	<!-- Create an instance of the multiSelector class, pass it the output target and the max number of files -->
    	var multi_selector = new MultiSelector( document.getElementById( 'files_list' ), 0 );
    	<!-- Pass in the file element -->
    	multi_selector.addElement( document.getElementById( 'my_file_element' ) );
    </script>
  5. Open your tickets.php file, and find this code:

    58
    59
    
                    $errors['attachment']='File is too big. Max '.$cfg->getMaxFileSize().' bytes allowed';
            }

    after the last line of that code, please insert this following code:

    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    
            $totalsize=0;
            foreach($_FILES as $file) {
                    if ($file['name']!='') {
                	  if(!$cfg->canUploadFileType($file['name'])) {
              	    $errors['file_1'] .= 'Invalid file type [ '.$file['name'].' ].<br />';
              	  }
                    }
              	if(!$cfg->allowOnlineAttachments()) {
                      $errors['file_1'] .= 'File [ '.$file['name'].' ] rejected.<br />';
    		}
              	$totalsize = $totalsize + $file['size'];
            }
            if($totalsize>$cfg->getMaxFileSize()) {
    	   $errors['file_1'] .= 'File is too big ('.$totalsize.' bytes). Max '.$cfg->getMaxFileSize().' bytes allowed.<br />';
    	}

    Find again this code:

    78
    79
    
                    if($_FILES['attachment']['name'] && $cfg->canUploadFiles() && $cfg->allowOnlineAttachments())
                        $ticket->uploadAttachment($_FILES['attachment'],$msgid,'M');

    after the last line of that code above, please insert this following code:

    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    
                $uploaded = '';
                foreach($_FILES as $file) {
                  if ($file['name'] != NULL ) {
                    if(!$cfg->canUploadFileType($file['name'])) {
                      $error = '<b>Error</b>  '.$file['type'].' File type '.$file['name'].' ignored.<br />';
                      echo $error;
                    } else {
                      $dir=$cfg->getUploadDir();
                      $rand=Misc::randCode(16);
                      $file['name']=Format::file_name($file['name']);
                      $upload_file=rtrim($dir,'/').'/'.$rand.'_'.$file['name'];
                      if (file_exists($upload_file)) {
                        $error = '<b>Error</b> '.$file['name'].' - already exist.<br />';
                        echo $error;
                      } elseif ($ticket->insertAttachment($file['tmp_name'],$file['name'],$file['size'],$msgid,'M')!=0) {
                        $uploaded = '<b>Uploaded</b> '.$file['name'].'.<br />';
                        echo $uploaded;
                      } else {
                        $error = '<b>Error</b> Upload failed '.$file['name'].' - Try again.';
                        echo $error;										
                      }
                    }
                  }
                }
  6. Open your open.php file, and find this code:

    23
    
        //Ticket::create...checks for errors..

    then replace with this following code:

    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    
        //Ticket::create...checks for errors..
     
        $totalsize=0;
        foreach($_FILES as $file) {
        	if ($file['name']!='') {
            if(!$cfg->canUploadFileType($file['name'])) {          	
              $errors['file_1'] .= 'Invalid file type [ '.$file['name'].' ].<br />';
            }
          }
          if(!$cfg->allowOnlineAttachments()) {
    	  $errors['file_1'] .= 'File [ '.$file['name'].' ] rejected.<br />';
          }
          $totalsize = $totalsize + $file['size'];
        }
        if($totalsize>$cfg->getMaxFileSize()) {
          $errors['file_1'] .= 'File is too big ('.$totalsize.' bytes). Max '.$cfg->getMaxFileSize().' bytes allowed.<br />';
        }

That’s all. Enjoy the code, everyone!

  • Share/Bookmark
965 views Print This Post Print This Post

  1. frenchy
    November 2nd, 2009 at 05:53 | #1

    Hello,

    Thanks for this good post !!!

    Works fine for client, and I tried to modified also for staff with no success !

    Could you also put code for staff please ?

    thanks again for this good job ;-)

    Best regards.

  2. November 3rd, 2009 at 08:22 | #2

    @frenchy

    You’re welcome. Glad it works at yours.

    Honestly, I have already created the same way on the staff-side. Unfortunately, the result is no success, too. I just wondered the causes why it does not work on the staff-side whereas on the client-side it works. Should you find the solution on the staff-side, please let us know. Thanks.

  3. bhupal
    November 18th, 2009 at 21:48 | #3

    I am getting blank page when doing 3 rd point Open your \include\class.ticket.php

    My osticket is not opening at all

    Please help me

  4. November 19th, 2009 at 08:54 | #4

    @bhupal

    The blank page generally occurs if there is wrong syntax or the modification code you applied to the file has not done correctly. Make sure you have followed the instruction carefully, as the others have successfully implemented it. Good luck!

  5. bhupal
    November 19th, 2009 at 16:00 | #5

    ok thanks it is working Excellent mod

  6. November 19th, 2009 at 20:26 | #6

    Glad to know it works for you! :-)

  7. bhupal
    November 24th, 2009 at 21:43 | #7

    did there is a solution when replying by staff

  8. November 25th, 2009 at 19:29 | #8

    Not yet, bhupal. :-(

  9. Piotr
    January 19th, 2010 at 18:32 | #9

    Hello,

    What about now? Do you had time to create this mod but for Stuff?

  10. June 23rd, 2010 at 03:28 | #10

    hay there. having trouble implementing this, eg it is not working. page loads but i dont see any of the buttons to add the attachment

  11. Balaji
    July 8th, 2010 at 09:17 | #11

    Nice Code….
    It works super in client side,
    Kindly post the code for staff side also.
    Which will be very useful

    THank U

  12. July 10th, 2010 at 23:54 | #12

    @Piotr

    Not yet.

    @andy

    Please re-check your modification code, as the others have successfully implemented it.

    @Balaji

    I hope so, unfortunately I cannot promise you when. ;-)

  1. No trackbacks yet.