php上傳圖片客戶端和服務(wù)器端實(shí)現(xiàn)方法
文章主要介紹了php上傳圖片客戶端和服務(wù)器端實(shí)現(xiàn)方法,涉及php操作文件的技巧,涉及圖片后綴驗(yàn)證、文件重命名等常用方法,具有一定參考借鑒價(jià)值,需要的朋友可以參考下。

本文實(shí)例講述了php上傳圖片客戶端和服務(wù)器端實(shí)現(xiàn)方法。分享給大家供大家參考。具體如下:
前臺表單代碼
?
1
2
3
4
<form name="form1" method="post" action="admin_upfile.php" enctype="multipart/form-data">
<input type="file" name="pic">
<input type="submit" name="Submit" value="開始上傳" class="button">
</form>
后端php代碼
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
<?php
/pic/p>
$uppath = "/attached/"; /pic/p>
/pic/p>
if (strpos($uppath, "/") == 0)
{
$i = 0;
$thpath = $_SERVER["SCRIPT_NAME"];
$thpath = substr($thpath, 1, strlen($thpath));
while (strripos($thpath, "/") !== false)
{
$thpath = substr($thpath, strpos($thpath, "/") + 1, strlen($thpath));
$i = ++$i;
}
$pp = "";
for ($j = 0; $j < $i; ++$j)
{
$pp .= "../";
}
$uppaths = $pp . substr($uppath, 1, strlen($thpath));
}
$filename = date("y-m-d");
if (is_dir($uppaths . $filename) != TRUE)
mkdir($uppaths . $filename, 0777);
$f = $_FILES['pic'];
if ($f["type"] != "image/gif" && $f["type"] != "image/pjpeg" && $f["type"] != "image/jpeg" && $f["type"] != "image/x-png")
{
echo "<script>alert('只能上傳圖片格式的文件');window.close()</script>";
return false;
}
/pic/p>
$temp_arr = explode(".", $f["name"]);
$file_ext = array_pop($temp_arr);
$file_ext = trim($file_ext);
$file_ext = strtolower($file_ext);
/pic/p>
$new_file_name = md5(date("YmdHis")) . '.' . $file_ext;
echo $dest = $uppaths . $filename . "/" . date("ymdhis") . "_" .
$new_file_name; /pic/p>
echo $dest1 = $uppath . $filename . "/" . date("ymdhis") . "_" .
$new_file_name; /pic/p>
$r = move_uploaded_file($f['tmp_name'], $dest);
?>
希望本文所述對大家的php程序設(shè)計(jì)有所幫助。
【php上傳圖片客戶端和服務(wù)器端實(shí)現(xiàn)方法】相關(guān)文章:
簡單PHP上傳圖片的方法02-01
PHP實(shí)現(xiàn)文件上傳和多文件上傳01-19
PHP圖片文件怎么上傳實(shí)現(xiàn)代碼01-25
php實(shí)現(xiàn)通過ftp上傳文件06-28
jQuery Mobile + PHP實(shí)現(xiàn)文件上傳12-08
PHP圖片上傳類帶圖片顯示03-13
php多圖片上傳的簡單例子11-21
php上傳與刪除圖片的簡單范例12-15
- 相關(guān)推薦