PHP File Upload code usage _php Tutorial

Source: Internet
Author: User
PHP File Upload code writing process

1. First determine whether to upload the file

2. If there is any further error in the upload

3. If an error occurs, an error message is indicated

4. If no error is found, then determine the file type

5. If the type meets the criteria, then determine if the file exists in the specified directory

6. If not, move the file to the specified directory

A few things to know about uploading files in PHP

$_files[' myfile ' [' name '] refers to the name of the file being uploaded

$_files[' myfile ' [' type '] refers to the type of file being uploaded

$_files[' myfile ' [' size '] refers to the size of the uploaded file, in bytes (B)

$_files[' myfile ' [' tmp_name '] means the file name of the temporary copy of the file being uploaded to the server, and the file is moved to the specified directory and the pro file is automatically destroyed.

$_files[' myfile ' ["error"] refers to the status code of errors that may appear in the upload of a file, which is explained after each state meaning.

Let's look at the HTML section first.

The code is as follows Copy Code

?

Description

Form action= "upload.php" refers to the click on the form of the submission, the upload command will be sent to this page called upload.php to deal with. Method= "POST" means to send by post, enctype= "Multipart/form-data" attribute specifies what type of content to use when submitting this form, and when the form requires binary data, such as the file content, use the "multipart/ Form-data ", this property is necessary if you want to upload a file. The type= "file" in input specifies that the input should be treated as a file, and there will be a browse button behind input.

Let's look at a PHP processing page upload.php

The code is as follows Copy Code


if ($_files[' myfile ' [' name ']! = ') {
if ($_files[' myfile ' [' Error '] > 0) {
echo "Error status:". $_files[' myfile ' [' Error '];
} else {
Move_uploaded_file ($_files[' myfile ' [' Tmp_name '], "uploads/". $FILES [' myfile '] [' name ']);
echo "";
}
} else{
echo "";
}
?>

It's super simple, let's upgrade now.

1, upload.php

The code is as follows Copy Code




Ddd






2, uploadprocess.php

The code is as follows Copy Code


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";
}

?>

Attention:

Let me give you an example of a picture file pic.jpg, we use STRRCHR processing, STRRCHR (pic.jpg, '. '), it will return. jpg, do you understand? The function returns the character of the specified character after the last occurrence of the string. With substr () we can take a jpg, so we get the file suffix, to determine whether the upload file conforms to the specified format. This program puts the specified format in an array, which can be added as needed when actually used.

Next look at the generation of random number filename part, we see Mt_srand () This function, the manual called him "sow a better random number generator seed", is actually the initialization of a random number function, the parameter is (double) microtime () * 1000000, here if not this is the parameter will automatically set a random number, of course, this does not meet our needs, so that the random number has a certain length, to ensure that the upload file does not have the same name

http://www.bkjia.com/PHPjc/372030.html www.bkjia.com true http://www.bkjia.com/PHPjc/372030.html techarticle php File Upload code writing process 1. First to determine whether to upload the file 2. If there is any further to determine if there is an error in the upload 3. If an error occurs, an error message 4 is indicated. If there is no error, then judge the text ...

  • 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.