PHP还是比较常用的,相信大家也都知道它的好处,给大家演示一个PHP上传文件代码,下面我们来一起看看吧。通过memory_limit变量来指定单个脚本程序可以使用的最大内存容量变量memory_limit的值应当适当大于post_max_size的值max_execution_time,max_execution_time设置了在强制终止脚本前PHP等待脚本执行完毕的时间,此时间以秒计算。
PHP上传文件代码
<ol class="dp-xml"> <li class="alt"><span><strong><font color="#006699"><span class="tag"></span><span class="tag-name">php</span></font></strong><span> </span></span></li> <li class=""> <span>$</span><span class="attribute"><font color="#ff0000">file</font></span><span>=$_FILES['userfile1']['tmp_name']; </span> </li> <li class="alt"> <span>$</span><span class="attribute"><font color="#ff0000">filename</font></span><span>=$_FILES['userfile1']['name']; </span> </li> <li class=""> <span>$</span><span class="attribute"><font color="#ff0000">x</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">rand</font></span><span>(1000,9999); </span> </li> <li class="alt"> <span>$</span><span class="attribute"><font color="#ff0000">tmp_add_pd_path</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">"./upload/"</font></span><span>.$x.".jpg"; </span> </li> <li class=""> <span>if(!$file||$</span><span class="attribute"><font color="#ff0000">file</font></span><span>=="none"){ </span> </li> <li class="alt"><span>echo"no"; </span></li> <li class=""><span>} </span></li> <li class="alt"><span>else{ </span></li> <li class=""><span>if(move_uploaded_file($file,$tmp_add_pd_path)){ </span></li> <li class="alt"><span>echo$filename; </span></li> <li class=""> <span>$</span><span class="attribute"><font color="#ff0000">db</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">mysql_connect</font></span><span>("localhost","root","wyh7ye"); </span> </li> <li class="alt"><span>mysql_select_db("test",$db); </span></li> <li class=""> <span>$</span><span class="attribute"><font color="#ff0000">query</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">"INSERTintoproduct(name)values('$tmp_add_pd_path')"</font></span><span>; </span> </li> <li class="alt"> <span>$</span><span class="attribute"><font color="#ff0000">result</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">mysql_query</font></span><span>($query,$db); </span> </li> <li class=""> <span>$</span><span class="attribute"><font color="#ff0000">pid</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">"SELECTLAST_INSERT_ID()"</font></span><span>; </span> </li> <li class="alt"> <span>$</span><span class="attribute"><font color="#ff0000">pid_result</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">mysql_query</font></span><span>($pid,$db); </span> </li> <li class=""> <span>$</span><span class="attribute"><font color="#ff0000">rows</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">mysql_fetch_array</font></span><span>($pid_result); </span> </li> <li class="alt"> <span>$</span><span class="attribute"><font color="#ff0000">tmp_pid</font></span><span>=$rows[0]; </span> </li> <li class=""> <span>$</span><span class="attribute"><font color="#ff0000">tmp_p_type_sql</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">"INSERTintot_product(typeid,pid)values('$p_type','$tmp_pid')"</font></span><span>; </span> </li> <li class="alt"> <span>$</span><span class="attribute"><font color="#ff0000">result</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">mysql_query</font></span><span>($tmp_p_type_sql,$db); </span> </li> <li class=""><span>echo"sucess"; </span></li> <li class="alt"><span>} </span></li> <li class=""><span>else{ </span></li> <li class="alt"><span>echo"false"; </span></li> <li class=""><span>} </span></li> <li class="alt"><span>} </span></li> <li class=""> <span></span><span class="tag"><strong><font color="#006699">?></font></strong></span><span> </span> </li> <li class="alt"> <span></span><strong><font color="#006699"><span class="tag"><span class="tag-name">br</span><span class="tag">></span><span class="tag"><span class="tag-name">br</span><span class="tag">></span><span class="tag"><span class="tag-name">br</span><span class="tag">></span><span class="tag"><span class="tag-name">br</span><span class="tag">></span></span></span></span></span></font></strong><span> </span> </li> <li class=""> <span></span><strong><font color="#006699"><span class="tag"></span><span class="tag-name">php</span></font></strong><span> </span> </li> <li class="alt"> <span>$</span><span class="attribute"><font color="#ff0000">db</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">mysql_connect</font></span><span>("localhost","root","wyh7ye"); </span> </li> <li class=""><span>mysql_select_db("test",$db); </span></li> <li class="alt"> <span>$</span><span class="attribute"><font color="#ff0000">query</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">"selectnamefromproduct"</font></span><span>; </span> </li> <li class=""> <span>$</span><span class="attribute"><font color="#ff0000">result</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">mysql_query</font></span><span>($query,$db); </span> </li> <li class="alt"> <span>while($</span><span class="attribute"><font color="#ff0000">rows</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">mysql_fetch_array</font></span><span>($result)){ </span> </li> <li class=""> <span>echo"</span><span class="tag"><strong><font color="#006699"></font></strong></span><span class="attribute"><font color="#ff0000">imgsrc</font></span><span class="tag-name"><strong><font color="#006699">imgsrc</font></strong></span><span>=".$rows['name']."</span><span class="attribute-value"><font color="#0000ff">width</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">180</font></span><span class="attribute"><font color="#ff0000">180height</font></span><span>=180</span><strong><font color="#006699"><span class="tag">></span><span class="tag"><span class="tag-name">br</span><span class="tag">></span></span></font></strong><span>"; </span> </li> <li class="alt"><span>} </span></li> <li class=""> <span></span><span class="tag"><strong><font color="#006699">?></font></strong></span><span> </span> </li> </ol>
以上就是详细的PHP上传文件代码,希望对大家有帮助。