if (empty($modx)) return ''; // initialize output; $output = true; $counter = 1; // valid extensions $ext_array = array('jpg', 'JPG', 'JPEG', 'png'); // create unique path for this form submission $uploadpath = 'assets/avatars/'; // get full path to unique folder $target_path = $modx->config['base_path'] . $uploadpath; $sf = 'avatar'; $file = $_FILES[$sf]; // Get Filename and make sure its good. $filename = basename( $file['name'] ); // Get file's extension $ext = pathinfo($filename, PATHINFO_EXTENSION); $ext = mb_strtolower($ext); // case insensitive // is the file name empty (no file uploaded) if($filename != '' && isset($_POST['updpword-btnnn'])) { // is this the right type of file? if(in_array($ext, $ext_array)) { // clean up file name and make unique $filename = 'user'.$modx->user->get('id') . '.' . $ext; // full path to new file $myTarget = $target_path . $filename; $myTargetTemp = $target_path . '_' . $filename; if(move_uploaded_file($file['tmp_name'], $myTargetTemp)) { require_once MODX_CORE_PATH.'model/phpthumb/phpthumb.class.php'; $phpThumb = new phpThumb(); $phpThumb->setSourceFilename($myTargetTemp); $phpThumb->setParameter('w', 150); $phpThumb->setParameter('h', 150); $phpThumb->setParameter('zc', '1'); $phpThumb->setParameter('q', '150'); if ($phpThumb->GenerateThumbnail()) { if ($phpThumb->RenderToFile($myTarget)) { $avas=glob($target_path . '/user'.$modx->user->get('id') . ".*"); if(is_array($avas)) foreach ($avas as $fpath) { if(basename($fpath) != basename($myTarget)) @unlink($fpath); } // set a new placeholder with the new full path (if you need it in subsequent hooks) $myFile = $uploadpath . $filename; $hook->setValue($sf,$myFile); // set the permissions on the file if (!chmod($myTarget, 0644)) { /*some debug function*/ } } else { // File not uploaded $errorMsg = 'There was a problem uploading the file.'; $hook->addError($sf, $errorMsg); $output = false; // generate submission error } } } else { // File not uploaded $errorMsg = 'There was a problem uploading the file.'; $hook->addError($sf, $errorMsg); $output = false; // generate submission error } @unlink($myTargetTemp); /* // is the file moved to the proper folder successfully? if(move_uploaded_file($file['tmp_name'], $myTarget)) { // set a new placeholder with the new full path (if you need it in subsequent hooks) $myFile = $uploadpath . $filename; $hook->setValue($sf,$myFile); // set the permissions on the file if (!chmod($myTarget, 0644)) { //some debug function } } else { // File not uploaded $errorMsg = 'There was a problem uploading the file.'; $hook->addError($sf, $errorMsg); $output = false; // generate submission error } */ } else { // File type not allowed $errorMsg = 'Type of file not allowed.'; $hook->addError($sf, $errorMsg); $output = false; // generate submission error } } else { $hook->setValue($sf, ''); } return $output;
<form action='upload.php' method='post'> <input type="file" name="file[]"> <input type="file" name="file[]"> <input type="file" name="file[]"> </form>
foreach($_FILES['file']['name'] as $key => $value) { //some code }