IT Knowledge Base

~ Without sacrifice, there can be no victory ~

發佈日期:

分類:

,

如何使用JUpload‧將一堆檔案一次性上傳(upload)到網站內

使用PHP上傳(upload)一堆檔案,每次都要一個一個檔案選擇,麻煩又費時,要是那是百多個的檔案,算吧,還得用回ftp。

01. 今次要找的就是一個像Facebook上傳相片檔案的工具。既然Facebook是用Java,那也找一個是用Java的程式,在網上找到一個叫JUpload的程式,正合今次要求。

02. 下載JUpload檔案,解壓後把site資料夾內容,複製到網頁伺服器(web server)上,便可測試。

03. 而今次利用的,是以PHP呼叫JUpload程式,而JUpload程式,亦一早包含需要的程式碼。在site資料夾下,會看到samples.PHP資料夾,內裡有3個檔案,就是今次需要的內容。

04. 把3個檔案(index.php、jupload.php及after_upload.php),複製到網頁伺服器(web server)上的另一jupload資料夾。

05. 在site資料夾下,複製wjhk.jupload.jar檔案到網頁伺服器(web server)上的jupload資料夾內。

06. 編輯index.php檔案。將陣列(array):$appletParameters內的maxFileSize更改為4G,archive更改為wjhk.jupload.jar,新增uploadPolicy設定為PictureUploadPolicy,新增nbFilesPerRequest設定為1,新增width設定為800,新增height設定為600。將陣列(array):$classParameters內的demo_mode更改為false,destdir更改為c:/xampp/tmp,此為上傳資料夾位置。

<?php
session_start(); 
include 'jupload.php';

function handle_uploaded_files($juploadPhpSupportClass, $files) {
return 
"<P>We are in the 'handle_uploaded_files' callback function, in the index.php script. To avoid double coding, we "
. "just call the default behavior of the JUpload PHP class. Just replace this by your code...</P>"
. $juploadPhpSupportClass->defaultAfterUploadManagement();
;
}

$appletParameters = array(
'maxFileSize' => '4G',
'archive' => 'wjhk.jupload.jar',
'afterUploadURL' => 'after_upload.php', 
'sendMD5Sum' => 'true',
'uploadPolicy' => 'PictureUploadPolicy',
'nbFilesPerRequest' => '1',
'width' => '800',
'height' => '600',
'showLogWindow' => 'false',
'debugLevel' => 99
);

$classParameters = array(
'demo_mode' => false,
'allow_subdirs' => true,
'destdir' => 'c:/xampp/tmp'
);

$juploadPhpSupportClass = new JUpload($appletParameters, $classParameters);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>JUpload PHP Upload Test</title>
</head>
<body>
<h2 align="center">JUpload PHP Upload Test</h2>
<div align="center"><!--JUPLOAD_FILES--></div>
<div align="center"><!--JUPLOAD_APPLET--></div>
</body>
</html>

07. jupload.php檔案內容不需要更改。

