php上传图片客户端和服务器端实现方法_php技巧
May 16, 2016 pm 08:18 PM
php
upload image
Implementation
client
Service-Terminal
本文实例讲述了php上传图片客户端和服务器端实现方法。分享给大家供大家参考。具体如下:
前台表单代码
<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>
Copy after login
后端php代码
<?php //这里上传 $upsize判断上传文件的大小 $uppath = "/attached/"; //文件上传路径 //转换根目录的路径 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; } //获得文件扩展名 $temp_arr = explode(".", $f["name"]); $file_ext = array_pop($temp_arr); $file_ext = trim($file_ext); $file_ext = strtolower($file_ext); //新文件名 $new_file_name = md5(date("YmdHis")) . '.' . $file_ext; echo $dest = $uppaths . $filename . "/" . date("ymdhis") . "_" . $new_file_name; //设置文件名为日期加上文件名避免重复 上传目录 echo $dest1 = $uppath . $filename . "/" . date("ymdhis") . "_" . $new_file_name; //设置文件名为日期加上文件名避免重复 $r = move_uploaded_file($f['tmp_name'], $dest); ?>
Copy after login
希望本文所述对大家的php程序设计有所帮助。
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article
Repo: How To Revive Teammates
3 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
How Long Does It Take To Beat Split Fiction?
3 weeks ago
By DDD
R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌

Hot tools Tags

Hot Article
Repo: How To Revive Teammates
3 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
How Long Does It Take To Beat Split Fiction?
3 weeks ago
By DDD
R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian

How To Set Up Visual Studio Code (VS Code) for PHP Development
