Mys 1.3.6 Changing File Upload Path

Forum
Last Post
Threads / Messages

KittHaven

Member
Member
Joined
May 21, 2013
Messages
386
Points
18
Age
24
Location
Devon, UK
Mysidian Dollar
7,319
This is a very small modification to the mysidia script that allows you to upload your images to subfolders for better organisation! This is made with 1.3.6 but will probably work with older versions, potentially with some tweaking. If anyone reads this and notices any security errors or anything please let me know as I'm not an expert lol.

0. Before we start we need to open the picuploads folder and add in the subfolders that you want. As an example, these are the folders I have for my site. I deleted the default png, gif, and jpg folders as I don't need them. You'll need to come up with your own to suit your own needs. Feel free to use mine as a guide if you want.

/picuploads
  • adopts
    • cat_bean
    • dog_bean
    • dragon_bean
    • mothling_bean
    • pony_bean
  • avatars
  • items
After you have your folders set up we are ready to begin!

1. Open view/admincp/imageview.php and add these lines at the top among the use resource lines.

PHP:
use Resource\GUI\Component\Option;
use Resource\GUI\Container\DropdownList;

They allow you to make yourself a dropdown.

2. Scroll down to around line 74/75, under the section for uploading an image.

Find:

PHP:
$document->addLangvar($this->lang->upload);

And place the following underneath that:

PHP:
$imgDestination = new DropdownList("imgdest");
$imgDestination->add(new Option("Images (base directory)", "picuploads"));
$imgDestination->add(new Option("Adopts (base directory)", "picuploads/adopts"));
$imgDestination->add(new Option("Cat Bean", "picuploads/adopts/cat_bean"));
$imgDestination->add(new Option("Dog Bean", "picuploads/adopts/dog_bean"));
$imgDestination->add(new Option("Dragon Bean", "picuploads/adopts/dragon_bean"));
$imgDestination->add(new Option("Mothling Bean", "picuploads/adopts/mothling_bean"));
$imgDestination->add(new Option("Pony Bean", "picuploads/adopts/pony_bean"));

You can use what I've done to customise it for yourself. Basically you need to copy and past the...

PHP:
$imgDestination->add(new Option("NAME", "folder"));

...lines so you can make as many options as you want. Whenever you add more folders you'll need to remember to add them to the dropdown. It should also work for more than 2 or 3 subfolders. Feel free to skip the 'base directories' that I have at the top, I've only included them for my own use if I don't want to use the specific subfolders contained within.

Anyway! Next step.

3. To add the new dropdown to the form we need to add some lines. Go down a few lines and find this:

PHP:
$imageForm->add(new Comment("Friendly Name: ", FALSE));
$imageForm->add(new TextField("ffn"));

And underneath put this:

PHP:
$imageForm->add(new Comment("Destination: ", FALSE));
$imageForm->add($imgDestination);

Feel free to change 'Destination:' to whatever you want. But these lines basically add the dropdown to the upload form.

Feel free to save at this point and if you refresh your site, it should look something like this:

2023-01-18 15_23_59-Bean Pets and 11 more pages - Personal - Microsoft Edge.png


2023-01-18 15_24_06-Bean Pets and 11 more pages - Personal - Microsoft Edge.png

If you tried to submit right now it wouldn't work properly, though. So onto the next step.

4. Open controller/admincp/imagecontroller.php and find the upload function around line 41. Find this line:

PHP:
$hashedfilename = md5($hashstring) . ".{$filetype}";

And above it put this line:

PHP:
$imgdest = $mysidia->input->post("imgdest");

Then where it says...

PHP:
 $uploaddir = "picuploads/{$filetype}";

...change that to:

PHP:
$uploaddir = "{$imgdest}";

This then changes the upload directory to the image destination you select using the dropdown.

5. And that should be it! Save, refresh your site, and then when you go to upload an image you should have the option to choose a folder for it. As stated at the beginning, just remember to update the dropdown whenever you want new options.

Here's what the database should save if you've done it right:

2023-01-18 15_30_44-localhost _ MySQL _ beanpets _ adopts_filesmap _ phpMyAdmin 5.2.0 and 11 m...png

(My picuploads folder has been renamed to images)

Let me know if this doesn't work for you and I can try to help troubleshoot!

!!If you copy the below files into your own, rename your 'picuploads' folder to 'images' as that's what I use!!

  Spoiler: Full files if you need them/want to check placements 
imagecontroller.php:
PHP:
<?php

namespace Controller\AdminCP;
use Exception;
use Model\DomainModel\UploadedFile;
use Resource\Collection\ArrayList;
use Resource\Core\AppController;
use Resource\Core\Pagination;
use Resource\Core\Registry;
use Resource\Exception\DuplicateIDException;
use Resource\Exception\InvalidActionException;
use Resource\Exception\InvalidIDException;
use Resource\Exception\NoPermissionException;
use Resource\Exception\UnsupportedFileException;
use Resource\Native\MysString;
use Resource\Utility\Date;

