Login
Username: Password:
Remember me

[Sign Up] / [Recover Password]

Theme / Language

Popular
New Torrents
new name
Size: 18.40 MB
Most active
new name
S: 74 L: 3
New Posters
attachments
Controls ·  Search New posts · Mark unread

You are in: Forum / Forum / M-BiTsy / M-BiTsy Support

2022-01-11 14:28:53
heres the code i used for attachments - very different from v3 but easy to run through just change all sql and look out for paths

sql
CREATE TABLE `attachments` (
 `id` int(11)  UNSIGNED NOT NULL AUTO_INCREMENT,
 `content_id` int(10) UNSIGNED NOT NULL,
 `view_count` int(10) UNSIGNED NOT NULL DEFAULT 0,
 `user_id` int(10) UNSIGNED NOT NULL,
 `upload_date` int(10) UNSIGNED NOT NULL,
 `filename` varchar(100) NOT NULL,
 `file_size` int(10) UNSIGNED NOT NULL,
 `file_hash` varchar(50) NOT NULL,
 `attach_count` int(10) UNSIGNED NOT NULL DEFAULT 0,
 `content_type` varchar(10) NOT NULL DEFAULT 'post',
 `topicid` int(10) UNSIGNED NOT NULL,
 PRIMARY KEY (`id`)
) ENGINE=MyISAM;


make two functions get & set sql & html will need replacing then just pass id the function auto upload i add thumbnails as well

function get_attachment($id)
{
   $sql = DB::raw('attachments', '*', ['content_id' =>$id]);
   if ($sql->rowCount() != 0) {
       foreach ($sql as $row7) {
           print("<br><br> <b>$row7[filename]</b><br>");
           $extension = substr($row7['filename'], -3);
           if ($extension == 'zip') {
               $daimage = UPLOADDIR . "/attachment/$row7[file_hash].data";
               if (file_exists($daimage)) {
                   print(" <a class='btn btn-sm ttbtn' href=\"" . URLROOT . "/download/attachment?id=$row7[id]&hash=" . rawurlencode($row7["file_hash"]) . "\"><i class='fa fa-file-archive-o tticon' ></i>Download</a><br>");
               } else {
                   print("no zip<br>");
               }
           } else {
               $daimage = "uploads/thumbnail/$row7[file_hash].jpg";
               if (file_exists($daimage)) { ?>
                   <!-- Trigger/Open The Model -->
                   <img id="myBtn" src='<?php echo data_uri($daimage, $row7['filename']); ?>' height='110px' width='110px' border='0' alt=''  data-toggle="modal" data-target="#myModal-<?=$daimage;?>">
                   <!-- The Modal -->
                   <div id="myModal-<?=$daimage;?>" class="modal">
                       <!-- Modal content -->
                       <div class="modal-content">
                           <!-- The Close Button --> <?php
                           $switchimage = UPLOADDIR . "/attachment/$row7[file_hash].data"; ?>
                           <button type="button" class="btn btn-sm ttbtn" data-dismiss="modal"><b>CLOSE</b></button><br><?php
                            echo $row7['filename']; ?><br>
                           <img src='<?php echo data_uri($switchimage, $row7['filename']); ?>' style="width:100%" alt=''>
                       </div>
                   </div> <?php
               } else {
                   print("<a href=\"" . URLROOT . "/download/attachment?id=$row7[id]&hash=" . rawurlencode($row7["file_hash"]) . "\"><img src='" . URLROOT . "/thumb/$row7[file_hash].jpg' height='110px' width='110px' border='0' alt='' /></a><br>");
               }
           }
       }
   }
}

function set_attachmnent($topicid, $postid)
{
   if (isset($_FILES['upfile'])):
       $array = array($_FILES['upfile']['name']);
       $result = array_filter($array);
       foreach ($result as $k => $ar):
           // check if file has one of the following extensions
           $allowedfileExtensions = array('jpg', 'gif', 'png', 'zip');
           if (in_array($_FILES['upfile']['type'][$k], $allowedfileExtensions)) {
               Redirect::autolink(URLROOT . '/forum', "Sorry, only zip, JPG, JPEG, PNG, GIF files are allowed.");
           }
           $sourcePath = $_FILES['upfile']['tmp_name'][$k]; // Storing source path of the file in a variable
           $fileSize = $_FILES['upfile']['size'][$k];
           $fileName = $_FILES['upfile']['name'][$k];
           $extension = substr($fileName, -3);
           $hash = sha1($sourcePath);

           $newfile = $hash . "." . $extension;
           $targetPath = UPLOADDIR . "/attachment/" . $hash . ".data"; // Target path where file is to be stored
           $thumbPath = "uploads/thumbnail/" . $hash . ".jpg"; // Target path where attachment as jpg is to be stored

           if ($extension == 'zip') {
               move_uploaded_file($sourcePath, $targetPath); // Moving Uploaded file
               DB::run("INSERT INTO attachments (content_id, user_id, upload_date, filename, file_size, file_hash, topicid)
                       VALUES (?,?,?,?,?,?,?)",
                       [$postid, Users::get('id'), TimeDate::gmtime(), $fileName, $fileSize, $hash, $topicid]);
           } else {
               if (move_uploaded_file($sourcePath, $targetPath)) { // Moving Uploaded file
                   SimpleThumbnail::create()->image($targetPath)->thumbnail(100)->to($thumbPath);
                   DB::run("INSERT INTO attachments (content_id, user_id, upload_date, filename, file_size, file_hash, topicid)
                           VALUES (?,?,?,?,?,?,?)",
                           [$postid, Users::get('id'), TimeDate::gmtime(), $fileName, $fileSize, $hash, $topicid]);
               }
           }
       endforeach;
   endif;
}


then in a html form
<p class='text-center'><input type="file" name="upfile[]" multiple></p>


finally download
        $id = (int) Input::get("id");
       $filename = Input::get("hash");

       $fn = UPLOADDIR . "/attachment/$filename.data";
       $sql = DB::select('attachments', '*', ['id'=>$id]);
       $extension = substr($sql['filename'], -3);
       
       if (!file_exists($fn)) {
           Redirect::autolink($_SERVER['HTTP_REFERER'], "The file $filename does not exists");
       } else {
           header('Content-Disposition: attachment; filename="' . $sql['filename'] . '"');
           header('Content-Length: ' . filesize($fn));
           header("Content-Type: application/$extension");
           readfile($fn);
       }
Last edited by M-jay warn on 2022-01-11 14:29:41
2022-01-11 20:31:46
Thanks. Imma check this out. I never really thought about it until you did it here on your site, and it got me thinking.
TTv3 Micro Edition
Ubuntu 20.04.6 LTS Server
Mysql Version: 10.3.39-MariaDB-0ubuntu0.20.04.2
PHP Version: 7.4.3-4ubuntu2.20
Apache Version: Apache/2.4.59 (Ubuntu)
Users online

No users online

Search



Statistics

    Torrents

  • Tracking: 3
  • Todays torrents: 0
  • Seeders: 0
  • Leechers: 0
  • Peers: 0
  • Downloaded: 89.96 GB
  • Uploaded: 9.86 GB

  • Members

  • We have: 838
  • New today: 0
  • Guest today: 9

  • Online

  • Total online: 9
  • Members: 0
  • Guests online: 9
  • Comments: 3
  • Messages: 1,728