<?php
//获取文件后缀名函数
function fileext($filename)
{
return substr(strrchr($filename,'.'),1);
}
//生成文件随机名函数
function random($length)
{
$hash ='cr-';
$chars='abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz';
$max = strlen($chars)-1;
mt_srand((double)microtime()*1000000);
for($i = 0; $i < $length; $i++ )
{
$hash .= $chars[mt_rand(0, $max)];
}
return $hash;
$a=strtolower(fileext($_files['file']['name']));
//判断文件的类型
if(!in_array(strtolower(fileext($_files['file']['name'])),$type))
{
$text=implode(",",$type);
echo "您只能上传以下类型的文件:",$text,"<br>";
}
//生成目标文件的文件名
else{
$filename=explode(".",$_files['file']['name']);
do
{
$filename[0]=random(3);
$name=implode(".",$filename);
//$name1=$name.".mcncc";
$uploadfile=$uploaddir.$name;
}
while(file_exists($uploadfile));
if(move_uploaded_file($_files['file']['tmp_name'],$uploadfile)){
//if(is_uploaded_file($_files['file']['tmp_name'])){
if($_files['file']['size']>0){
//输出图片的预览
echo "<center>您的文件上传完毕请看上传图片的预览:</center><br><center><img src='$uploadfile'></center>";
echo "<br><center><a href='example2_9.php'>继续上传</a></center>";
}
else{
echo "上传失败!";
}
}
}
}
|