When uploading ...LOGIN

When uploading files, you need to pay attention to the php.ini file.

You need to pay attention to the php.ini file when uploading files

Before formally explaining the upload in this chapter, the first thing we need to pay attention to is the php.ini file.

This is our first introduction to how to modify the php.ini file. If your configuration items are inconsistent with what we said, please pay attention to the modification.

Let’s understand each configuration item.

Let’s take a look at how to modify php.ini.

There are too many php.ini files. If you can’t find them, you can use ctrl+f to search for related configuration items.

##file_uploads on is to turn on the file upload function, off is to turn it offpost_max_sizeThe maximum value of POST parameters allowed by the system upload_max_filesizeThe maximum size of uploaded files allowed by the systemmemory_limitMemory usage limit
Configuration itemFunction description

Recommended size: file_size (file size) < upload_max_filesize < post_max_size < memory_limit

In addition, you need to pay attention to the script execution time.

max_execution_time, the unit of this parameter is seconds.

This parameter is to set the maximum execution time of the script.
You can also make appropriate changes according to your needs. Usually there is no need to modify it, the system default value is enough. When uploading very large files, this parameter may be modified.

The upload time is too long and will time out. If you set this parameter to 0, the timeout period is not limited and is not recommended.

After completing the relevant configuration of php.ini, we can start trying to complete the first file upload.


Next Section

<form action="" enctype="multipart/form-data" method="post" name="uploadfile">上传文件:<input type="file" name="upfile" /><br> <input type="submit" value="上传" /></form> <?php //print_r($_FILES["upfile"]); if(is_uploaded_file($_FILES['upfile']['tmp_name'])){ $upfile=$_FILES["upfile"]; //获取数组里面的值 $name=$upfile["name"];//上传文件的文件名 $type=$upfile["type"];//上传文件的类型 $size=$upfile["size"];//上传文件的大小 $tmp_name=$upfile["tmp_name"];//上传文件的临时存放路径 //判断是否为图片 switch ($type){ case 'image/pjpeg':$okType=true; break; case 'image/jpeg':$okType=true; break; case 'image/gif':$okType=true; break; case 'image/png':$okType=true; break; } if($okType){ /** * 0:文件上传成功<br/> * 1:超过了文件大小,在php.ini文件中设置<br/> * 2:超过了文件的大小MAX_FILE_SIZE选项指定的值<br/> * 3:文件只有部分被上传<br/> * 4:没有文件被上传<br/> * 5:上传文件大小为0 */ $error=$upfile["error"];//上传后系统返回的值 echo "================<br/>"; echo "上传文件名称是:".$name."<br/>"; echo "上传文件类型是:".$type."<br/>"; echo "上传文件大小是:".$size."<br/>"; echo "上传后系统返回的值是:".$error."<br/>"; echo "上传文件的临时存放路径是:".$tmp_name."<br/>"; echo "开始移动上传文件<br/>"; //把上传的临时文件移动到up目录下面 move_uploaded_file($tmp_name,'up/'.$name); $destination="up/".$name; echo "================<br/>"; echo "上传信息:<br/>"; if($error==0){ echo "文件上传成功啦!"; echo "<br>图片预览:<br>"; echo "<img src=".$destination.">"; //echo " alt=\"图片预览:\r文件名:".$destination."\r上传时间:\">"; }elseif ($error==1){ echo "超过了文件大小,在php.ini文件中设置"; }elseif ($error==2){ echo "超过了文件的大小MAX_FILE_SIZE选项指定的值"; }elseif ($error==3){ echo "文件只有部分被上传"; }elseif ($error==4){ echo "没有文件被上传"; }else{ echo "上传文件大小为0"; } }else{ echo "请上传jpg,gif,png等格式的图片!"; } } ?>
submitReset Code
ChapterCourseware