Jump to content

Creating a new Page (with file upload)

Recommended Posts


I'm working on a script that allows users to add a new Page to my installation of ProcessWire. I will explain how this script works below.

  • The user enters some information into a simple form
  • ^ This can include an image
  • The form is validated with jQuery
  • When valid, the form triggers a function through a class that handles the process

I will include some of the code below.


<form id="entry" class="entry" method="post" action="./" name="entry" enctype="multipart/form-data">
    <div class="row">
        <input type="text" name="fullName" id="fullName" placeholder="Naam initiatiefhouder *" data-validation="required" />
    <div class="row">
        <input type="text" name="projectName" id="projectName" placeholder="Projectnaam *" data-validation="required" />
    <div class="row">
        <input type="email" name="emailAddress" id="emailAddress" placeholder="E-mailadres *" data-validation="required" />
    <div class="row">
        <textarea name="projectDescription" id="projectDescription" placeholder="Beschrijf uw initiatief *" data-validation="required"></textarea>
    <div class="row">
        <input type="file" id="image" name="image" data-validation="mime size" data-validation-allowing="jpg, jpeg, png, gif" data-validation-max-size="2M" />
    <div class="row">
        <input type="submit" name="submitEntry" id="submitEntry" value="Insturen" />

So basically the "image" field is the field that allows the user to add a file (jpg, jpeg, png, gif - max 2mb).

Now, when all the information is valid it will post this form through the following function.

