php作为后端接受上传的图片还是很简单的,需要用到FILES,当客户端或web端向后端post图片时,我们可以用FILES,当客户端或web端向后端post图片时,我们可以用_FILE接收图片,然后存储在临时缓冲区中,最后用move_upload_file函数保存在本地。(推荐学习:PHP视频教程)
<?php $imgname = $_FILES['myfile']['name']; $tmp = $_FILES['myfile']['tmp_name']; $filepath = 'photo/'; if(move_uploaded_file($tmp,$filepath.$imgname.".png")){ echo "上传成功"; }else{ echo "上传失败"; } ?>
代码:
/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>
以上是php如何存储图片的详细内容。更多信息请关注PHP中文网其他相关文章!