<?php // initialize output; $output = true; // valid extensions $ext_array = array('pdf', 'txt', 'doc', 'docx', 'rtf'); // create unique path for this form submission $uploadpath = 'assets/manuscripts/'; // you can create some logic to automatically // generate some type of folder structure here. // the path that you specify will automatically // be created by the script if it doesn't already // exist. // Function to create unique filename 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; // 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 file name and make unique $filename = mb_strtolower($filename); // to lowercase $filename = str_replace(' ', '_', $filename); // spaces to underscores $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); } // full path to new file $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, $myTarget); $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); return $output;
This question has been answered by m.engel. See the first response.
$filename = $modx->resource->cleanAlias($filename);
$hook->setValue($sf, '');
$hook->setValue($sf, '');
Hi,
Im trying the same thing. can you share the working script? that would help me a lot:)
Tnx
RDG
<?php // initialize output; $output = true; // valid extensions $ext_array = array('pdf', 'txt', 'doc', 'docx', 'rtf'); // create unique path for this form submission $uploadpath = 'assets/manuscripts/'; // 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;