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
make two functions get & set sql & html will need replacing then just pass id the function auto upload i add thumbnails as well
then in a html form
finally download
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
on 2022-01-11 14:29:41
on 2022-01-11 14:29:41