Issue with passing a $_POST value within function

Post Reply
freya
Jr. Bludit
Posts: 1
Joined: Sat Dec 11, 2021 10:59 pm

Hello,

THank you for this CMS ! I chose it for my new project and as I needed a simple file upload plugin (to upload to a specific folder) I tried to create one even if I'm not very with PHP.

I do have an issue though, a $_POST variable doesn't seem to pass within the function...

Here is my code:

Code: Select all

<?php
class pluginHello extends Plugin {
    
  // init plugin
  public function init() {
    $this->dbFields = array(
      'gallery-title' => ''
    );

  }

  public function adminBodyEnd(){
    global $security, $url, $L;
    if (!$url->slug() == $this->pluginSlug()) {
      return false;
    }
    

  }

  // Shortcut on sidebar
  public function adminSidebar() {
    global $login, $L;
    if ($login->role() === 'admin' || $login->role() === 'author') {
      return '<a class="nav-link" href="'.$this->pluginUrl().'">Galerie test</a>';
    } else {
      return '';
    }
  }

  public function adminHead(){
    global $url;  
    if (!$url->slug() == $this->pluginSlug()) {
      return false;
    }
    
    $dir = 'abcde';  // set your gallery folder name
    
    
    if(isset($_GET["del"]))
    {
        unlink($dir."/".$_GET["del"]);
    }

    
    if(isset($_POST["fileupload"]))
    { echo "yep";
        $dirfile = $dir.basename( $_FILES["file"]["name"]);     
        if(move_uploaded_file($_FILES["file"]["tmp_name"], $dirfile)) {  
            return "File uploaded successfully!";  
        } else{  
            return "Sorry, file not uploaded, please try again!";  
        }  
    }
    
  }


  // config form
  public function form() {
    global $L, $staticPages;

    $dir = 'abcde';  // set your gallery folder name

    if (is_dir($dir)){
      if ($dh = opendir($dir)){
        while (($file = readdir($dh)) !== false){
           if($file=="." OR $file==".."){} else { $text = '
         
              <div class="col-md-3">
              <img src="http://fannyarles.com/Estelle-V/'.$dir.'/'.$file.'" width="100%" class="img-thumbnail">
              <a href="?del='.$file.'" onclick="return confirm(\'Are you sure you want to delete this item?\');"> Delete </a>
              </div>';
          }
        }
        closedir($dh);
      }
    }
    
    return '<div class="card">
                <div class="card-header">Upload Images</div>
                <div class="card-body">
                    <form action="" method="post" enctype="multipart/form-data">
                        <input type="hidden" value="1" name="fileupload">
                        <div class="form-group row">
                            <label  class="col-md-4 col-form-label text-md-right">Select a File</label>
                            <div class="col-md-6">
                                <input type="file" class="form-control" name="file" required autofocus>
                            </div>
                        </div>  
                        <div class="col-md-6 offset-md-4">
                            <button type="submit" class="btn btn-success">
                                Upload
                            </button>
                           
                        </div>
                </div>
                </form>
            </div>
                
            <div class="col-md-8" style="margin-top:15px;">
                <div class="card">
                    <div class="card-header">My Gallery</div>
                    <div class="card-body">
                         <div class="row">'.$text.'and'.var_dump($_POST['fileupload']).'
                           </div>
                    </div>
                </div>
            </div>
    ';
    
    return $html;
    
    
  }


  /****
   * admin methods
   *****/
  private function pluginUrl(){
    return HTML_PATH_ADMIN_ROOT.'configure-plugin/'.$this->className();
  }

  private function pluginSlug(){
    return 'configure-plugin/'.$this->className();
  }
}
?>
✔️ The form shows correctly on my admin page
✔️ If I put a random file in the folder abcde, it shows below under "my gallery"
✔️ If I click on "delete", the file is correctly deleted in the abcde folder
❌ The thing I can't get to work is the upload part : the $_POST doesn't seem to be passed (when I write "echo 'yes'" within the "isset($_POST" and submit the form, nothing happens).

I did post it to Stackoverflow and someone answered that it should work, so I was wondering if something the CMS/admin panel might create a conflict? Thank you for your insight ! :)
User avatar
Edi
Site Admin
Posts: 2838
Joined: Sun Aug 09, 2015 5:01 pm
Location: Zurich
Has thanked: 19 times
Been thanked: 28 times
Contact:

freya wrote: Sun Dec 12, 2021 8:06 pm I do have an issue though, a $_POST variable doesn't seem to pass within the function...
Does the log file show an error?
Post Reply