-------uploaded code, source Wisdom Podcast "PHP from Beginner to proficient" 124th speaking php file programming ③ File upload details discussion---------
<?php
1. The user receiving the submitted file
$username =$_post[' username '];
$fileintro =$_post[' Fileintro '];
(1), how to control the user upload file size < 2m
$file _size=$_files[' myfile ' [' Size '];
if ($file _size>2*1024*1024) {
echo "File too large, can not upload more than 2m files";
Exit ();
}
(2), how to control the user upload file type (problem!)
$file _type=$_files[' myfile ' [' type '];
if ($file _type!= ' image/jpeg ' && $file _type!= ' Image/pjpeg ') {
echo "File type can only be JPG";
Exit ();
}
Determine if upload OK
if (Is_uploaded_file ($_files[' myfile ' [' tmp_name '])) {
Dump the files into the directory you want
$uploaded _file=$_files[' myfile ' [' tmp_name '];
(3), how to prevent the user picture overlay problem, we give each user dynamically create a folder
$user _path=$_server[' Document_root ']. " /file/up/". $username;
Determine if the user already has a folder
if (!file_exists ($user _path)) {
mkdir ($user _path);
}
$move _to_file=$_server[' Document_root ']. " /file/up/". $_files[' myfile ' [' name '];
(4), how to prevent the same user to upload the same file name problem, we use time (). Rand (1,1000) method
$file _true_name=$_files[' myfile ' [' name '];
$move _to_file= $user _path. " /". Time (). Rand (1,1000). substr ($file _true_name,strrpos ($file _true_name,". "));
if (Move_uploaded_file ($uploaded _file,iconv ("Utf-8", "gb2312", $move _to_file))) {
echo $_files[' myfile ' [' Name ']. " Upload Success ";
Echo $uploaded _file. $move _to_file;
}else{
echo "Upload failed";
}
}else{
echo "Upload failed";
}
------------downloaded code, Source Wisdom Podcast "PHP from Beginner to proficient" 87th, HTTP protocol depth analysis ④-------
The code is as follows:
<?php
Description of the function
Parameter description $file _name file name
$file _sub_dir: The sub-path of the download file '/xxx/xxx/'
Functiondown_file ($file _name, $file _sub_dir) {
Die, demo download a picture.
If the file is in Chinese.
Reason PHP file functions, older, need to Chinese transcoding gb2312
$file _name=iconv ("Utf-8", "gb2312", $file _name);
Absolute path
$file _path=$_server[' Document_root '). $file _sub_dir. $file _name;
1. Open File
if (!file_exists ($file _path)) {
echo "file does not exist!";
return;
}
$FP =fopen ($file _path, "R");
2. Working with files
Get the size of the download file
$file _size=filesize ($file _path);
if ($file _size>30) {
echo "<script language= ' JavaScript ' >window.alert (' too big ') </script>";
Return
}
The returned file
Header ("Content-type:application/octet-stream");
Returns by byte size
Header ("Accept-ranges:bytes");
Return file size
Header ("Accept-length: $file _size");
Here the client's popup dialog box, corresponding to the file name
Header ("content-disposition:attachment; Filename= ". $file _name);
Loopback data to the client
$buffer = 1024;
In order to download the security, we'd better make a file byte read counter
$file _count=0;
This sentence is used to determine whether the file ends
while (!feof ($fp) && ($file _size-$file _count>0)) {
$file _data=fread ($fp, $buffer);
How many bytes did the statistic read?
$file _count+= $buffer;
Send some data back to the browser;
Echo$file_data;
}
Close File
Fclose ($FP);
}
File upload Download