今日、PHP に関する本を読み、PHP でファイルをアップロードする方法を学びました。move_uploade_file (一時ファイル、ターゲットの場所、ファイル名) と is_uploaded_file() は、アップロードの移動に使用されます。次に、サーバー キャッシュ領域内のファイルをターゲット ファイルに保存します。これは、ファイルが正常にアップロードされたかどうかを判断するために使用されます。上記の 2 つの関数に加えて、form タグの enctype の値が次のようになっている必要があることも説明する必要があります:
<ol class="dp-xml"><li class="alt"><span><span class="tag"><strong><font color="#006699"><</FONT></STRONG></SPAN><SPAN class=attribute><FONT color=#ff0000>formenctype</FONT></SPAN><SPAN class=tag-name><STRONG><FONT color=#006699>formenctype</FONT></STRONG></SPAN><SPAN>="multipart/form-data"</SPAN><SPAN class=attribute-value><FONT color=#0000ff>method</FONT></SPAN><SPAN>="post"</SPAN><SPAN class=attribute><FONT color=#ff0000>name</FONT></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>"upform"</FONT></SPAN><SPAN class=tag><STRONG><FONT color=#006699>></font></strong></span><span> </span></span></li></ol>
ファイルが正しい形式でアップロードされることを保証するために、その値のみが multipart/form-data である必要があります。エンコード方式。 input タグの type 属性の "file"
<ol class="dp-xml"><li class="alt"><span><span class="tag"><strong><font color="#006699"><</FONT></STRONG></SPAN><SPAN class=attribute><FONT color=#ff0000>inputname</FONT></SPAN><SPAN class=tag-name><STRONG><FONT color=#006699>inputname</FONT></STRONG></SPAN><SPAN>="upfile"</SPAN><SPAN class=attribute-value><FONT color=#0000ff>type</FONT></SPAN><SPAN>="file"</SPAN><SPAN class=tag><STRONG><FONT color=#006699>></font></strong></span><span> </span></span></li></ol>
PHP アップロード ファイル コード クラス saveupload.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 class=""><SPAN>if(is_uploaded_file($_FILES['upfile']['tmp_name'])){ </SPAN><LI class=alt><SPAN>$</SPAN><SPAN class=attribute><FONT color=#ff0000>upfile</FONT></SPAN><SPAN>=$_FILES["upfile"];//如果已经选定了要上传的文件,将其索引保存在$upfile中 </SPAN></SPAN><LI class=""><SPAN> </SPAN><LI class=alt><SPAN> </SPAN><LI class=""><SPAN>//分别去上传文件的名字,类型等 </SPAN><LI class=alt><SPAN>$</SPAN><SPAN class=attribute><FONT color=#ff0000>name</FONT></SPAN><SPAN>=$upfile["name"]; </SPAN></SPAN><LI class=""><SPAN>$</SPAN><SPAN class=attribute><FONT color=#ff0000>type</FONT></SPAN><SPAN>=$upfile["type"]; </SPAN></SPAN><LI class=alt><SPAN>$</SPAN><SPAN class=attribute><FONT color=#ff0000>size</FONT></SPAN><SPAN>=$upfile["size"]; </SPAN></SPAN><LI class=""><SPAN>$</SPAN><SPAN class=attribute><FONT color=#ff0000>tmp_name</FONT></SPAN><SPAN>=$upfile["tmp_name"]; </SPAN></SPAN><LI class=alt><SPAN>$</SPAN><SPAN class=attribute><FONT color=#ff0000>error</FONT></SPAN><SPAN>=$upfile["error"]; </SPAN></SPAN><LI class=""><SPAN> </SPAN><LI class=alt><SPAN> </SPAN><LI class=""><SPAN>//设定上传文件类型 </SPAN><LI class=alt><SPAN>switch($type){ </SPAN><LI class=""><SPAN>case'image/pjpeg': </SPAN><LI class=alt><SPAN>$</SPAN><SPAN class=attribute><FONT color=#ff0000>ok</FONT></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>1</FONT></SPAN><SPAN>; </SPAN></SPAN><LI class=""><SPAN>break; </SPAN><LI class=alt><SPAN> </SPAN><LI class=""><SPAN>case'image/jpeg': </SPAN><LI class=alt><SPAN>$</SPAN><SPAN class=attribute><FONT color=#ff0000>ok</FONT></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>1</FONT></SPAN><SPAN>; </SPAN></SPAN><LI class=""><SPAN>break; </SPAN><LI class=alt><SPAN> </SPAN><LI class=""><SPAN>case'image/png': </SPAN><LI class=alt><SPAN>$</SPAN><SPAN class=attribute><FONT color=#ff0000>ok</FONT></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>1</FONT></SPAN><SPAN>; </SPAN></SPAN><LI class=""><SPAN>break; </SPAN><LI class=alt><SPAN> </SPAN><LI class=""><SPAN>case'image/gif': </SPAN><LI class=alt><SPAN>$</SPAN><SPAN class=attribute><FONT color=#ff0000>ok</FONT></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>1</FONT></SPAN><SPAN>; </SPAN></SPAN><LI class=""><SPAN>break; </SPAN><LI class=alt><SPAN>} </SPAN><LI class=""><SPAN> </SPAN><LI class=alt><SPAN> </SPAN><LI class=""><SPAN>//如果文件类型合法并且$error返回值为0,说明上传成功 </SPAN><LI class=alt><SPAN>if($ok&&$</SPAN><SPAN class=attribute><FONT color=#ff0000>error</FONT></SPAN><SPAN>=='0'){ </SPAN></SPAN><LI class=""><SPAN>move_uploaded_file($tmp_name,'up/'.$name);//将保存在缓存的文件移动到指定目录下 </SPAN><LI class=alt><SPAN>echo"上传成功"; </SPAN><LI class=""><SPAN>} </SPAN><LI class=alt><SPAN>} </SPAN><LI class=""><SPAN> </SPAN><LI class=alt><SPAN></SPAN><SPAN class=tag><STRONG><FONT color=#006699>?></span></font></strong></span><span> </span> </li></ol>
PHP アップロード ファイル コード アップロード ページ Upload.php
<ol class="dp-xml"> <li class="alt"><span><span><!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"</SPAN><SPAN class=tag><STRONG><FONT color=#006699>></span><span> </span></span></li> <li class=""> <span></span><span class="tag"><strong><font color="#006699"><</FONT></STRONG></SPAN><SPAN class=attribute><FONT color=#ff0000>htmlxmlns</FONT></SPAN><SPAN class=tag-name><STRONG><FONT color=#006699>htmlxmlns</FONT></STRONG></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>"http://www.w3.org/1999/xhtml"</FONT></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><SPAN class=tag-name>head</SPAN><SPAN class=tag>></span></font></strong><span> </span> </li> <li class=""> <span></span><span class="tag"><strong><font color="#006699"><</FONT></STRONG></SPAN><SPAN class=attribute><FONT color=#ff0000>metahttp-equiv</FONT></SPAN><SPAN class=tag-name><STRONG><FONT color=#006699>metahttp-equiv</FONT></STRONG></SPAN><SPAN>="Content-Type"</SPAN><SPAN class=attribute-value><FONT color=#0000ff>content</FONT></SPAN><SPAN>="text/html;</SPAN><SPAN class=attribute><FONT color=#ff0000>charset</FONT></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>utf</FONT></SPAN><SPAN>-8"</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><SPAN class=tag-name>title</SPAN><SPAN class=tag>></span></font></strong><span>upload</span><strong><font color="#006699"><span class="tag"></</SPAN><SPAN class=tag-name>title</SPAN><SPAN class=tag>></span></font></strong><span> </span> </li> <li class=""> <span></span><span class="tag"><strong><font color="#006699"><</FONT></STRONG></SPAN><SPAN class=attribute><FONT color=#ff0000>styletype</FONT></SPAN><SPAN class=tag-name><STRONG><FONT color=#006699>styletype</FONT></STRONG></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>"text/css"</FONT></SPAN><SPAN class=tag><STRONG><FONT color=#006699>></font></strong></span><span> </span> </li> <li class="alt"> <span><!-- </SPAN><LI class=""><SPAN>body{ </SPAN><LI class=alt><SPAN>background-color:#CFF; </SPAN><LI class=""><SPAN>text-align:center; </SPAN><LI class=alt><SPAN>} </SPAN><LI class=""><SPAN>--</SPAN><SPAN class=tag><STRONG><FONT color=#006699>></span><span> </span> </li> <li class="alt"> <span></span><strong><font color="#006699"><span class="tag"></</SPAN><SPAN class=tag-name>style</SPAN><SPAN class=tag>></span><span class="tag"></</SPAN><SPAN class=tag-name>head</SPAN><SPAN class=tag>></span></font></strong><span> </span> </li> <li class=""><span> </span></li> <li class="alt"> <span></span><strong><font color="#006699"><span class="tag"><</SPAN><SPAN class=tag-name>body</SPAN><SPAN class=tag>></span></font></strong><span> </span> </li> <li class=""><span>文件上传 </span></li> <li class="alt"> <span></span><strong><font color="#006699"><span class="tag"><</SPAN><SPAN class=tag-name>hr</SPAN><SPAN class=tag>/></span></font></strong><span> </span> </li> <li class=""> <span></span><span class="tag"><strong><font color="#006699"><</FONT></STRONG></SPAN><SPAN class=attribute><FONT color=#ff0000>formid</FONT></SPAN><SPAN class=tag-name><STRONG><FONT color=#006699>formid</FONT></STRONG></SPAN><SPAN>="form1"</SPAN><SPAN class=attribute-value><FONT color=#0000ff>name</FONT></SPAN><SPAN>="form1"</SPAN><SPAN class=attribute><FONT color=#ff0000>method</FONT></SPAN><SPAN>="post"</SPAN><SPAN class=attribute-value><FONT color=#0000ff>action</FONT></SPAN><SPAN>="saveupload.php"</SPAN><SPAN class=attribute><FONT color=#ff0000>enctype</FONT></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>"multipart/form-data"</FONT></SPAN><SPAN class=tag><STRONG><FONT color=#006699>></font></strong></span><span> </span> </li> <li class="alt"><span>上传文件: </span></li> <li class=""> <span></span><strong><font color="#006699"><span class="tag"><</SPAN><SPAN class=tag-name>label</SPAN><SPAN class=tag>></span></font></strong><span> </span> </li> <li class="alt"> <span></span><span class="tag"><strong><font color="#006699"><</FONT></STRONG></SPAN><SPAN class=attribute><FONT color=#ff0000>inputtype</FONT></SPAN><SPAN class=tag-name><STRONG><FONT color=#006699>inputtype</FONT></STRONG></SPAN><SPAN>="file"</SPAN><SPAN class=attribute-value><FONT color=#0000ff>name</FONT></SPAN><SPAN>="upfile"</SPAN><SPAN class=tag><STRONG><FONT color=#006699>/></font></strong></span><span> </span> </li> <li class=""> <span></span><strong><font color="#006699"><span class="tag"></</SPAN><SPAN class=tag-name>label</SPAN><SPAN class=tag>></span></font></strong><span> </span> </li> <li class="alt"> <span></span><strong><font color="#006699"><span class="tag"><</SPAN><SPAN class=tag-name>label</SPAN><SPAN class=tag>></span></font></strong><span> </span> </li> <li class=""> <span></span><span class="tag"><strong><font color="#006699"><</FONT></STRONG></SPAN><SPAN class=attribute><FONT color=#ff0000>inputtype</FONT></SPAN><SPAN class=tag-name><STRONG><FONT color=#006699>inputtype</FONT></STRONG></SPAN><SPAN>="submit"</SPAN><SPAN class=attribute-value><FONT color=#0000ff>name</FONT></SPAN><SPAN>="button"</SPAN><SPAN class=attribute><FONT color=#ff0000>id</FONT></SPAN><SPAN>="button"</SPAN><SPAN class=attribute-value><FONT color=#0000ff>value</FONT></SPAN><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><SPAN class=tag-name>label</SPAN><SPAN class=tag>></span></font></strong><span> </span> </li> <li class=""> <span></span><strong><font color="#006699"><span class="tag"></</SPAN><SPAN class=tag-name>form</SPAN><SPAN class=tag>></span></font></strong><span> </span> </li> <li class="alt"><span> </span></li> <li class=""> <span></span><strong><font color="#006699"><span class="tag"></</SPAN><SPAN class=tag-name>body</SPAN><SPAN class=tag>></span></font></strong><span> </span> </li> </ol>