php實現圖片的簡單上傳功能~php
<?php if($_SERVER['REQUEST_METHOD']=='POST'){ if (is_uploaded_file($_FILES['upfile']['tmp_name'])){ //判斷文件是否上傳 $upfile=($_FILES['upfile']); $name=$upfile['name']; $type=$upfile['type']; $size=$upfile['size']; $tmp_name=$upfile['tmp_name']; $error=$upfile['error']; //限制文件格式只可爲:pjpeg png gif jpeg 格式! switch($type){ case 'p_w_picpath/pjpeg': $ok=1; break; case'p_w_picpath/png' : $ok=1; break; case 'p_w_picpath/gif' :$ok=1; break; case 'p_w_picpath/jpeg' :$ok=1; break; } if($ok && $error=='0'){ move_uploaded_file($tmp_name,'p_w_picpaths/'.$name); echo "上傳成功!"; } else{ echo "非法文件!"; //非圖片格式,給出提示 } } } unset($upfile,$error,$name,$ok,$size,$tmp_name,$type); ?> <!doctype> <html> <head> <title>上傳圖片</title> </head> <body> <form action="" method="post" enctype="multipart/form-data" name="upform"> 上傳文件:<input type="file" name="upfile"> <input type="submit" value="上傳"><br> </form> </body> </html>