<?php
class JUpload {
var $appletparams;
var $classparams;
var $files;

public function JUpload($appletparams = array(), $classparams = array()) {
if (gettype($classparams) != 'array')
$this->abort('Invalid type of parameter classparams: Expecting an array');
if (gettype($appletparams) != 'array')
$this->abort('Invalid type of parameter appletparams: Expecting an array');

if (!isset($appletparams['afterUploadURL']))
$appletparams['afterUploadURL'] = $_SERVER['PHP_SELF'] . '?afterupload=1';
if (!isset($appletparams['name']))
$appletparams['name'] = 'JUpload';
if (!isset($appletparams['archive']))
$appletparams['archive'] = 'wjhk.jupload.jar';
if (!isset($appletparams['code']))
$appletparams['code'] = 'wjhk.jupload2.JUploadApplet';
if (!isset($appletparams['debugLevel']))
$appletparams['debugLevel'] = 0;
if (!isset($appletparams['httpUploadParameterType']))
$appletparams['httpUploadParameterType'] = 'array';
if (!isset($appletparams['showLogWindow']))
$appletparams['showLogWindow'] = ($appletparams['debugLevel'] > 0) ? 'true' : 'false';
if (!isset($appletparams['width']))
$appletparams['width'] = 640;
if (!isset($appletparams['height']))
$appletparams['height'] = ($appletparams['showLogWindow'] == 'true') ? 500 : 300;
if (!isset($appletparams['mayscript']))
$appletparams['mayscript'] = 'true';
if (!isset($appletparams['scriptable']))
$appletparams['scriptable'] = 'false';
//if (!isset($appletparams['stringUploadSuccess']))
$appletparams['stringUploadSuccess'] = 'SUCCESS';
//if (!isset($appletparams['stringUploadError']))
$appletparams['stringUploadError'] = 'ERROR: (.*)';
$maxpost = $this->tobytes(ini_get('post_max_size'));
$maxmem = $this->tobytes(ini_get('memory_limit'));
$maxfs = $this->tobytes(ini_get('upload_max_filesize'));
$obd = ini_get('open_basedir');
if (!isset($appletparams['maxChunkSize'])) {
$maxchunk = ($maxpost < $maxmem) ? $maxpost : $maxmem;
$maxchunk = ($maxchunk < $maxfs) ? $maxchunk : $maxfs;
$maxchunk /= 4;
$optchunk = (500000 > $maxchunk) ? $maxchunk : 500000;
$appletparams['maxChunkSize'] = $optchunk;
}
$appletparams['maxChunkSize'] = $this->tobytes($appletparams['maxChunkSize']);
if (!isset($appletparams['maxFileSize']))
$appletparams['maxFileSize'] = $maxfs;
$appletparams['maxFileSize'] = $this->tobytes($appletparams['maxFileSize']);
if (isset($classparams['errormail'])) {
$appletparams['urlToSendErrorTo'] = $_SERVER["PHP_SELF"] . '?errormail';
}

if (!isset($classparams['demo_mode']))
$classparams['demo_mode'] = false;
if ($classparams['demo_mode']) {
$classparams['create_destdir'] = false;
$classparams['allow_subdirs'] = true;
$classparams['allow_zerosized'] = true;
$classparams['duplicate'] = 'overwrite';
}
if (!isset($classparams['debug_php']))
$classparams['debug_php'] = false;
if (!isset($this->classparams['allowed_mime_types']))
$classparams['allowed_mime_types'] = 'all';
if (!isset($this->classparams['allowed_file_extensions']))
$classparams['allowed_file_extensions'] = 'all';
if (!isset($classparams['verbose_errors']))
$classparams['verbose_errors'] = true;
if (!isset($classparams['session_regenerate']))
$classparams['session_regenerate'] = false;
if (!isset($classparams['create_destdir']))
$classparams['create_destdir'] = true;
if (!isset($classparams['allow_subdirs']))
$classparams['allow_subdirs'] = false;
if (!isset($classparams['spaces_in_subdirs']))
$classparams['spaces_in_subdirs'] = false;
if (!isset($classparams['allow_zerosized']))
$classparams['allow_zerosized'] = false;
if (!isset($classparams['duplicate']))
$classparams['duplicate'] = 'rename';
if (!isset($classparams['dirperm']))
$classparams['dirperm'] = 0755;
if (!isset($classparams['fileperm']))
$classparams['fileperm'] = 0644;
if (!isset($classparams['destdir'])) {
if ($obd != '')
$classparams['destdir'] = $obd;
else
$classparams['destdir'] = '/var/tmp/jupload_test';
}
if ($classparams['create_destdir'])
@mkdir($classparams['destdir'], $classparams['dirperm']);
if (!is_dir($classparams['destdir']) && is_writable($classparams['destdir']))
$this->abort('Destination dir not accessible');
if (!isset($classparams['tmp_prefix']))
$classparams['tmp_prefix'] = 'jutmp.';
if (!isset($classparams['var_prefix']))
$classparams['var_prefix'] = 'juvar.';
if (!isset($classparams['jscript_wrapper']))
$classparams['jscript_wrapper'] = 'JUploadSetProperty';
if (!isset($classparams['tag_jscript']))
$classparams['tag_jscript'] = '<!--JUPLOAD_JSCRIPT-->';
if (!isset($classparams['tag_applet']))
$classparams['tag_applet'] = '<!--JUPLOAD_APPLET-->';
if (!isset($classparams['tag_flist']))
$classparams['tag_flist'] = '<!--JUPLOAD_FILES-->';
if (!isset($classparams['http_flist_start']))
$classparams['http_flist_start'] =
"<table border='1'><TR><TH>Filename</TH><TH>file size</TH><TH>Relative path</TH><TH>Full name</TH><TH>md5sum</TH><TH>Specific parameters</TH></TR>";
if (!isset($classparams['http_flist_end']))
$classparams['http_flist_end'] = "</table>\n";
if (!isset($classparams['http_flist_file_before']))
$classparams['http_flist_file_before'] = "<tr><td>";
if (!isset($classparams['http_flist_file_between']))
$classparams['http_flist_file_between'] = "</td><td>";
if (!isset($classparams['http_flist_file_after']))
$classparams['http_flist_file_after'] = "</td></tr>\n";

$this->appletparams = $appletparams;
$this->classparams = $classparams;
$this->page_start();
}

protected function logDebug($function, $msg, $htmlComment=true) {
$output = "[DEBUG] [$function] $msg";
if ($htmlComment) {
echo("<!-- $output -->\r\n");
} else {
echo("$output\r\n");
}
}

protected function logPHPDebug($function, $msg) {
if ($this->classparams['debug_php'] === true) {
$output = "[DEBUG] [$function] ".$this->arrayexpand($msg);
error_log($output);
}
}

private function arrayexpand($array) {
$output = '';
if (is_array($array)) {
foreach ($array as $key => $value) {
$output .= "\n ".$key.' => '.$this->arrayexpand($value);
}
} else {
$output .= $array;
}
return $output;
}

private function tobytes($val) {
$val = trim($val);
$last = strtolower($val{strlen($val)-1});
switch($last) {
case 'g':
$val *= 1024;
case 'm':
$val *= 1024;
case 'k':
$val *= 1024;
}
return $val;
}

private function str_jsinit() {
$N = "\n";
$name = $this->appletparams['name'];
$ret = '<script type="text/javascript">'.$N;
$ret .= '<!--'.$N;
$ret .= 'function '.$this->classparams['jscript_wrapper'].'(name, value) {'.$N;
$ret .= 'document.applets["'.$name.'"] == null || document.applets["'.$name.'"].setProperty(name,value);'.$N;
$ret .= 'document.embeds["'.$name.'"] == null || document.embeds["'.$name.'"].setProperty(name,value);'.$N;
$ret .= '}'.$N;
$ret .= '//-->'.$N;
$ret .= '</script>';
return $ret;
}

private function str_applet() {
$N = "\n";
$params = $this->appletparams;
$ret = '<object classid = "clsid:8AD9C840-044E-11D1-B3E9-00805F499D93"'.$N;
$ret .= 'codebase = "http://java.sun.com/update/1.5.0/jinstall-1_5-windows-i586.cab#Version=5,0,0,3"'.$N;
$ret .= 'width = "'.$params['width'].'"'.$N;
$ret .= 'height = "'.$params['height'].'"'.$N;
$ret .= 'name = "'.$params['name'].'">'.$N;
foreach ($params as $key => $val) {
if ($key != 'width' && $key != 'height')
$ret .= '<param name = "'.$key.'" value = "'.$val.'" />'.$N;
}
$ret .= '<comment>'.$N;
$ret .= '<embed'.$N;
$ret .= 'type = "application/x-java-applet;version=1.5"'.$N;
foreach ($params as $key => $val)
$ret .= ''.$key.' = "'.$val.'"'.$N;
$ret .= 'pluginspage = "http://java.sun.com/products/plugin/index.html#download">'.$N;
$ret .= '<noembed>'.$N;
$ret .= 'Java 1.5 or higher plugin required.'.$N;
$ret .= '</noembed>'.$N;
$ret .= '</embed>'.$N;
$ret .= '</comment>'.$N;
$ret .= '</object>';
return $ret;
}

private function abort($msg = '') {
$this->cleanup();
if ($msg != '')
die(str_replace('(.*)',$msg,$this->appletparams['stringUploadError'])."\n");
exit;
}

private function warning($msg = '') {
$this->cleanup();
if ($msg != '')
echo('WARNING: '.$msg."\n");
echo $this->appletparams['stringUploadSuccess']."\n";
exit;
}

private function cleanup() {
if (isset($_FILES)) {
foreach ($_FILES as $key => $val)
@unlink($val['tmp_name']);
}
@unlink($this->classparams['destdir'].'/'.$this->classparams['tmp_prefix'].session_id());
@unlink($this->classparams['destdir'].'/'.$this->classparams['tmp_prefix'].'tmp'.session_id());
$_SESSION[$this->classparams['var_prefix'].'size'] = 0;
return;
}

private function mkdirp($path) {
$dirs = explode('/', $path);
$path = $this->classparams['destdir'];
foreach ($dirs as $dir) {
$path .= '/'.$dir;
if (!file_exists($path)) {
@mkdir($path, $this->classparams['dirperm']);
}
}
if (!is_dir($path) && is_writable($path))
$this->abort('Destination dir not accessible');
}

private function dstfinal(&$name, &$subdir) {
$name = preg_replace('![`$\/|]!', '_', $name);
if ($this->classparams['allow_subdirs'] && ($subdir != '')) {
$subdir = trim(preg_replace('!\!','/',$subdir),'/');
$subdir = preg_replace('![`$|]!', '_', $subdir);
if (!$this->classparams['spaces_in_subdirs']) {
$subdir = str_replace(' ','_',$subdir);
}
if (!$this->classparams['demo_mode'])
$this->mkdirp($subdir);
$subdir .= '/';
} else {
$subdir = '';
}
$ret = $this->classparams['destdir'].'/'.$subdir.$name;
if (file_exists($ret)) {
if ($this->classparams['duplicate'] == 'overwrite') {
return $ret;
}
if ($this->classparams['duplicate'] == 'reject') {
$this->abort('A file with the same name already exists');
}
if ($this->classparams['duplicate'] == 'warning') {
$this->warning("File $name already exists - rejected");
}
if ($this->classparams['duplicate'] == 'rename') {
$cnt = 1;
$dir = $this->classparams['destdir'].'/'.$subdir;
$ext = strrchr($name, '.');
if ($ext) {
$nameWithoutExtension = substr($name, 0, strlen($name) - strlen($ext));
} else {
$ext = '';
$nameWithoutExtension = $name;
}

$rtry = $dir.$nameWithoutExtension.'.['.$cnt.']'.$ext;
while (file_exists($rtry)) {
$cnt++;
$rtry = $dir.$nameWithoutExtension.'.['.$cnt.']'.$ext;
}
$name = $nameWithoutExtension.'.['.$cnt.']'.$ext;
$ret = $rtry;
}
}
return $ret;
}

public function defaultAfterUploadManagement() {
$flist = '[defaultAfterUploadManagement] Nb uploaded files is: ' . sizeof($this->files);
$flist = $this->classparams['http_flist_start'];
foreach ($this->files as $f) {
$this->logDebug('defaultAfterUploadManagement', "Reading file ${f['name']}");
$flist .= $this->classparams['http_flist_file_before'];
$flist .= $f['name'];
$flist .= $this->classparams['http_flist_file_between'];
$flist .= $f['size'];
$flist .= $this->classparams['http_flist_file_between'];
$flist .= $f['relativePath'];
$flist .= $this->classparams['http_flist_file_between'];
$flist .= $f['fullName'];
$flist .= $this->classparams['http_flist_file_between'];
$flist .= $f['md5sum'];
$addBR = false;
foreach ($f as $key=>$value) {
if ($key != 'name' && $key != 'size' && $key != 'relativePath' && $key != 'fullName' && $key != 'md5sum') {
if ($addBR) {
$flist .= "<br>";
} else {
$flist .= $this->classparams['http_flist_file_between'];
$addBR = true;
}
$flist .= "$key => $value";
}
}
$flist .= $this->classparams['http_flist_file_after'];
}
$flist .= $this->classparams['http_flist_end'];
return $flist;
}

private function generateAppletTag($str) {
$this->logDebug('generateAppletTag', 'Entering function');
$str = preg_replace('/'.$this->classparams['tag_jscript'].'/', $this->str_jsinit(), $str);
return preg_replace('/'.$this->classparams['tag_applet'].'/', $this->str_applet(), $str);
}

public function interceptBeforeUpload($str) {
$this->logDebug('interceptBeforeUpload', 'Entering function');
return $this->generateAppletTag($str);
}

public function interceptAfterUpload($str) {
$this->logDebug('interceptAfterUpload', 'Entering function');
$this->logPHPDebug('interceptAfterUpload', $this->files);

if (count($this->files) > 0) {
if (isset($this->classparams['callbackAfterUploadManagement'])) {
$this->logDebug('interceptAfterUpload', 'Before call of ' .$this->classparams['callbackAfterUploadManagement']);
$strForFListContent = call_user_func($this->classparams['callbackAfterUploadManagement'], $this, $this->files);
} else {
$strForFListContent = $this->defaultAfterUploadManagement();
}
$str = preg_replace('/'.$this->classparams['tag_flist'].'/', $strForFListContent, $str);
}
return $this->generateAppletTag($str);
}

private function receive_debug_log() {
if (isset($_POST['description']) && isset($_POST['log'])) {
$msg = $_POST['log'];
mail($this->classparams['errormail'], $_POST['description'], $msg);
} else {
if (isset($_SERVER['SERVER_ADMIN']))
mail($_SERVER['SERVER_ADMIN'], 'Empty jupload error log',
'An empty log has just been posted.');
$this->logPHPDebug('receive_debug_log', 'Empty error log received');
}
exit;
}

private function receive_uploaded_files() {
$this->logDebug('receive_uploaded_files', 'Entering POST management');

if (session_id() == '') { session_start(); }
if (!isset($_SESSION[$this->classparams['var_prefix'].'size'])) {
$this->abort('Invalid session (in afterupload, POST, check of size)');
}
if (!isset($_SESSION[$this->classparams['var_prefix'].'files'])) {
$this->abort('Invalid session (in afterupload, POST, check of files)');
}
$this->files = $_SESSION[$this->classparams['var_prefix'].'files'];
if (!is_array($this->files)) {
$this->abort('Invalid session (in afterupload, POST, is_array(files))');
}
if ($this->appletParameters['sendMD5Sum'] && !isset($_POST['md5sum'])) {
$this->abort('Required POST variable md5sum is missing');
}
$cnt = 0;
foreach ($_FILES as $key => $value) {
if (isset($files_data)) {
unset($files_data);
}
$jupart = (isset($_POST['jupart'])) ? (int)$_POST['jupart'] : 0;
$jufinal = (isset($_POST['jufinal'])) ? (int)$_POST['jufinal'] : 1;
$relpaths = (isset($_POST['relpathinfo'])) ? $_POST['relpathinfo'] : null;
$md5sums = (isset($_POST['md5sum'])) ? $_POST['md5sum'] : null;
$mimetypes = (isset($_POST['mimetype'])) ? $_POST['mimetype'] : null;

if (gettype($relpaths) == 'string') {
$relpaths = array($relpaths);
}
if (gettype($md5sums) == 'string') {
$md5sums = array($md5sums);
}
if ($this->appletParameters['sendMD5Sum'] && !is_array($md5sums)) {
$this->abort('Expecting an array of MD5 checksums');
}
if (!is_array($relpaths)) {
$this->abort('Expecting an array of relative paths');
}
if (!is_array($mimetypes)) {
$this->abort('Expecting an array of MIME types');
}
if (isset($this->classparams['allowed_mime_types']) && is_array($this->classparams['allowed_mime_types'])) {
if (!in_array($mimetypes[$cnt], $this->classparams['allowed_mime_types'])) {
$this->abort('MIME type '.$mimetypes[$cnt].' not allowed');
}
}
if (isset($this->classparams['allowed_file_extensions']) && is_array($this->classparams['allowed_file_extensions'])) {
$fileExtension = substr(strrchr($value['name'][$cnt], "."), 1);
if (!in_array($fileExtension, $this->classparams['allowed_file_extensions'])) {
$this->abort('File extension '.$fileExtension.' not allowed');
}
}

$dstdir = $this->classparams['destdir'];
$dstname = $dstdir.'/'.$this->classparams['tmp_prefix'].session_id();
$tmpname = $dstdir.'/'.$this->classparams['tmp_prefix'].'tmp'.session_id();

$files_data['name'] = $value['name'][$cnt];
$files_data['size']= 'not calculated yet';
$files_data['tmp_name'] = $value['tmp_name'][$cnt];
$files_data['error'] = $value['error'][$cnt];
$files_data['relativePath'] = $relpaths[$cnt];
$files_data['md5sum'] = $md5sums[$cnt];
$files_data['mimetype'] = $mimetypes[$cnt];

if (!move_uploaded_file($files_data['tmp_name'], $tmpname)) {
if ($classparams['verbose_errors']) {
$this->abort("Unable to move uploaded file (from ${files_data['tmp_name']} to $tmpname)");
} else {
trigger_error("Unable to move uploaded file (from ${files_data['tmp_name']} to $tmpname)",E_USER_WARNING);
$this->abort("Unable to move uploaded file");
}
}

if ($this->classparams['demo_mode']) {
if ($jufinal || (!$jupart)) {
if ($jupart) {
$files_data['size']= ($jupart-1) * $this->appletparams['maxChunkSize'] + filesize($tmpname);
} else {
$files_data['size']= filesize($tmpname);
}
$files_data['fullName']= 'Demo mode<BR>No file storing';
array_push($this->files, $files_data);
}
unlink($tmpname);
$cnt++;
continue;
}

if ($jupart) {
$len = filesize($tmpname);
$_SESSION[$this->classparams['var_prefix'].'size'] += $len;
if ($len > 0) {
$src = fopen($tmpname, 'rb');
$dst = fopen($dstname, ($jupart == 1) ? 'wb' : 'ab');
while ($len > 0) {
$rlen = ($len > 8192) ? 8192 : $len;
$buf = fread($src, $rlen);
if (!$buf) {
fclose($src);
fclose($dst);
unlink($dstname);
$this->abort('read IO error');
}
if (!fwrite($dst, $buf, $rlen)) {
fclose($src);
fclose($dst);
unlink($dstname);
$this->abort('write IO error');
}
$len -= $rlen;
}
fclose($src);
fclose($dst);
unlink($tmpname);
}
if ($jufinal) {
$dlen = filesize($dstname);
if ($dlen != $_SESSION[$this->classparams['var_prefix'].'size'])
$this->abort('file size mismatch');
if ($this->appletParameters['sendMD5Sum'] ) {
if ($md5sums[$cnt] != md5_file($dstname))
$this->abort('MD5 checksum mismatch');
}
if (($dlen > 0) || $this->classparams['allow_zerosized']) {
$dstfinal = $this->dstfinal($files_data['name'],$files_data['relativePath']);
if (!rename($dstname, $dstfinal))
$this->abort('rename IO error');
if (!chmod($dstfinal, $this->classparams['fileperm']))
$this->abort('chmod IO error');
$files_data['size']= filesize($dstfinal);
$files_data['fullName']= $dstfinal;
$files_data['path']= dirname($dstfinal);
array_push($this->files, $files_data);
} else {
unlink($dstname);
}
$_SESSION[$this->classparams['var_prefix'].'size'] = 0;
}
} else {
if ($md5sums[$cnt] != md5_file($tmpname))
$this->abort('MD5 checksum mismatch');
$dstfinal = $this->dstfinal($files_data['name'],$files_data['relativePath']);
if (!rename($tmpname, $dstfinal))
$this->abort('rename IO error');
if (!chmod($dstfinal, $this->classparams['fileperm']))
$this->abort('chmod IO error');
$files_data['size']= filesize($dstfinal);
$files_data['fullName']= $dstfinal;
$files_data['path']= dirname($dstfinal);
array_push($this->files, $files_data);
}
$cnt++;
}

echo $this->appletparams['stringUploadSuccess']."\n";
$_SESSION[$this->classparams['var_prefix'].'files'] = $this->files;
session_write_close();
exit;
}

private function page_start() {
$this->logDebug('page_start', 'Entering function');

if ($_SERVER['REQUEST_METHOD'] == 'HEAD') {
// Nothing to do
} else if ($_SERVER['REQUEST_METHOD'] == 'GET') {
$this->logDebug('page_start', 'Entering GET management');

if (session_id() == '') { session_start(); }
if (isset($_GET['afterupload'])) {
$this->logDebug('page_start', 'afterupload is set');
if (!isset($_SESSION[$this->classparams['var_prefix'].'files'])) {
$this->abort('Invalid session (in afterupload, GET, check of $_SESSION): files array is not set');
}
$this->files = $_SESSION[$this->classparams['var_prefix'].'files'];
if (!is_array($this->files)) {
$this->abort('Invalid session (in afterupload, GET, check of is_array(files)): files is not an array');
}
$_SESSION[$this->classparams['var_prefix'].'files'] = array();
ob_start(array(& $this, 'interceptAfterUpload'));
} else {
$this->logDebug('page_start', 'afterupload is not set');
if ($this->classparams['session_regenerate']) {session_regenerate_id(true); }
$this->files = array();
$_SESSION[$this->classparams['var_prefix'].'size'] = 0;
$_SESSION[$this->classparams['var_prefix'].'files'] = $this->files;
ob_start(array(& $this, 'interceptBeforeUpload'));
}

} else if ($_SERVER['REQUEST_METHOD'] == 'POST') {
if (isset($_GET['errormail'])) {
$this->receive_debug_log();
} else {
$this->receive_uploaded_files();
}
}
}
}

