<?php
// initialize output;
$output = true;
// valid extensions
$ext_array = array('pdf', 'txt', 'doc', 'docx', 'rtf');
// create unique path for this form submission
$uploadpath = 'assets/form-uploads/';
// Unique file creation
function createUploadFilename($currentPath,$currentFilename) {
// Create unique filename
if(file_exists($currentPath . $currentFilename)) {
$i = 0;
do {
$nF = $i . $currentFilename;
$i++;
} while (file_exists($currentPath . $nF));
}
if(isset($nF)) {
$newFilename = $currentPath . $nF;
} else {
$newFilename = $currentPath . $currentFilename;
}
return $newFilename;
}
// get full path to unique folder
$target_path = $modx->config['base_path'] . $uploadpath;
$modx->log(modX::LOG_LEVEL_ERROR, 'Target path: ' . $target_path );
// get uploaded file names:
$submittedfiles = array_keys($_FILES);
// loop through files
$countFiles = 1;
foreach ($submittedfiles as $sf) {
// Get Filename and make sure its good.
$filename = basename( $_FILES[$sf]['name'] );
$modx->log(modX::LOG_LEVEL_ERROR, 'Filename: ' . $filename);
// Get file's extension
$ext = pathinfo($filename, PATHINFO_EXTENSION);
$ext = mb_strtolower($ext); // case insensitive
$modx->log(modX::LOG_LEVEL_ERROR, 'Extension: ' . $ext );
// is the file name empty (no file uploaded)
if($filename != '') {
// is this the right type of file?
if(in_array($ext, $ext_array)) {
// clean up filename
$filename = $modx->resource->cleanAlias($filename);
$modx->log(modX::LOG_LEVEL_ERROR, 'Cleaned file: ' . $filename );
$filename = date("Y-d-m_") . $filename; // add date
// create directory to move file into if it doesn't exist
if(!file_exists($target_path)) {
mkdir($target_path, 0755, true);
}
// Create unique filename
$myTarget = createUploadFilename($target_path,$filename);
$modx->log(modX::LOG_LEVEL_ERROR, 'Clean and unique filename: ' . $myTarget );
// is the file moved to the proper folder successfully?
if(move_uploaded_file($_FILES[$sf]['tmp_name'], $myTarget)) {
// set a new placeholder with the new full path (if you need it in subsequent hooks)
$modx->setPlaceholder('fi.bestand' . $countFiles, $modx->getOption('site_url') . $uploadpath . $filename);
$modx->log(modX::LOG_LEVEL_ERROR, 'Placeholder set: ' . 'fi.bestand' . $countFiles);
// set the permissions on the file
if (!chmod($myTarget, 0644)) { /*some debug function*/ }
} else {
// File not uploaded
$modx->log(modX::LOG_LEVEL_ERROR, 'Bestand kon niet worden geupload!' );
$errorMsg = 'There was a problem uploading the file.';
$hook->addError($sf, $errorMsg);
$output = false; // generate submission error
}
} else {
// File type not allowed
$modx->log(modX::LOG_LEVEL_ERROR, 'Bestandtype niet toegestaan!');
$errorMsg = 'Type of file not allowed.';
$hook->addError($sf, $errorMsg);
$output = false; // generate submission error
}
// if no file, don't error, but return blank
} else {
$hook->setValue($sf, '');
}
$countFiles++;
}
$modx->log(modX::LOG_LEVEL_ERROR, 'Continue? ' . $output);
$hook->setValue($sf, '');
return $output;
return;
<?php
// initialize output;
$output = true;
// valid extensions
$ext_array = array('pdf', 'txt', 'doc', 'docx', 'rtf');
// create unique path for this form submission
$uploadpath = 'assets/form-uploads/';
// Unique file creation
function createUploadFilename($currentPath, $currentFilename)
{
// Create unique filename
if (file_exists($currentPath . $currentFilename)) {
$i = 0;
do {
$nF = $i . $currentFilename;
$i++;
} while (file_exists($currentPath . $nF));
}
if (isset($nF)) {
$newFilename = $currentPath . $nF;
} else {
$newFilename = $currentPath . $currentFilename;
}
return $newFilename;
}
// get full path to unique folder
$target_path = $modx->config['base_path'] . $uploadpath;
$modx->log(modX::LOG_LEVEL_ERROR, 'Target path: ' . $target_path);
// get uploaded file names:
$submittedfiles = array_keys($_FILES);
// loop through files
$countFiles = 1;
foreach ($submittedfiles as $sf) {
// Get Filename and make sure its good.
$filename = basename($_FILES[$sf]['name']);
$modx->log(modX::LOG_LEVEL_ERROR, 'Filename: ' . $filename);
// Get file's extension
$ext = pathinfo($filename, PATHINFO_EXTENSION);
$ext = mb_strtolower($ext); // case insensitive
$modx->log(modX::LOG_LEVEL_ERROR, 'Extension: ' . $ext);
// is the file name empty (no file uploaded)
if ($filename != '') {
// is this the right type of file?
if (in_array($ext, $ext_array)) {
// clean up filename
$filename = $modx->resource->cleanAlias($filename);
$modx->log(modX::LOG_LEVEL_ERROR, 'Cleaned file: ' . $filename);
$filename = date("Y-d-m_") . $filename; // add date
// create directory to move file into if it doesn't exist
if (!file_exists($target_path)) {
mkdir($target_path, 0755, true);
}
// Create unique filename
$myTarget = createUploadFilename($target_path, $filename);
$modx->log(modX::LOG_LEVEL_ERROR, 'Clean and unique filename: ' . $myTarget);
// is the file moved to the proper folder successfully?
if (move_uploaded_file($_FILES[$sf]['tmp_name'], $myTarget)) {
// set a new placeholder with the new full path (if you need it in subsequent hooks)
$modx->setPlaceholder('fi.bestand' . $countFiles, $modx->getOption('site_url') . $uploadpath . $filename);
$modx->log(modX::LOG_LEVEL_ERROR, 'Placeholder set: ' . 'fi.bestand' . $countFiles);
// set the permissions on the file
if (!chmod($myTarget, 0644)) {
/*some debug function*/
}
} else {
// File not uploaded
$modx->log(modX::LOG_LEVEL_ERROR, 'Bestand kon niet worden geupload!');
$errorMsg = 'There was a problem uploading the file.';
$hook->addError($sf, $errorMsg);
$output = false; // generate submission error
}
} else {
// File type not allowed
$modx->log(modX::LOG_LEVEL_ERROR, 'Bestandtype niet toegestaan!');
$errorMsg = 'Type of file not allowed.';
$hook->addError($sf, $errorMsg);
$output = false; // generate submission error
}
// if no file, don't error, but return blank
} else {
$hook->setValue($sf, '');
}
$countFiles++;
}
$modx->log(modX::LOG_LEVEL_ERROR, 'Continue? ' . $output);
$hook->setValue($sf, '');
return $output;
return;You had a character encoding problem. Here' your code reformatted. It should work now:Thnx for your response But the code shows error again....
<!--?php // initialize output; $output = true; // valid extensions $ext_array = array('pdf', 'txt', 'doc', 'docx', 'rtf'); // create unique path for this form submission $uploadpath = 'assets/form-uploads/'; // Unique file creation function createUploadFilename($currentPath, $currentFilename) { // Create unique filename if (file_exists($currentPath . $currentFilename)) { $i = 0; do { $nF = $i . $currentFilename; $i++; } while (file_exists($currentPath . $nF)); } if (isset($nF)) { $newFilename = $currentPath . $nF; } else { $newFilename = $currentPath . $currentFilename; } return $newFilename; } // get full path to unique folder $target_path = $modx--->config['base_path'] . $uploadpath; $modx->log(modX::LOG_LEVEL_ERROR, 'Target path: ' . $target_path); // get uploaded file names: $submittedfiles = array_keys($_FILES); // loop through files $countFiles = 1; foreach ($submittedfiles as $sf) { // Get Filename and make sure its good. $filename = basename($_FILES[$sf]['name']); $modx->log(modX::LOG_LEVEL_ERROR, 'Filename: ' . $filename); // Get file's extension $ext = pathinfo($filename, PATHINFO_EXTENSION); $ext = mb_strtolower($ext); // case insensitive $modx->log(modX::LOG_LEVEL_ERROR, 'Extension: ' . $ext); // is the file name empty (no file uploaded) if ($filename != '') { // is this the right type of file? if (in_array($ext, $ext_array)) { // clean up filename $filename = $modx->resource->cleanAlias($filename); $modx->log(modX::LOG_LEVEL_ERROR, 'Cleaned file: ' . $filename); $filename = date("Y-d-m_") . $filename; // add date // create directory to move file into if it doesn't exist if (!file_exists($target_path)) { mkdir($target_path, 0755, true); } // Create unique filename $myTarget = createUploadFilename($target_path, $filename); $modx->log(modX::LOG_LEVEL_ERROR, 'Clean and unique filename: ' . $myTarget); // is the file moved to the proper folder successfully? if (move_uploaded_file($_FILES[$sf]['tmp_name'], $myTarget)) { // set a new placeholder with the new full path (if you need it in subsequent hooks) $modx->setPlaceholder('fi.bestand' . $countFiles, $modx->getOption('site_url') . $uploadpath . $filename); $modx->log(modX::LOG_LEVEL_ERROR, 'Placeholder set: ' . 'fi.bestand' . $countFiles); // set the permissions on the file if (!chmod($myTarget, 0644)) { /*some debug function*/ } } else { // File not uploaded $modx->log(modX::LOG_LEVEL_ERROR, 'Bestand kon niet worden geupload!'); $errorMsg = 'There was a problem uploading the file.'; $hook->addError($sf, $errorMsg); $output = false; // generate submission error } } else { // File type not allowed $modx->log(modX::LOG_LEVEL_ERROR, 'Bestandtype niet toegestaan!'); $errorMsg = 'Type of file not allowed.'; $hook->addError($sf, $errorMsg); $output = false; // generate submission error } // if no file, don't error, but return blank } else { $hook->setValue($sf, ''); } $countFiles++; } $modx->log(modX::LOG_LEVEL_ERROR, 'Continue? ' . $output); $hook->setValue($sf, ''); return $output; return;