萬盛學電腦網

 萬盛學電腦網 >> 網絡編程 >> php編程 >> php入門級文件圖片上傳代碼

php入門級文件圖片上傳代碼

$uploaddir = "../www.111cn.net/upload_img/";
$type = array("jpg","gif","bmp","jpeg","png");
$patch = "http://localhost/sl/upload_img/";  
?>

 代碼如下 復制代碼

<?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 "上傳失敗!";
}
}
}
}

 

copyright © 萬盛學電腦網 all rights reserved