Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2026-03-27 22:30:06

Adams
Plugin Author
Registered: 2024-08-30
Posts: 69

upload file inside plugin

Hello,
I have this code inside my plugin,

<div>                                             
<form name="myuploadform" method="post" enctype="multipart/form-data">
    Select file to upload:
    <input type="file" name="fileToUpload" id="fileToUpload">
   <i class="fa fa fa-globe"></i> <input class="link-button" type="submit" value="Upload File" name="uploadsubmit" id="uploadsubmit">
</form>
</div>

and

public function hw_panel($evt, $stp)
    {

        $available_steps = array(
           'list' => true,
        );

        if (!$stp or !bouncer($stp, $available_steps)) {
            $stp = 'list';
        }

if (isset($_POST["uploadsubmit"])) 
{
    $target_dir = "tmp/";
    echo realpath("tmp/"); // correct path

    $fileName = basename($_FILES["fileToUpload"]["name"]);
    $target_file = $target_dir . $fileName;
    $uploadOk = 1;


    if (file_exists($target_file)) {
        echo "Sorry, file already exists.";
        $uploadOk = 0;
    }

    if ($uploadOk == 0) {
        echo "Sorry, your file was not uploaded.";
    } else {
        if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
            echo "The file ". htmlspecialchars($fileName). " has been uploaded.";
        } else {
            echo "Sorry, there was an error uploading your file.";
        }
    }
 }  
        $this->$stp();
    }

The issue I’m having is that $_FILES["fileToUpload"] returns null, no files (I’m sure the file is selected).
any idea, what I’m missing, what call back I need to put the code somewhere else ?
BTW: same code outside the plugin works just fine.

Thank you

Offline

#2 2026-03-27 23:19:20

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 12,603
Website GitHub

Re: upload file inside plugin

Depending on how you register your callback or handle it via bouncer() I’m surprised it runs at all. The “list” call is expecting a CSRF token to be passed too, so you’ll need to add form_token() to your form.

Other things to check:

  • The file size doesn’t exceed your Txp prefs value for max file size.
  • The file size doesn’t exceed the php.ini max size.
  • The POSTed payload doesn’t exceed the max size.
  • There aren’t any server-level modules installed that interfere with it (e.g. suhosin).
  • All.permissions are correct.

The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.

Hire Txp Builders – finely-crafted code, design and Txp

Offline

Board footer

Powered by FluxBB