File if ((isset($file_array[$id]['tmp_name'][$i])) && (filesize($file_array[$id]['tmp_name'][$i]) < MAX_FILE_SIZE) && (filesize($file_array[$id]['tmp_name'][$i]))) { // Initial value $ext_ok = 1; // Check extension $ext = strtolower(substr(strrchr($file_array[$id]['name'][$i], "."), 1)); if (is_array($allowed_ext)) { if (!in_array($ext, $allowed_ext)) { $ext_ok = 0; } } if ($ext_ok) { $file = array(); // Retrieve file content $fp = fopen($file_array[$id]['tmp_name'][$i], 'r'); $file['document'] = fread($fp, filesize($file_array[$id]['tmp_name'][$i])); fclose($fp); // It seems that Mozilla SeaMonkey and Firefox have a bug that set the MIME type for PDFs // to "text/plain". if( $ext == "pdf" && substr($file['document'], 0, 4) == "%PDF" ) { // huh? bug in Mozilla SeaMonkey? (and perhaps others...) $file['mimetype'] = "application/pdf"; } else { $file['mimetype'] = $file_array[$id]['type'][$i]; } $file['name'] = $file_array[$id]['name'][$i]; $file['tmp_name'] = $file_array[$id]['tmp_name'][$i]; // Store values if (!is_array($files)) { $files = array(); } array_push($files, $file); } else { $error = sprintf(_("The file \"%s\" has an invalid extension"), $file_array[$id]['name'][$i]) . " (" . implode(", ", $allowed_ext) . ")."; } } else { $error = sprintf(_("The file \"%s\" has an invalid size."), $file_array[$id]['name'][$i]); } break; case 1: $error = sprintf(_("The file \"%s\" is bigger than %d bytes."), $file_array[$id]['name'][$i], ini_get("upload_max_filesize")); break; case 2: $error = sprintf(_("The file \"%s\" is too big."), $file_array[$id]['name'][$i]); break; case 3: $error = sprintf(_("The file \"%s\" is partly received."), $file_array[$id]['name'][$i]); break; case 4: $error = sprintf(_("The file \"%s\" does not exist."), $file_array[$id]['name'][$i]); break; default: $error = sprintf(_("The file \"%s\" has an undefined error."), $file_array[$id]['name'][$i]); break; } if (strlen($error)) { // Add LF/CR to error handling? if (isset($_PAGE_INFO['errormsg']['text'])) { $_PAGE_INFO['errormsg']['text'] .= "

"; } // Define error message $_PAGE_INFO['errormsg']['text'] .= $error; // Show alert $_PAGE_INFO['errormsg']['type'] = "alert"; } } } return $files; } /** * download document * * Inputs: * - $file_id file db id, or array containing document data * */ function download_document($file_id, $doc_table = NULL, $inline = FALSE) { if( is_numeric($file_id) && !is_null($doc_table) ) { // Fetch file info $file = db_fetch_file($file_id, $doc_table, TRUE); } else if( is_array($file_id) ) { $file = $file_id; } else { trigger_error("Bad function call", E_USER_ERROR); return FALSE; } if( $file === FALSE || !$file['document'] ) { DBG("File does not exist; id=" . $file_id . " in table \"" . $doc_table . "\""); $_PAGE_INFO['errormsg']['type'] = "alert"; $_PAGE_INFO['errormsg']['text'] = _("File does not exist"); return FALSE; } // Download file (pop-up open/save as) header("Pragma: public"); header("Expires: 0"); header("Cache-Control: private"); header("Content-length: " . strlen($file['document']) . ""); header("Content-transfer-encoding: binary"); header("Content-type: " . $file['mimetype'] . ""); if ($inline) { header("Content-disposition: inline; target:_blank; filename=\"" . $file['filename'] . "\""); } else { header("Content-disposition: attachment; filename=\"" . $file['filename'] . "\""); } echo $file['document']; return TRUE; } /** * download document header (split up download function!) * * Inputs: * - $mimetype File mimetype * - $filename Filename * - $action Download/write * */ function download_document_header($mimetype, $filename, $action = "download", $handle = "temp_file", $inline = FALSE) { GLOBAL $_PAGE_INFO; if ($action == "download") { header("Pragma: public"); header("Expires: 0"); header("Cache-Control: private"); header("Content-transfer-encoding: binary"); header("Content-type: " . $mimetype . ""); if ($inline) { header("Content-disposition: inline; target:_blank; filename=\"" . $filename . "\""); } else { header("Content-disposition: attachment; filename=\"" . $filename . "\""); } } else { // Initial values $file = RemoveExtension($filename); $ext = GetExtension($filename); $counter = 0; $valid_file = 0; do { $tmp = session_save_path() . "/" . $file; if ($counter) { $tmp .= "(" . $counter .")"; } $tmp .= $ext; if (file_exists($tmp)) { $valid_file = 0; $counter++; } else { $valid_file = 1; } } while(!$valid_file); // Store filename $_PAGE_INFO[$handle] = $tmp; $fh = fopen($_PAGE_INFO[$handle], "w"); fclose($fh); } } /** * download document data * * Inputs: * - $data Data * - $action Download/write * */ function download_document_data($data, $action = "download", $handle = "temp_file") { GLOBAL $_PAGE_INFO; if ($action == "download") { echo $data; } else { if ((isset($_PAGE_INFO[$handle])) && (file_exists($_PAGE_INFO[$handle]))) { $fh = fopen($_PAGE_INFO[$handle], "a"); fwrite($fh, $data); fclose($fh); } } } ?>