2

I am trying to make an Attachments Module for a website. Here is the Attachments controller script Attachments.php

<?php
  class Attachments extends Controller {
    public function __construct(){

    }

    public function index(){
      $data = [
        'title' => 'Attachments',
        'description' => 'Area for attachments and uploads'
      ];

      $this->loadView('attachments/index', $data);
    }


    public function save(){
      $data = [
        'title' => 'Attachments',
        'description' => 'Area for attachments and uploads'
      ];
        $fileName = $_FILES['file']['name'];
        $fileTmpName = $_FILES['file']['tmp_name'];
        $fileSize = $_FILES['file']['size'];
        $fileError = $_FILES['file']['error'];
        $fileType = $_FILES['file']['type'];
        $fileExt = explode('.', $fileName);
      $fileActualExt = strtolower(end($fileExt));
        $fileNameNew = uniqid('',true).".".$fileActualExt;
        $fileDestination = '/app/attachments/'.$fileNameNew;
       if(move_uploaded_file($fileTmpName, $fileDestination)){
           return true;
       } else{
           return false;
       }
    }

public function remove($files){
      $data = [
        'title' => 'Attachments',
        'description' => 'Area for attachments and uploads'
      ];

            foreach($files as $file){
                $fileName = $file['fileName'];
                $filetoDel = $_SERVER['DOCUMENT_ROOT'] . '/app/attachments/' .$fileName;
            if (file_exists($filetoDel)) {
                unlink($file);
            } else {
                // File not found.
            }

            }
          }

  }

and here is the Attachments View

<?php require APPROOT . '/views/inc/header.php'; ?>
  <div class="jumbotron jumbotron-flud text-center">
   <div class="session col-md-4 mx-auto">
       <?php echo flash('session_message');?>
   </div>
    <div class="container">
    <h1 class="display-3"><?php echo $data['title']; ?></h1>
    <p class="lead"><?php echo $data['description']; ?></p>
    </div>
   <div class="container">
    <div class="row">
       <form id="attatchments" method="post" enctype="multipart/form-data">
           <input id="files" type="file" name="files" />
           <input type="submit" name="save" value="Save"/>


       </form>

    </div>
</div>
<script>
 jQuery(function ($) {
        $('#files').shieldUpload({
            async: {
                enabled: true,
                save: {
                    url: "/attachments/save"
                },
                remove: {
                    url: "/attachments/remove"
                }
            }
        });
    });
      </script>
    </div>

<?php require APPROOT . '/views/inc/footer.php'; ?>

The Script and css and bootstrap are referenced in the /views/inc/header.php /views/inc/footer.php files

The save url should be triggering the function save as the /attachments/save url tells the boot loader to look for Attachments.php and load the function save()

is there a way I can debug this as no file is saved when testing it, however it does seem to run the script...

I've been scratching my head for weeks now trying to figure out how to get this too work. I could just not use the shieldui one and make a simple one however I would like to use the sheildUI one to manage multiple files. Also because I paid for a developers licence so I'd rather use the tools as much as possible.

I have done searches on Youtube and they all seem to favour the code I have under the save function for uploading single files, and from the documentation the shield UI component passes each file one at a time calling the save function on each file?

-- I have managed to get it working as answered below, for uploading files. However I'm using the $_FILES array. Is this correct or should I be passing a file in at a time from the shieldUI. I am unsure how to do this? I am now struggling to find away for removing the files.

Thank You so much in advance for your help.

timmac15
  • 59
  • 3

1 Answers1

0

I stepped away, had a sleep and came back to this. I figured I didn't need the index file on each of the lines. I also needed to use the ../ instead of / in the path to the destination file. My save function in the controller now looks like this:

public function save(){
      $data = [
        'title' => 'Attachments',
        'description' => 'Area for attachments and uploads'
      ];

            foreach($_FILES as $file){
                $fileName = $file['name'];
                $fileTmpName = $file['tmp_name'];
                $fileSize = $file['size'];
                $fileError = $file['error'];
                $fileType = $file['type'];
                $fileExt = explode('.', $fileName);
                $fileActualExt = strtolower(end($fileExt));
                $fileNameNew = uniqid('',true).".".$fileActualExt;
                $fileDestination = '../app/attachments/'.$fileNameNew;
                   if(move_uploaded_file($fileTmpName, $fileDestination)){
                      return true;
                   } else{
                       return false;
                   }

            }

    }
timmac15
  • 59
  • 3