class ImageController extends AppController{
   
    public function __construct(){
        parent::__construct();
        $mysidia = Registry::get("mysidia");
        if($mysidia->usergroup->getpermission("canmanagesettings") != "yes"){
            throw new NoPermissionException("You do not have permission to manage promocode.");
        }      
    }

    public function index(){
        $mysidia = Registry::get("mysidia");
        $total = $mysidia->db->select("filesmap")->rowCount();
        $pagination = new Pagination($total, $mysidia->settings->pagination, "admincp/image", $mysidia->input->get("page"));
        $stmt = $mysidia->db->select("filesmap", [], "1 LIMIT {$pagination->getLimit()},{$pagination->getRowsperPage()}");
        $files = new ArrayList;
        while($dto = $stmt->fetchObject()){
            $files->add(new UploadedFile($dto->id, $dto));
        }
        $this->setField("pagination", $pagination);
        $this->setField("files", $files);
    }
   
    public function upload(){
        $mysidia = Registry::get("mysidia");      
        if($mysidia->input->post("submit")){
            $filename = htmlentities($_FILES['uploadedfile']['name']);
            $filesize = htmlentities($_FILES['uploadedfile']['size']);
            $mimetype = htmlentities($_FILES['uploadedfile']['type']);
               
            $allowedExts = ["gif", "jpg", "png"];
            $date = new Date;
            $hashstring = PREFIX . "{$filename}_{$date->format('Y-m-d')}";
            $fileinfo = pathinfo($filename);
            if(!isset($fileinfo["extension"])) throw new UnsupportedFileException("extension");
            $filetype = $fileinfo["extension"];
            if(!in_array($filetype, $allowedExts)) throw new UnsupportedFileException("extension");
            $imgdest = $mysidia->input->post("imgdest");
            $hashedfilename = md5($hashstring) . ".{$filetype}";
            $uploaddir = "{$imgdest}";
           
            if(empty($hashedfilename)) throw new InvalidIDException("file_notexist");
            $existname = "{$uploaddir}/{$hashedfilename}";
            if(file_exists($existname)) throw new DuplicateIDException("file_exist");  
            if($filesize > 156000) throw new UnsupportedFileException("file_size");
            if($mimetype != "image/gif" && $mimetype != "image/jpeg" && $mimetype != "image/png"){
                throw new UnsupportedFileException("file_type");
            }
           
            $imageInfo = getimagesize($_FILES["uploadedfile"]["tmp_name"]);
            if($imageInfo["mime"] != "image/gif" && $imageInfo["mime"] != "image/jpeg" && $imageInfo["mime"] != "image/png"){
                throw new UnsupportedFileException("file_type");
            }                      
            if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $existname) && @file_exists($existname)){
                $this->setField("upload", new MysString("success"));
            }
            else throw new InvalidActionException("error");
           
            $ffn = $mysidia->secure(preg_replace("/[^a-zA-Z0-9\\040.]/", "", $mysidia->input->post("ffn")));
            if(empty($ffn)) throw new UnsupportedFileException("Unknown image");          
            $serverpath = str_replace("../", "", $existname);
            $wwwpath = $mysidia->path->getAbsolute() . $serverpath;
            $mysidia->db->insert("filesmap", ["id" => NULL, "serverpath" => $serverpath, "wwwpath" => $wwwpath, "friendlyname" => $ffn]);
            return;
        }  
    }
   
    public function delete(){
        $mysidia = Registry::get("mysidia");  
        if(!$mysidia->input->post("iid")){
            $this->index();
        }
        else{
            try{
                $file = new UploadedFile($mysidia->input->post("iid"));
                $serverpath = $file->getServerPath();
                 if(!is_writable($file->getServerPath())) throw new NoPermissionException("notwritable");
                unlink($serverpath);
                $mysidia->db->delete("filesmap", "id = '{$file->getID()}'");
            }
            catch(Exception $e){
                throw new InvalidIDException("noid");
            }
        }
    }
   
    public function settings(){
        $mysidia = Registry::get("mysidia");
        if($mysidia->input->post("submit")){
            $mysidia->db->update("settings", ["value" => $mysidia->input->post("enablegd")], "name = 'gdimages'");
            $mysidia->db->update("settings", ["value" => $mysidia->input->post("altbb")], "name = 'usealtbbcode'");
        }
    }  
}

imageview.php:
PHP:
<?php

namespace View\AdminCP;
use Resource\Collection\ArrayList;
use Resource\Core\Registry;
use Resource\Core\View;
use Resource\GUI\Component\Button;
use Resource\GUI\Component\CheckBox;
use Resource\GUI\Component\Option;
use Resource\GUI\Component\FileField;
use Resource\GUI\Component\Image;
use Resource\GUI\Component\RadioButton;
use Resource\GUI\Component\TextField;
use Resource\GUI\Container\DropdownList;
use Resource\GUI\Container\Form;
use Resource\GUI\Container\Table;
use Resource\GUI\Container\TCell;
use Resource\GUI\Container\TRow;
use Resource\GUI\Document\Comment;
use Resource\GUI\Element\Align;

