PHP file Programming Comprehensive case-file upload implementation _php Tutorial

Source: Internet
Author: User
PHP File Upload
1, upload.php
Copy CodeThe code is as follows:



<title>Ddd</title>








2, uploadprocess.php
Copy CodeThe code is as follows:
Receive
$username =$_post[' username '];
$fileintro =$_post[' Fileintro '];

echo $username. $fileintro;
Get file information
/* echo "
";
Print_r ($_files);
echo "
";
*/
Get the size of a file
$file _size=$_files[' myfile ' [' Size '];
if ($file _size>2*1024*1024) {
echo "";
Exit ();
}
Get file type
$file _type=$_files[' myfile ' [' type '];
if ($file _type!= "Image/jpeg" && $file _type!= "Image/pjpeg") {
echo "File type can only be in JPG format";
Exit ();
}

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 overlay 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 the same user name issue
$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 Transcode
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. Package:
Copy CodeThe code is as follows:
Class upload{
Public $upload _name; Upload file name
Public $upload _tmp_path; Save upload file 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 "";
Exit ();
}
Get file type
/* IF ($this->file_type!= "Image/jpeg" && $this->file_type!= "Image/pjpeg") {
echo "File type can only be in JPG format";
Exit ();
}
*//Get the file extension
$file _type= $this->getfileext ($this->upload_name);
if (!in_array ($file _type, $this->allow_file_type)) {
echo "Upload file type malformed";
Exit ();
}
Determine if the upload is OK
if (Is_uploaded_file ($this->upload_tmp_path)) {

Prevent picture overlay 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 the same user name issue
$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 Transcode
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";
}
}

Gets the file name extension
Public Function Getfileext ($filename) {
$fileExt =pathinfo ($filename);
return $fileExt ["extension"];
}
}
?>

http://www.bkjia.com/PHPjc/328021.html www.bkjia.com true http://www.bkjia.com/PHPjc/328021.html techarticle php File Upload 1, upload.php copy Code as follows:! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" HTML head titleddd/title meta http-equiv= "Content-type" Conte ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.