- pathinfo: documentation ( source)
- var_dump: documentation ( source)
- dirname: documentation ( source)
- mkdir: documentation ( source)
- strtolower: documentation ( source)
- header: documentation ( source)
- file_exists: documentation ( source)
<?php
header("Content-Type: text/html; charset=utf-8");
function varInfo($display, $var) {
echo '<b>==========' . $display . '===========</b><br />';
echo '<pre>';
var_dump($var);
echo '</pre>';
}
if (isset($_POST['submit'])) {
$files = $_FILES['files'];
$count = count($files['name']);
$response = '';
for ($i = 0; $i < $count; $i++) {
$name = $files['name'][$i];
$tmpName = $files['tmp_name'][$i];
$error = $files['error'][$i];
$size = $files['size'][$i];
$ext = strtolower(pathinfo($name, PATHINFO_EXTENSION));
if ($error == UPLOAD_ERR_OK) {
$valid = true;
//validate file extensions
if ( $ext !== 'xls' ) {
$valid = false;
$response .= '<span style="color:red">' . $name . ':不是 xls 格式</span><br />';
}
//validate file size
if ( $size/1024/1024 > 2 ) {
$valid = false;
$response .= '<span style="color:red">' . $name . ':文件不允许超过 2M</span><br />';
}
//upload file
if ($valid) {
if (!file_exists('uploads')) {
mkdir('uploads');
}
// fixing chinese charset problem in windows
$name = iconv('utf-8', 'cp936', $name);
$targetPath = dirname( __FILE__ ) . DIRECTORY_SEPARATOR. 'uploads' . DIRECTORY_SEPARATOR. $name;
$response .= '上传成功: ' . $name . '<br />';
}
} else {
$response = '<span style="color:red">' . $name . ':上传失败</span><br />';
}
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>upload keywords files</title>
</head>
<body>
<form enctype="multipart/form-data" action="" method="post">
Select a File:<br />
<input type="file" size="20" name="files[]" multiple /><br />
<input type="submit" name="submit" value="upload" />
</form>
<?php if (!empty($response)) { ?>
<p class="message"><?php echo $response; ?></p>
<?php } ?>
</body>
</html>