php作为后端接受上传的图片还是很简单的,需要用到files,当客户端或web端向后端post图片时,我们可以用files,当客户端或web端向后端post图片时,我们可以用_file接收图片,然后存储在临时缓冲区中,最后用move_upload_file函数保存在本地。使用wampserver服务器,则缓存文件放在tmp文件夹下。
//html代码<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="content-type" content="text/html; charset=utf-8" /><title>insert title here</title></head><body><form action="./uploadheadimg.php" method="post" enctype="multipart/form-data"><!-- <input type="hidden" name="max_file_size" value='176942' /> -->请选择您要上传的文件:<input type="file" name='myfile' /><!-- <input type="file" name="myfile" accept="image/jpeg,image/gif,image/png"/><br /> --><input type="submit" value="上传文件" /></form></body></html>
html界面展示
2.//uploadheadimg.php代码
<?php $imgname = $_files['myfile']['name']; $tmp = $_files['myfile']['tmp_name']; $filepath = 'photo/'; if(move_uploaded_file($tmp,$filepath.$imgname.".png")){ echo "上传成功"; }else{ echo "上传失败"; }?>
在代码中需要修改$filepath变量的值,其值建立一个文件夹,用于存放上传的文件。
推荐:《php教程》
附
php编程语言中的常见的$_files系统函数用法有:
$_files[‘myfile’][‘name’] 显示客户端文件的原名称。
$_files[‘myfile’][‘type’] 文件的 mime 类型,例如image/gif。
$_files[‘myfile’][‘size’] 已上传文件的大小,单位为字节。
$_files[‘myfile’][‘tmp_name’] 储存的临时文件名,一般是系统默认。
$_files[‘myfile’][‘error’] 该文件上传相关的错误代码。
以下为使用$_files[‘myfile’][‘error’] 出现错误代码的意思:
0; 文件上传成功。
1; 超过了文件大小php.ini中即系统设定的大小。
2; 超过了文件大小
max_file_size 选项指定的值。
3; 文件只有部分被上传。
4; 没有文件被上传。
5; 上传文件大小为0。
以上就是php怎么上传文件保存到本地的详细内容。
