Processwire WireUpload
<?php
$page->of(false);
// instantiate the class and give it the name of the HTML field
$u = new WireUpload('your_file_field_name');
// tell it to only accept 1 file
$u->setMaxFiles(1);
// tell it to rename rather than overwrite existing files
$u->setOverwrite(false);
// have it put the files in their final destination. this should be okay since
// the WireUpload class will only create PW compatible filenames
$dest = $config->path('files').$page->id."/";
$u->setDestinationPath($dest);
// tell it what extensions to expect
$u->setValidExtensions(['jpg', 'jpeg', 'gif', 'png']);
// execute() returns an array, so we'll foreach() it even though only 1 file
foreach($u->execute() as $filename) $page->images->add($filename);
// save the page
$page->save();
<?php
/**
* @param u processwire user
*
*/
public function changeProfileImage($u) {
// upload image
$upload = new WireUpload('imageFile');
$upload->setMaxFiles(1);
$upload->setMaxFileSize(50000); // 50kb
$upload->setOverwrite(false);
$upload->setDestinationPath($this->config->paths->files);
$upload->setValidExtensions(['jpg', 'jpeg', 'gif', 'png']);
// add image to the user
$u->of(false);
$u->img->removeAll(); // remove all existing first
$u->save(); // save before adding new
foreach($upload->execute() as $filename) $u->img->add($this->config->paths->files . $filename);
$u->save();
// delete temp image
$files->unlink($this->config->paths->files . $filename);
}