Jump to content

Creating a new Page (with file upload)


jrtderonde
 Share

Recommended Posts

Hi,

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:

<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>
    <div class="row">
        <input type="text" name="projectName" id="projectName" placeholder="Projectnaam *" data-validation="required" />
    </div>
    <div class="row">
        <input type="email" name="emailAddress" id="emailAddress" placeholder="E-mailadres *" data-validation="required" />
    </div>
    <div class="row">
        <textarea name="projectDescription" id="projectDescription" placeholder="Beschrijf uw initiatief *" data-validation="required"></textarea>
    </div>
    <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>
    <div class="row">
        <input type="submit" name="submitEntry" id="submitEntry" value="Insturen" />
    </div>
</form>

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) {
            $new->save();
        } else {
            echo "Dit initiatief bestaat al.";
            exit;
        }

        // 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 = [
                "jpg",
                "jpeg",
                "png",
                "gif"
            ];

            // Do the Wire
            $this->image = new WireUpload($file["image"]["name"]);
            $this->image->setMaxFiles(1);
            $this->image->setOverwrite(false);
            $this->image->setDestinationPath($this->path);
            $this->image->setValidExtensions($this->extensions);

            // 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
            $new->image->add($this->fullPath);

            // Unlink the file
            unlink($this->fullPath);

            // Save the page
            $new->save($new->id);

        }

    }

    // 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).

Regards,

Jim 


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

endif;
Link to comment
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
Link to comment
Share on other sites

  • 1 month later...

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() ?

https://processwire.com/blog/posts/august-2014-core-updates-3/#new-download-capabilities-in-wirehttp-class

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

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

  • Like 1
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...