[[!FormIt?
&hooks=`formit2file,formit2db,email`
&path=`images/assets/syltattoo/`
&extensions=`jpg,JPG,jpeg,JPEG,doc,docx,pdf,txt,png,zip,mov,avi,mpeg,mp4`
&preHooks=`db2formit`
&packagename=`demandes`
&classname=`Demande`
&emailSubject=`Mtutu`
&emailTo=`[email protected]`
]]
<h3>[[!+fi.successMessage]] [[+fi.error.error_message]]</h3>
<div class="wrap"><form action="[[~[[*id]]]]" method="post" enctype="multipart/form-data">
<div id="name"><label for="name">Nom:</label></div>
<div class="controls"><input class="input-block-level" type="text" name="name" value="[[!+fi.name]]" /></div>
<div id="name"><label for="name">Email:</label></div>
<div class="controls"><input class="input-block-level" type="text" name="email" value="[[!+fi.email]]" /></div>
<div id="description"><label for="description">Description:</label></div>
<div class="controls"><input class="input-block-level" type="text" name="description" value="[[!+fi.description]]" /></div>
<div id="name"><label for="name">Partie du corps:</label></div>
<div class="controls"><input class="input-block-level" type="text" name="partie" value="[[!+fi.partie]]" /></div>
<div>
<label for="image">Envoyez une image</label></div>
<input id="image" name="image" type="file" value="[[+fi.image]]" maxlength="100000" />
This question has been answered by Bruno17. See the first response.
<?php
/*
Formit hook to upload a file with certain restrictions:
path - path to upload files to from root
extensions - comma seperated list with extensions to allow uploading
maxsize - maximum filesize in bytes allowed
minsize - minimum filesize in bytes allowed
*/
//function to clean a string
function clean($string) {
$string = str_replace(' ', '-', $string); // Replaces all spaces with hyphens.
return preg_replace('/[^A-Za-z0-9\-]/', '', $string); // Removes special chars.
}
$path; // Path from root that user specifies
$extensions; // allow file extensions
$minsize; //minimum filesize in bytes
$maxsize; // maximum filesize in bytes
//define subdirectory by chose theme
$thema = strtolower($hook->getValue('thema'));
//naam ophalen en netjes maken
$naam = strtolower($hook->getValue('naam'));
$naam = str_replace(' ', '_', $naam);
$naam = clean($naam);
$thema = $thema . '/';
$ext_array = explode(',', $extensions);
// Create path
$basepath = $modx->config['base_path']; // Site root
$target_path = $basepath . $path; // root /assets/upload
// Get Filename and make sure its good.
$filename = basename($_FILES['image']['name']);
// Get files extension
$ext = pathinfo($filename, PATHINFO_EXTENSION);
//make lowercase for comparison
$ext = strtolower($ext);
if ($filename != '') {
// Make filename a good unique filename.
// Make lowercase
$filename = mb_strtolower($filename);
// Replace spaces with _
$filename = str_replace(' ', '_', $filename);
// Add timestamp
$filename = date("Ymdgi") . '_' . $filename;
// Add name
$filename = $naam . '_' . $filename;
// Set final path
$target_path = $target_path . $thema . $filename;
if ($_FILES['image']['size'] > $minsize) {
if ($_FILES['image']['size'] < $maxsize) {
if (in_array($ext, $ext_array)) {
if (move_uploaded_file($_FILES['image']['tmp_name'], $target_path)) {
// Upload successful
$hook->setValue('image', $filename);
return true;
} else {
// File not uploaded
$errorMsg = 'Er is een fout opgetreden, kan image niet uploaden';
$hook->addError('image', $errorMsg);
return false;
}
} else {
// File type not allowed
$errorMsg = 'Dit image is niet in JPEG of TIFF formaat';
$hook->addError('image', $errorMsg);
return false;
}
} else {
//filesize to big
$errorMsg = 'image mag niet groter zijn dan 7MB ';
$hook->addError('image', $errorMsg);
return false;
}
} else {
//filesize too low
$errorMsg = 'image mag niet kleiner zijn dan 2MB';
$hook->addError('image', $errorMsg);
return false;
}
}
$hook->setValue('image', $filename);
return true;