PHP File Upload Code usage

Source: Internet
Author: User
Tags file upload php file php file upload

PHP File Upload code writing process

1. First decide whether to upload files

2. If there is any more to determine whether the upload error

3. If an error occurs, the error message is prompted

4. If there is no error, then judge 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 file being uploaded, in bytes (B)

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

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

Let's look at the HTML section first.

The code is as follows Copy Code

? <form action= "upload.php" method= "post" enctype= "Multipart/form-data" >
Upload: <input type= "file" name= "MyFile"/>
<input type= "Submit" name= "submit" value= "Upload"/>
</form>

Description

Form action= "upload.php" refers to the click of the form in the submit, 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" attributes specify what type of content to use when submitting the form, when the form requires binary data, such as the contents of the file, use the "multipart/ Form-data, "If you want to upload a file, this property is necessary." The type= "file" in input provides that the input should be treated as a file, and there will be a browse button behind the input.

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

The code is as follows Copy Code


<?php
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 "<script>alert" (Upload success!) );</script> ";
}
} else{
echo "<script>alert (please upload file!) );</script> ";
}
?>

It's super simple, we're going to upgrade now.

1, upload.php

code as follows copy code

<! 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 brief 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

code as follows copy code


<?php

//Receive
$username =$_post[' username '];
$fileintro =$_post[' Fileintro '];

//echo $username. $fileintro;
//Get file information
/* echo <pre>;
Print_r ($_files);
Echo </pre& gt; ";
/
//Get the size of the 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 whether the upload is OK
if (is_uploaded_file ($_files[' myfile '] [' tmp_name ']) {
///Get uploaded file 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 transcoding
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? This function returns the character of the specified character after the last occurrence of the string. With substr () we can take JPG so we get the suffix name of the file to determine if the uploaded file matches the specified format. This program places the specified format in an array, which can be added as needed when it is actually used.

Next look at the random number of file name parts, we see Mt_srand () This function, the manual called him "sow a better random number generator seed", in fact, is the initialization of a random number of functions, parameters are (double) microtime () * 1000000, here if not this is the parameter will automatically set a random number, of course, this is not in line with our needs, so that a random number on a certain length, to ensure that the upload file does not duplicate

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.