Dies ist der Formularcode:
<form method = "post" action = "upload.php" enctype="multipart/form-data">; <input type = "hidden" name = "MAX_FILE_SIZE" value ="10000000"> 需求上传:<input type = "file" name = "myfile" >; <p> <input type = "submit" name = "submit" value = "确定上传"></p>; </form>;
<?php $allowtype = array("gif","png","jpg","doc","txt"); //上传图片的格式 $size = 10000000; //允许最大上传是10M $path = "./uploads"; //上传后的路径 //判断是否成功的上传了服务器,0表示上传成功 if($_FILES['myfile']['error'] > 0) { echo '上传错误:'; switch($_FILES['myfile']['error']){ case 1: die('上传大小超过了限定值:upload_max_filesize'); case 2: die('上传大小超过了表单中的约定值:MAX_FILE_SIZE'); case 3: die('文件只有部分上传!'); case 4: die('没有上传任何文件!'); default:die('未知错误'); } } $linshi =explode(".",$_FILES['myfile']['name']); //判断上传的文件是否符合允许的格式 $hz = array_pop($linshi);//数组获取文件的名称包括扩展名 //方法是判断后缀 if(!in_array($hz,$allowtype)) { die("这个后缀<b>{$hz}</b>不是允许的文件类型"); } //判断大小是否符合 if($_FILES['myfile']['size']>$size) { die('超过了允许的<b>{$size}</b>字节大小'); } //$filename = date("YmdHis").rand(100,999).".".$hz; if(is_uploaded_file($_FILES['myfile']['tmp_name'])) { if(!move_uploaded_file($_FILES['myfile']['tmp_name'],$path.'/'.$_FILES['myfile']['name'])) { die('不能将文件移至指定目录!'); } }else { die('不是指定文件!'); } echo"<center>"; echo "您好!</br>"; echo "您的文件:{$_FILES['myfile']['name']}已上传成功!大小为{$_FILES['myfile']['size']}字节!"; echo"我们会及时处理并联系您,请您耐心等待。"; echo "</br>"; echo "<a href = 'index.php'>返回首页</a>"; echo "</center>"; ?>
Das obige Beispiel stellt das Hochladen von Dateien in PHP vor, einschließlich des Inhalts. Ich hoffe, es wird für Freunde hilfreich sein, die sich für PHP-Tutorials interessieren.