08. after_upload.php檔案內容,可根據自己需要設定。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>JUpload PHP Upload Result</title>
</head>
<body>
<?php
session_start();
echo("<h2>List of session content</h2>");
foreach($_SESSION as $key => $value) {
echo("$key: $value<BR>");
} 

echo("<H3>List of uploaded files</H3>");
$files = $_SESSION['juvar.files'];
echo ('Nb uploaded files is: ' . sizeof($files));
echo('<table width="100%" border="1"><TR><TH>Filename</TH><TH>File size</TH><TH>Relative path</TH><TH>Full name</TH><TH>MD5</TH><TH>Specific parameters</TH></TR>');
foreach ($files as $f) {
echo('<tr><td>');
echo($f['name']);
echo('</td><td>');
echo($f['size']);
echo('</td><td>');
echo($f['relativePath']);
echo('</td><td>');
echo($f['fullName']);
echo('</td><td>');
echo($f['md5sum']);
$addBR = false;
foreach ($f as $key=>$value) {
if ($key != 'name' && $key != 'size' && $key != 'relativePath' && $key != 'fullName' && $key != 'md5sum') {
if ($addBR) {
echo('<br>');
} else {
echo('</td><td>');
$addBR = true;
}
echo("$key => $value");
}
}
echo('</td></tr>');
echo("\n");
}
echo("</table>\n");

?>
<p><a href="index.php">Go back to the upload page</a></p>
</body>
</html>

09. 打開瀏覽器,並確認電腦已安裝Java Runtime程式。到http://localhost/jupload/位置,便可看到檔案上載畫面。

10. 選擇相片檔案。

11. 按下面『上傳』便開始傳送檔案。

12. 完成後,會跳到after_upload.php版面,並顯示結果。

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *