PHP上传文件程序实例

    技术2024-10-20  24

    客户端软件:upload.html

     

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>客户端</title> </head> <body> <table width=100%> <form action='http://teach.onlinewebshop.net/upload.php' method='post' enctype=multipart/form-data> <tr><td> </td><td width=30%> <input type=file name='upfile' size=40><br></td><td> </td></tr> <tr><td> </td><td width=30%> <input type=submit name='按键提交'></center></td><td> </td></tr> <form> </table> </body> </html>

     

     

     

     

    服务端:upload.php

    <?php //如果报错 显示错误代码 if($_FILES['upfile']['error']>0){ echo "Error Number : ".$_FILES['upfile']['error']."<hr>"; } else{ //限制上传图片 如果正确 显示文件的信息 if(($_FILES['upfile']['type'] == "image/gif")||($_FILES['upfile']['type'] == "image/jpeg")||($_FILES['upfile']['type'] == "image/pjpeg")||($_FILES['upfile']['type'] == "image/png")&&($_FILES['upfile']['size'])<200000){ if(file_exists("./upload/".$_FILES['upfile']['name'])){ echo "File Allready Exists !!!<hr>"; } $filepath = "/upload/".$_FILES['upfile']['name']; else{ echo "File-name : ".$_FILES['upfile']['name']."<hr>"; echo "File-type : ".$_FILES['upfile']['type']."<hr>"; echo "File-size : ".$_FILES['upfile']['size']."<hr>"; echo "File-tmp_name : ".$_FILES['upfile']['tmp_name']."<hr>"; move_uploaded_file($_FILES['upfile']['tmp_name'],$filepath); } //显示图片 echo "<img src=".$filepath." mce_src=".$filepath." border=0 /><hr>"; } else{ echo "File type forbidden<hr>"; } } ?>`-

     

     

    正确的返回包括文件信息,以及图片的展示

    选择文件提交之后,返回一片空白!!!

     

     

    分析错误

     

     

    最终确定方案:

    <?php //如果报错 显示错误代码 if($_FILES['upfile']['error']>0){ echo "Error Number : ".$_FILES['upfile']['error']."<hr>"; } else{ //限制上传图片 如果正确 显示文件的信息 $filetype = $_FILES['upfile']['type']; $filepath = dirname(__FILE__)."/upload/".$_FILES['upfile']['name']; if(($filetype == "image/gif")||($filetype == "image/jpeg")||($filetype == "image/bmp")||($filetype == "image/pjpeg")||($filetype == "image/png")&&($_FILES['upfile']['size']<200000)){ echo "File-name : ".$_FILES['upfile']['name']."<hr>"; echo "File-type : ".$filetype."<hr>"; echo "File-size : ".$_FILES['upfile']['size']."byte<hr>"; echo "File-tmp_name : ".$_FILES['upfile']['tmp_name']."<hr>"; if(file_exists($filepath)){ echo "File Allready Exists !!!<hr>"; } else{ move_uploaded_file($_FILES['upfile']['tmp_name'],$filepath); echo dirname(__FILE__)."/upload/".$filepath."<hr>"; } //显示图片 echo "<hr><center><img src="/upload/" mce_src="upload/"".$_FILES['upfile']['name']." border=0 /></center><hr>"; } else{ echo "File type forbidden<hr>"; } } ?>  

     

     

     

     错误总结:   1、echo 输出显示的时候进行运算的问题?   2、move_loaded_file 目的文件夹使用绝对路径?相对路径?   3、细微语法错误,比如修改代码的时候双引号(")的残留!!!!

     

    使用fopen/fwrite/move_loaded_file等的时候 如果加入了路径 ,请注意是否具有读写权限,以及能否新建文件?能否新建文件夹!!!!

     

     

     

    最新回复(0)