- pathinfo: 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");
if (isset($_POST['submit'])) {
$files = $_FILES['files'];
$count = count($files['name']);
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;
}
} else {
$response = '<span style="color:red">' . $name . ':upload failed</span><br />';
}
echo $error;
}
}
?>
<!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>
</body>
</html>