public function pushEntry ($input, $file) {

    // Unset the array
    $this->array = [];

    // If there is any data
    if ($input) {

        // Get some of the variables needed
        $this->name = $this->sanitizer->text($input->fullName);
        $this->email = $this->sanitizer->email($input->emailAddress);
        $this->project = $this->sanitizer->text($input->projectName);
        $this->description = $this->sanitizer->text($input->projectDescription);

        // Get the date
        $this->date = date("d-m-Y H:i:s");

        // Generate the title
        $this->title = "$this->project - $this->name";

        $this->duplicate = $this->pages->find("title=$this->title");

        // Generate the array
        $this->array = [
            "title" => $this->title,
            "fullName" => $this->name,
            "emailAddress" => $this->email,
            "projectName" => $this->project,
            "projectDescription" => $this->description,
            "date" => $this->date

        // Create the page
        $new = new Page();
        $new->template = $this->templates->get("entry"); 
        $new->parent = $this->pages->get("/entries/");

        foreach ($this->array as $key => $value) {

            // Foreach key and value, create a new page
            $new->$key = $value;


        // Check if there is any duplicates
        if ($this->duplicate->count() == 0) {
        } else {
            echo "Dit initiatief bestaat al.";

        // If there is an image
        if (!empty($file["image"]["name"])) {

            // Some variables
            $this->path = $this->config->paths->assets . "files/$new->id";

            // Set the extensions
            $this->extensions = [

            // Do the Wire
            $this->image = new WireUpload($file["image"]["name"]);

            // Execute and check for errors
            $files = $this->image->execute();

            // Add the image to the page
            $this->fileName = $file["image"]["name"];
            $this->fullPath = $this->path ."/". $this->fileName;

            // Move the file

            // Unlink the file

            // Save the page



    // Return the array
    return $this->array;


The script adds a page to my installation of ProcessWire perfectly; except for the file upload. I encounter the following errors;

Warning: unlink(C:/wamp/www/gezondsteregio/site/assets/files/1135/dekkleden_header.1919x301.jpg): No such file or directory in C:\wamp\www\gezondsteregio\site\templates\controllers\home.php on line 106

Warning: filemtime(): stat failed for C:/wamp/www/gezondsteregio/site/assets/files/1135/dekkleden_header.1919x301.jpg in C:\wamp\www\gezondsteregio\wire\core\Pagefile.php on line 324

Warning: filemtime(): stat failed for C:/wamp/www/gezondsteregio/site/assets/files/1135/dekkleden_header.1919x301.jpg in C:\wamp\www\gezondsteregio\wire\core\Pagefile.php on line 324

I am really curious if someone can help me with this problem. I have tried this script on both my localhost (regular Apache) and my webhost (Fast CGI).



Oh, and I use the following script to trigger the function.

$entry = new Entry();

// If there is a post
if ($input->post->submitEntry && !empty($input->post->fullName) && !empty($input->post->emailAddress) && !empty($input->post->projectName) && !empty($input->post->projectDescription)):

    // Push the entry to the controller
    $entry->pushEntry($input->post, $_FILES);


Share this post

Link to post
Share on other sites

Hi @jrtderonde and welcome to the forums.

The first thing I notice is that you are uploading the file directly to the assets/files/page_id/ location. You should upload to a temp directory first, then $new->image->add('full_temp_path'), and then unlink from that temp location.

  • Like 2

Share this post

Link to post
Share on other sites

Hey Adrian, 

Thank you for your quick reply. It worked like a charm! I indeed tried to move the files directly from the user's end to a folder that corresponded to the page id. Knew I was wrong somewhere. Thanks!

  • Like 1

Share this post

Link to post
Share on other sites

Can I pass a remote file path to WireUpload to have it download it using curl?   Or do they need to always be local?

Share this post

Link to post
Share on other sites

Can I pass a remote file path to WireUpload to have it download it using curl?   Or do they need to always be local?

Maybe you are looking for WireHttp::download() ?


"...First it attempts to use cURL..."

Never used it though, just guessing. Sorry if ti is not the one you need.

  • Like 1

Share this post

Link to post
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

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By EyeDentify
      Hello dear PW gurus.

      I have stumbled over a strange error that i all of sudden got when trying to upload an image to a images field on a page.
      There where images allready stored in the field that i wanted to keep, but during the upload the error apear and after that all images are gone from the field and i can´t upload any, i just get the error every time.
      I am running ProcessWire 3.0.153 dev.
      After looking in the assets folder i find the folder for the page and the image files seems to be there including the ones i tried to upload when the error occured.
      But they don´t show up in the images field in the page editor.
      The error reported:
      SQLSTATE[01000]: Warning: 1265 Data truncated for column 'ratio' at row 1 And here is a screenshot of the event:

      The TracyDebugger Error reporting:

      I hope you fine folks could point me in a direction.
      But it seems our old pal set_time_limit() is back.

      Regards, EyeDentify
    • By GradDev
      I have a file field named "resume". I want to delete the file saved in it using the API.
      The field is limited to only 1 upload of file and the return type is set to "Automatic" which means it will return a single object if a file is available.
      Now there are two options that I have tried- delete() and deleteAll().
      $page_to_change = $pages->get("name=xyz"); $page_to_change->resume->deleteAll(); //OR //$page_to_change->resume->delete($page_to_change->resume); $page_to_change->of(false); $page_to_change->save(); Both of these give an error like-
      Error: Exception: Method Pagefile::deleteAll does not exist or is not callable in this context(...) I understand that this error is because these two functions work on a wirearray rather than on a file object.
      But how do I delete a single file?
    • By Spyros
      I'm having a strange issue with the $page->find(), for some reason I'm missing some of the pages from the results. I found then that I was missing all the pages with the same "PAGE NAME". Is it a bug or am I missing something?
      If I change the "PAGE NAME" of one of the missing ones then I'm retrieving the page without any problem.
      Thank you
    • By benbyf
      Hi, Looking to create form elements on a page–some input with a colection of form inputs and the appropriate labels and variables for that input. I've used ProForms in the past and rolled out my own when creating simply one off forms, but I wonder if anyone has found a good way of allowing form creation on page editing so that clients can adhocly make and edit forms?
    • By EyeDentify
      Hello Dear PW Gurus.

      Hope you fair well in these Corona Crisis times.

      Anyhow, i have a problem with a Cryptic error message that shows up when i am trying to Delete images out of a Images field.
      The Error message does say a little but it does not make sense to me why i can´t delete the images because of it.

      Is it some permission issue perhaps?

      I will attach screenshots of it and what i did prior to it.
      I am from sweden so ignore the funny words here and there.

      Hope you all can send me on the right track.
      i am running PW 3.0.139 on this install.


      Step 2: Select the images with the trashcan symbol

      Step 3: Cryptic Error message

      I am scratching my head on this one, have not seen this before.

      Thankfull for all help.
  • Create New...