public function uploadFiles($folder, $formdata, $itemId = null, $user_defined_filename = null, $thumbnails = array(), $force = false) { // setup dir names absolute and relative $folder_url = WWW_ROOT . $folder; $rel_url = $folder; // create the folder if it does not exist if (!is_dir($folder_url)) { mkdir($folder_url); } // if itemId is set create an item folder if ($itemId) { // set new absolute folder $folder_url = WWW_ROOT . $folder . '/' . $itemId; // set new relative folder $rel_url = $folder . '/' . $itemId; // create directory if (!is_dir($folder_url)) { mkdir($folder_url); } } // list of permitted file types, this is only images but documents can be added $permitted = array('image/gif', 'image/jpeg', 'image/jpg', 'image/pjpeg', 'image/png', 'text/csv', 'application/vnd.ms-excel', 'text/html', 'text/x-comma-separated-values', 'text/comma-separated-values', 'application/octet-stream', 'application/x-csv', 'text/x-csv', 'application/csv', 'application/excel', 'application/vnd.msexcel', 'text/plain', 'application/pdf'); // loop through and deal with the files // error_log("file".print_r($formdata,true)); foreach ($formdata as $file) { // replace spaces with underscores $filename = str_replace(' ', '_', $file['name']); //error_log("file".print_r($file,true)); if (!empty($user_defined_filename)) { $filename = $user_defined_filename; } // assume filetype is false $typeOK = false; // check filetype is ok foreach ($permitted as $type) { if ($type == $file['type']) { $typeOK = true; break; } } // if file type ok upload the file // error_log("tyrp".$typeOK); if ($typeOK) { // switch based on error code switch ($file['error']) { case 0: // check filename already exists if (!file_exists($folder_url . '/' . $filename) || $force) { // create full filename $full_url = $folder_url . '/' . $filename; $url = $rel_url . '/' . $filename; // upload the file $success = move_uploaded_file($file['tmp_name'], $url); } else { // create unique filename and upload file ini_set('date.timezone', 'Asia/Kolkata'); $now = date('Y-m-d-His'); $filename = $now . $filename; $full_url = $folder_url . '/' . $filename; $url = $rel_url . '/' . $filename; $success = move_uploaded_file($file['tmp_name'], $url); } // if upload was successful if ($success) { // save the url of the file foreach ($thumbnails as $thumbnail) { $file = explode(".", $filename); $file = $folder_url . "/" . $file[0] . "_" . $thumbnail . "." . $file[1]; $ThumbnailGenerator = new ImageResize(); $ThumbnailGenerator->GenerateThumbnail($full_url, $file, $thumbnail, 0, 0.8); } $result['urls'][] = $url; } else { $result['errors'][] = "Error uploaded {$filename}. Please try again."; } break; case 3: // an error occured $result['errors'][] = "Error uploading {$filename}. Please try again."; break; default: // an error occured $result['errors'][] = "System error uploading {$filename}. Contact webmaster."; break; } } elseif ($file['error'] == 4) { // no file was selected for upload $result['nofiles'][] = "No file Selected"; } else { // unacceptable file type $result['errors'][] = "{$filename} cannot be uploaded. Acceptable file types: gif, jpg, png,csv."; } } return $result; }