PHP File Upload
1, upload.php
Copy Code code as follows:
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >
<title>ddd</title>
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 ">
<body>
<!--file Upload to note: 1, to have enctyp,2, method= "POST"-->
<form enctype= "Multipart/form-data" action= "uploadprocess.php" method= "POST" >
<table>
<tr><td> please fill in User name </td><td><input type= "text" name= "username" ></td></tr>
<tr><td> please briefly introduce the file </td><td><textarea rows= "7" cols= "Name=" Fileintro "style=" width : 300px; " ></textarea></td></tr>
<tr><td> please upload your file </td><td><input type= "file" Name= "MyFile" ></td></tr>
<TR><TD colspan= "2" ><input type= "submit" value= "Upload" ><td></tr>
</table>
</form>
</body>
2, uploadprocess.php
Copy Code code as follows:
<?php
Receive
$username =$_post[' username '];
$fileintro =$_post[' Fileintro '];
echo $username. $fileintro;
Get file information
/* echo "<pre>";
Print_r ($_files);
echo "</pre>";
*/
Get the size of a file
$file _size=$_files[' myfile ' [' Size '];
if ($file _size>2*1024*1024) {
echo "<script type= ' text/javascript ' >window.alert (' file cannot be greater than 2M ') </script>";
Exit ();
}
Get file type
$file _type=$_files[' myfile '] [' type '];
if ($file _type!= "Image/jpeg" && $file _type!= "Image/pjpeg") {
echo "File type can only be JPG format";
Exit ();
}
To determine if the upload is OK
if (Is_uploaded_file ($_files[' myfile '] [' tmp_name '])) {
Get uploaded files to the directory you want
$upload _file=$_files[' myfile ' [' tmp_name '];
Prevent picture overwrite problems, create a folder for each user
$user _path=$_server[' Document_root ']. " /file/up/". $username;
if (!file_exists ($user _path)) {
mkdir ($user _path);
}
$move _to_file= $user _path. " /". $_files[' myfile ' [' name '];
Prevent users from uploading user names the same problem
$file _true_name=$_files[' myfile ' [' name '];
$move _to_file= $user _path. " /". Time (). Rand (1,1000). substr ($file _true_name,strripos ($file _true_name,". "));
Echo $upload _file. $move _to_file;
Chinese to transfer code
if (Move_uploaded_file ($upload _file,iconv ("Utf-8", "gb2312", "$move _to_file")) {
echo $_files[' myfile ' [' Name ']. " Upload Success ";
}else{
echo "Upload failed";
}
}else{
echo "Upload failed";
}
?>
3. Encapsulation:
Copy Code code as follows:
<?php
Class upload{
Public $upload _name; Upload file name
Public $upload _tmp_path; Upload file Save to Server temp path
public $file _size;
public $file _type;
public $file _save_path;
function __construct () {
$this->upload_name=$_files[' myfile ' [' name '];
$this->upload_tmp_path=$_files[' myfile ' [' tmp_name '];
$this->file_size=$_files[' myfile ' [' Size '];
$this->file_type=$_files[' myfile '] [' type '];
$this->allow_file_type = array (' JPEG ', ' jpg ', ' png ', ' gif ', ' bmp ', ' Doc ', ' zip ', ' rar ', ' txt ', ' wps ', ' xlsx ', ' ppt ');
$this->file_save_path=$_server[' Document_root ']. " /file/up/";
}
Public Function Upload_file ($username) {
Determine file size
if ($this->file_size>2*1024*1024) {
echo "<script type= ' text/javascript ' >window.alert (' file cannot be greater than 2M ') </script>";
Exit ();
}
Get file type
/* IF ($this->file_type!= "Image/jpeg" && $this->file_type!= "Image/pjpeg") {
echo "File type can only be JPG format";
Exit ();
}
*//Get the file name extension
$file _type= $this->getfileext ($this->upload_name);
if (!in_array ($file _type, $this->allow_file_type)) {
echo "Upload file type format error";
Exit ();
}
To determine if the upload is OK
if (Is_uploaded_file ($this->upload_tmp_path)) {
Prevent picture overwrite problems, create a folder for each user
$user _path= $this->file_save_path. $username;
if (!file_exists ($user _path)) {
mkdir ($user _path);
}
$move _to_file= $user _path. " /". $_files[' myfile ' [' name '];
Prevent users from uploading user names the same problem
$file _true_name=$_files[' myfile ' [' name '];
$move _to_file= $user _path. " /". Time (). Rand (1,1000). substr ($this->upload_name,strripos ($this->upload_name,". "));
Echo $upload _file. $move _to_file;
Chinese to transfer code
if (Move_uploaded_file ($this->upload_tmp_path,iconv ("Utf-8", "gb2312", "$move _to_file")) {
echo $this->upload_name. " Upload Success ";
}else{
echo "Upload failed";
}
}else{
echo "Upload failed";
}
}
To get the file name extension
Public Function Getfileext ($filename) {
$fileExt =pathinfo ($filename);
return $fileExt ["extension"];
}
}
?>