function upload_img($insert_id, $file, $folder_name) { if ($folder_name == "") { $current_dir = getcwd(); $folder_url = $current_dir . '/uploads/'; } else { $folder = $insert_id; $current_dir = getcwd(); $projects_dir = $current_dir . $folder_name . '/'; //echo $projects_dir; if (!is_dir($projects_dir)) { mkdir($projects_dir); } $folder_url = $projects_dir . $folder; if (!is_dir($folder_url)) { mkdir($folder_url); } } //foreach($formdata as $file) { // replace spaces with underscores if (isset($file['name']) and $file['name']) { $file_details = file_name_details($file['name']); $filename = md5($file_details['name']) . "." . $file_details['ext']; } // assume filetype is false $typeOK = true; // check filetype is ok // if file type ok upload the file if ($typeOK) { // switch based on error code switch ($file['error']) { case 0: // check filename already exists if (!file_exists($folder_url . '/' . $filename)) { // create full filename $full_url = $folder_url . '/' . $filename; $url = $folder . '/' . $filename; $full_name = $filename; // upload the file $success = move_uploaded_file($file['tmp_name'], $full_url); } else { // create unique filename and upload file //ini_set('date.timezone', 'Europe/London'); //$now = date('Y-m-d-His'); $now = time(); $now = $now . "-"; $full_url = $folder_url . '/' . $now . $filename; $full_name = $now . $filename; $url = $folder . '/' . $now . $filename; $success = move_uploaded_file($file['tmp_name'], $full_url); } // if upload was successful if ($success) { // save the url of the file $result['urls'][] = $folder_name . "/" . $full_name; } 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."; } //} return $result; }
if (isset($_POST) && $_POST) { try { NoCSRF::check('csrf_token', $_POST, true, 60 * 10, false); $result = 'CSRF check passed. Form parsed.'; $err_trgr = false; if (isset($_FILES['data']['name']) and $_FILES['data']['name']) { $post_key_arr = array_keys($_FILES['data']['name']); foreach ($post_key_arr as $k => $post_key) { foreach ($_FILES['data'] as $files_key => $files_value) { $upload_files_array[$post_key][$files_key] = $files_value[$post_key]; } } if (isset($upload_files_array) and !empty($upload_files_array)) { foreach ($upload_files_array as $post_key => $upload_array) { if ($upload_array['error'] != 4) { $f_detail = file_name_details($upload_array['name']); if (in_array($f_detail['ext'], $allowed_exts[$post_key])) { $location = get_upload_file_location($post_key, $file_fields[$table_name]); $image_path = upload_img("", $upload_array, $location); $_POST['data'][$post_key] = $image_path['urls'][0]; } else { $er_msg = 'Selected File for <b class="text-danger">' . $post_key . '</b> Field is wrong.<br><div class="well well-sm"><b><i>Allowed File types are</i></b>: <ol>'; foreach ($allowed_exts[$post_key] as $allowed_exts_key => $allowed_exts_val) { $er_msg .= '<li>' . $allowed_exts_val . '</li>'; } $er_msg .= "</ol></div>"; $error_message[] = $er_msg; $err_trgr = true; } } }