class ImageView extends View{

    public function index(){
        $document = $this->document;
        $document->setTitle($this->lang->manage_title);
        $document->addLangvar($this->lang->manage);
       
        $align = new Align("center", "center");
        $imageForm = new Form("manageform", "delete", "post");
        $imageForm->setAlign($align);
        $files = $this->getField("files");
        $imagesTable = new Table("images", "", FALSE);
        $total = $files->size();
        $numColumns = 3;
        $index = 0;
       
        for($row = 0; $row < ceil($total / $numColumns); $row++){
            $imageRow = new TRow("row{$row}");
            for($column = 0; $column < $numColumns; $column++){
                $file = $files->get($index);
                 $fileImage = new Image($file->getWWWPath());          
                $fileImage->setLineBreak(TRUE);            
                $action = new RadioButton("Delete this Image", "iid", $file->getID());
                $action->setLineBreak(TRUE);
                $cell = new ArrayList;
                $cell->add($fileImage);
                $cell->add($action);
                $imageRow->add(new TCell($cell, "cell{$index}"));
                $index++;
                if($index == $total) break;
            }
            $imagesTable->add($imageRow);
        }

        $imageForm->add($imagesTable);
        $imageForm->add(new Button("Submit", "submit", "submit"));
        $document->add($imageForm);
        $pagination = $this->getField("pagination");
        if($pagination) $document->addLangvar($pagination->showPage());
    }
   
    public function upload(){
        $mysidia = Registry::get("mysidia");
        $document = $this->document;  
       
        if($mysidia->input->post("submit")){
            $document->setTitle($this->lang->uploaded_title);
            $document->addLangvar($this->lang->uploaded);
            $document->addLangvar($this->lang->next);
            return;
        }  
       
        $document->setTitle($this->lang->upload_title);
        $document->addLangvar($this->lang->upload);
        $imgDestination = new DropdownList("imgdest");
        $imgDestination->add(new Option("Images (base directory)", "images"));
        $imgDestination->add(new Option("Adopts (base directory)", "images/adopts"));
        $imgDestination->add(new Option("Cat Bean", "images/adopts/cat_bean"));
        $imgDestination->add(new Option("Dog Bean", "images/adopts/dog_bean"));
        $imgDestination->add(new Option("Dragon Bean", "images/adopts/dragon_bean"));
        $imgDestination->add(new Option("Mothling Bean", "images/adopts/mothling_bean"));
        $imgDestination->add(new Option("Pony Bean", "images/adopts/pony_bean"));
        $imageForm = new Form("uploadform", "upload", "post");
        $imageForm->setEnctype("multipart/form-data");
        $imageForm->add(new Comment("Friendly Name: ", FALSE));
        $imageForm->add(new TextField("ffn"));
        $imageForm->add(new Comment("Destination: ", FALSE));
        $imageForm->add($imgDestination);
        $imageForm->add(new Comment($this->lang->explain));
        $imageForm->add(new Comment("File to Upload: ", FALSE));
        $imageForm->add(new FileField("uploadedfile"));
        $imageForm->add(new Comment("<br><br>"));
        $imageForm->add(new Button("Upload File", "submit", "submit"));
        $document->add($imageForm);
    }
   
    public function delete(){
        $mysidia = Registry::get("mysidia");
        $document = $this->document;
        if(!$mysidia->input->post("iid")) $this->index();
        else{
            $document->setTitle($this->lang->delete_title);
            $document->addLangvar($this->lang->delete);
        }
    }
   
    public function settings(){
        $mysidia = Registry::get("mysidia");
        $document = $this->document;  
        if($mysidia->input->post("submit")){
            $document->setTitle($this->lang->settings_updated_title);
            $document->addLangvar($this->lang->settings_updated);
            return;
        }
       
        $document->setTitle($this->lang->settings_title);
        $document->addLangvar($this->lang->settings);      
        $settingsForm = new Form("settingsform", "settings", "post");      
        $settingsForm->add(new CheckBox(" Enable GD Signature Image for GIF Files", "enablegd", "yes", $mysidia->settings->gdimages));
        $settingsForm->add(new Comment($this->lang->gd_explain));
        $settingsForm->add(new CheckBox(" Enable Alternate Friendly Signature BBCode", "altbb", "yes", $mysidia->settings->usealtbbcode));
        $settingsForm->add(new Comment($this->lang->altbb_explain));
        $settingsForm->add(new Button("Change Settings", "submit", "submit"));
        $document->add($settingsForm);
    }  
}
 

Similar threads

Users who are viewing this thread

  • Forum Contains New Posts
  • Forum Contains No New Posts

Forum statistics

Threads
4,214
Messages
32,630
Members
1,577
Latest member
pudgiehedgie
BETA

Latest Threads

Latest Posts

Top