Jump to content

PW3 - Frontend File Upload


ridgedale
 Share

Recommended Posts

I've been trying to work out how I can provide a frontend file upload (whatever file format the user chooses to upload - albeit not executables!) for registered users to be able to create pages/posts and upload any file they wish so they can create a link to the file so it can either be downloaded or displayed.

Everything I've read so far appears to relate to just images and uploading and adding images is already covered using the images field.

Any thoughts/guidance would be a appreciated.

 

Link to comment
Share on other sites

Here's a working example for you:

<?php namespace ProcessWire;

$showForm = true;

if ($input->post->upload) {
    $tempDir = wire()->files->tempDir('userUploads')->get();

    $uploaded = (new WireUpload('uploadedFile')) // same as form field name
        ->setValidExtensions(['txt', 'png', 'jpg', 'pdf'])
        ->setMaxFiles(1) // remove this to allow multiple files
        ->setMaxFileSize(10 * pow(2, 20))// 10MB
        ->setDestinationPath($tempDir)
        ->execute();


    // $page = $pages->get(1234);
    foreach ($uploaded as $file) {
        $filePath = $tempDir . $file;
        // $page->files->add($filePath);

        echo $filePath . "<br>";
    }
    // $page->save('files');

    if (count($uploaded)) {
        echo sprintf("Uploaded %d files", count($uploaded));
        $showForm = false;
    }
}


?>
<?php if ($showForm): ?>
    <?php // Adding enctype is crucial!! ?>
    <form method="POST" action="./" enctype="multipart/form-data">
        <label for="uploadedFile">
            Upload:

            <?php // suffix name with [] and add multiple attribute if you allow multiple files ?>
            <input type="file" name="uploadedFile[]" id="uploadedFile" multiple>
        </label>
        <div>
            <button name="upload" value="1">Upload</button>
        </div>
    </form>
<?php endif; ?>

 

  • Like 8
Link to comment
Share on other sites

Hi abdus,

Thank you for your reply and .

Could you describe how your upload process works from a registered user's perspective when editing a given page?

I realise also there is a possible solution staring me in the face as I write this reply.

Do you or does anyone else know how the Insert other media button/panel is implemented?

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...