PHPを学習するとき、PHPで画像をアップロードする際に問題が発生することがあります。ここでは、この問題を解決するための詳細なPHP画像アップロードコードを紹介します。 PHP の構文は非常にシンプルであるため、インターネット上で最も人気のあるプログラミング言語となります。たとえば、C 言語を学習する場合、各変数の定義方法、ポインタの操作方法、メモリの作成と破棄の方法などを十分に理解する必要があります。 。
以下は完全な PHP 画像アップロード コードです。
<ol class="dp-xml"> <li class="alt"><span><span class="tag"><</SPAN><SPAN class=attribute>formenctype</SPAN><SPAN class=tag-name>formenctype</SPAN><SPAN>="multipart/form-data"</SPAN><SPAN class=attribute-value>action</SPAN><SPAN>=""</SPAN><SPAN class=attribute>method</SPAN><SPAN>=</SPAN><SPAN class=attribute-value>"post"</SPAN><SPAN class=tag>></span><span> </span></span></li> <li class=""> <span></span><span class="tag"><</SPAN><SPAN class=attribute>inputtype</SPAN><SPAN class=tag-name>inputtype</SPAN><SPAN>="hidden"</SPAN><SPAN class=attribute-value>name</SPAN><SPAN>="max_file_size"</SPAN><SPAN class=attribute>value</SPAN><SPAN>=</SPAN><SPAN class=attribute-value>"200000"</SPAN><SPAN class=tag>></span><span> </span> </li> <li class="alt"> <span></span><span class="tag"><</SPAN><SPAN class=attribute>inputname</SPAN><SPAN class=tag-name>inputname</SPAN><SPAN>="userfile"</SPAN><SPAN class=attribute-value>type</SPAN><SPAN>="file"</SPAN><SPAN class=tag>></span><span> </span> </li> <li class=""> <span></span><span class="tag"><</SPAN><SPAN class=attribute>inputtype</SPAN><SPAN class=tag-name>inputtype</SPAN><SPAN>="submit"</SPAN><SPAN class=attribute-value>name</SPAN><SPAN>="submit"</SPAN><SPAN class=attribute>value</SPAN><SPAN>=</SPAN><SPAN class=attribute-value>"上传文件"</SPAN><SPAN class=tag>></span><span> </span> </li> <li class="alt"> <span></span><span class="tag"></</SPAN><SPAN class=tag-name>form</SPAN><SPAN class=tag>></span><span> </span> </li> <li class=""><span> </span></li> <li class="alt"> <span></span><span class="tag"><?</SPAN><SPAN class=tag-name>php</SPAN><SPAN> </SPAN></SPAN><LI class=""><SPAN>$</SPAN><SPAN class=attribute>type</SPAN><SPAN>=</SPAN><SPAN class=attribute-value>array</SPAN><SPAN>("jpg","gif","bmp","jpeg","png"); </SPAN></SPAN><LI class=alt><SPAN>$</SPAN><SPAN class=attribute>uploaddir</SPAN><SPAN>=</SPAN><SPAN class=attribute-value>"./file/"</SPAN><SPAN>; </SPAN></SPAN><LI class=""><SPAN> </SPAN><LI class=alt><SPAN>if(isset($_POST['submit'])) </SPAN><LI class=""><SPAN>{ </SPAN><LI class=alt><SPAN>functiontexttype($name) </SPAN><LI class=""><SPAN>{ </SPAN><LI class=alt><SPAN>returnsubstr(strrchr($name,'.'),1); </SPAN><LI class=""><SPAN>} </SPAN><LI class=alt><SPAN>if(!in_array(strtolower(texttype($_FILES['userfile']['name'])),$type)) </SPAN><LI class=""><SPAN>{ </SPAN><LI class=alt><SPAN>$</SPAN><SPAN class=attribute>text</SPAN><SPAN>=</SPAN><SPAN class=attribute-value>implode</SPAN><SPAN>(",",$type); </SPAN></SPAN><LI class=""><SPAN>echo"您只能上传以下类型文件:",$text,"</SPAN><SPAN class=tag><</SPAN><SPAN class=tag-name>br</SPAN><SPAN class=tag>></span><span>"; </span> </li> <li class="alt"><span>} </span></li> <li class=""><span>else </span></li> <li class="alt"><span>{ </span></li> <li class=""> <span>if($_FILES['userfile']['size']</span><span class="tag"><</SPAN><SPAN>="200000") </SPAN></SPAN><LI class=alt><SPAN>{ </SPAN><LI class=""><SPAN>$</SPAN><SPAN class=attribute>filename</SPAN><SPAN>=</SPAN><SPAN class=attribute-value>explode</SPAN><SPAN>(".",$_FILES['userfile']['name']); </SPAN></SPAN><LI class=alt><SPAN>$</SPAN><SPAN class=attribute>time</SPAN><SPAN>=</SPAN><SPAN class=attribute-value>date</SPAN><SPAN>("m-d-H-i-s"); </SPAN></SPAN><LI class=""><SPAN>$filename[0]=$time; </SPAN><LI class=alt><SPAN>$</SPAN><SPAN class=attribute>name</SPAN><SPAN>=</SPAN><SPAN class=attribute-value>implode</SPAN><SPAN>(".",$filename); </SPAN></SPAN><LI class=""><SPAN>$</SPAN><SPAN class=attribute>uploadfile</SPAN><SPAN>=$uploaddir.$name; </SPAN></SPAN><LI class=alt><SPAN>if(move_uploaded_file($_FILES['userfile']['tmp_name'],$uploadfile)) </SPAN><LI class=""><SPAN>{ </SPAN><LI class=alt><SPAN>echo"</SPAN><SPAN class=tag><</SPAN><SPAN class=tag-name>center</SPAN><SPAN class=tag>></span><span>您的文件已经上传完毕上传图片预览:</span><span class="tag"></</SPAN><SPAN class=tag-name>center</SPAN><SPAN class=tag>></span><span class="tag"><</SPAN><SPAN class=tag-name>br</SPAN><SPAN class=tag>></span><span class="tag"><</SPAN><SPAN class=tag-name>center</SPAN><SPAN class=tag>></span><span class="tag"><</SPAN><SPAN class=attribute>imgsrc</SPAN><SPAN class=tag-name>imgsrc</SPAN><SPAN>=</SPAN><SPAN class=attribute-value>'$uploadfile'</SPAN><SPAN class=tag>></span><span class="tag"></</SPAN><SPAN class=tag-name>center</SPAN><SPAN class=tag>></span><span>"; </span> </li> <li class=""> <span>echo"</span><span class="tag"><</SPAN><SPAN class=tag-name>br</SPAN><SPAN class=tag>></span><span class="tag"><</SPAN><SPAN class=tag-name>center</SPAN><SPAN class=tag>></span><span class="tag"><</SPAN><SPAN class=attribute>ahref</SPAN><SPAN class=tag-name>ahref</SPAN><SPAN>=</SPAN><SPAN class=attribute-value>'javascrīpt:history.go(-1)'</SPAN><SPAN class=tag>></span><span>继续上传</span><span class="tag"></</SPAN><SPAN class=tag-name>a</SPAN><SPAN class=tag>></span><span class="tag"></</SPAN><SPAN class=tag-name>center</SPAN><SPAN class=tag>></span><span>"; </span> </li> <li class="alt"><span>} </span></li> <li class=""><span>else </span></li> <li class="alt"><span>{ </span></li> <li class=""><span>echo"传输失败!"; </span></li> <li class="alt"><span>} </span></li> <li class=""><span>} </span></li> <li class="alt"><span>else </span></li> <li class=""><span>{ </span></li> <li class="alt"><span>echo"图片太大"; </span></li> <li class=""><span>} </span></li> <li class="alt"><span>} </span></li> <li class=""><span>} </span></li> <li class="alt"> <span></span><span class="tag">?></span><span> </span> </li> </ol>
上記は詳細な PHP 画像アップロード コードです。皆様のお役に立てれば幸いです。