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 7
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

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By longtom
      I'm creating this site where a 'motherpage' renders all subpages.  I switched on frontend editing to make editing as easy as possible to end-users. 
      I'm stuck on adding images to the subpages.  The frontend editor suggests to add images to the motherpage (that has no images field on it, nor should it), so it asks for another page to add the images to/from. 
      Is there a way to make the frontend-editor context aware so that it looks for images in the correct subpage? 
      I already tried the 'type C' approach and added <edit field="body" page="id">, but that doesn't seem to do the trick. 
    • By muzzer
      Existing PW site version 2.7.2 core running on php7.1. Site is perhaps 7 yrs old and never misses a beat. Can't speak highly enough about this solid version, but....
      As new php versions are released (v8 in the next year I think?) and each seems to get quicker I'm looking at upgrading to php7.3 or 7.4 and upgrading the site to PW v3.x.
      I've been away from the forums since v3 was released so don't know much about it. I guess it's stable as it's been around for ages now, but what I'm wondering is:
      what are the real advantages of upgrading to v3 for a site which is actively used but with only periodic development. And what are the disadvantages if any? Is there any speed impact (good or bad) in either general site speed under 3.x or admin-use speed/ease of use? any issues with either PW version with newer php versions (>7.1) I should know about? is there any good write-ups/vids about new features etc of v3 compared to v2.7? Thank you
       
    • By rushy
      Hi all
      Continuing my first project where I am creating and manipulating stuff from the frontend. Till now I've always added things like images from the backend, but in this project I need to add and move images from a frontend control. This is a photo album where images are stored in albums, each album being a page containing a Pageimages array in the usual way. So what I want to know is how do I move an image from one album (page) to another in the frontend? I just need some guidance on how to approach this.  I suppose I need to do a copy and delete - but how do I copy an image or images from one page to another? What function should I use to create a new image on an existing page? I include a code snippet from the server side of my delete image request and it works fine. I'd like to implement something similar for a move and upload new image request. 
      Many thanks for any help. Paul 
      <?php namespace ProcessWire; // sanitize inputs as 1-line text $action = $sanitizer->text($input->post('action')); $instr = $sanitizer->text($input->post('input')); $sel = $input->post('selected'); // Expect JSON for image selected image list $selected = json_decode($sel); $nosel = count($selected); $response = array(); // for building JSON response switch($action) { // delete selected images case 'delete': $out = "<p>Deleted $nosel image(s)</p>"; foreach($selected as $item){ $album = $pages->get($item->album); $album->of(false); $out .= "<p>Image {$item->file} from album {$album->title}</p>"; $album->images->delete($item->file); $album->of(true); } $out .= saveUpdatedAlbums($pages, $selected); // add the response message for the delete $response['message'] = $out; break; ...... // save any album that had an image deleted function saveUpdatedAlbums($pages, $selected) { $cur = ''; $out = ''; foreach($selected as $item){ $album = $pages->get($item->album); if($album->id != $cur) { $album->of(false); $album->save('images'); $album->of(true); $cur = $album->id; $out .= "<p>Updated album {$pages->get($cur)->title}</p>"; } } return $out; }  
       
       
    • By hellomoto
      I can't tell what's wrong; my local development version appears just fine, but I copy over the site files and db online and the homepage content is not being contained. This is what it should look like (same site in the same browser, running on my localhost): http://imgur.com/UFZFzrd
      What could be the problem here? Sorry to bring up something so irrelevant to PW here, I just know that you all are a valiant and helpful group, and no one on StackExchange seems to even know what I'm talking about.
      Thanks a lot.
×
×
  • Create New...