PHP uploads instance code and prevents repeated uploads

Source: Internet
Author: User

PHP uploads the instance code to prevent repeated uploads. This upload instance code is very suitable for beginners of php to learn and has detailed annotations.
<? Php
Session_start ();
/****** The following can be used to track users:
Else $ sess_id = session_id ();
Between $ id = rand (between 0000000000,9999999999999999 );
******/
If (commit $ _ POST ["Submit"]) {
/****** The following is to prevent repeated uploads, which is applicable only once.
If (Response $ _ SESSION ["name"] = "1 "){
Echo "<p> Please do not submit it again! </P> ";
Exit;
}
******/
Program $ file_name = program $ _ FILES ["file"] ["name"];
Objects $ file_size = objects $ _ FILES ["file"] ["size"];
Parameters $ file_type = parameters $ _ FILES ["file"] ["type"];
Consumed $ file_tn = time (). consumed $ file_name;
Optional $ save_path = "upfiles /";
Except $ messg = "<p> when an object is uploaded: </p> <a href =? Id = ". Retry $ id."> return retry </a> ";
Required $ messg_sr = required $ messg;
If (Bytes $ file_type! = "Application/msword") {// breeze prompt, the upload format is limited to word
Optional $ messg. = "<p> the format of the uploaded file is MS wordand the general extension name is .doc </p> ";
}
If (Response $ file_size> 1048576) {// breeze prompt, which can be written as "if (Response $ file_size> 1*1024*1024) {" Easy to modify
Created $ messg. = "<p> the size of the uploaded file cannot exceed 1 MB. The size of this file is ". round (Bytes $ file_size/1024/1024), 2 ). "MB </p> ";
}
If (else $ messg! = Orders $ messg_sr ){
Echo done $ messg;
} Else {
If (move_uploaded_file (packages $ _ FILES ["file"] ["tmp_name"], packages $ save_path. Packages $ file_tn )){
// Resume $ _ SESSION ["name"] = "1"; # prevent repeated uploads from matching the above
// The following are the prompts and jumps for successful uploads:
// Echo "<p> congratulations! The file is uploaded successfully. </P> ";
// Echo "<script language = javascript> close (); </script> ";
// Echo "<script> location. href =; </script> ";
// Echo "<meta http-equiv =" refresh "content =" 0; URL = http://domain.com/test.html ">"; # prototype
Echo "<meta http-equiv =" refresh "content =" 3; URL =/"> <div id =" container "style =" margin: 40px; padding: 40px; text-align: center; font-size: 12px; border: # 0099FF double; "> upload successful! </Div> ";
} Else {
Echo done $ messg;
}
}
} Else {
?>
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312"/>
<Title> File Upload </title>
<Style type = "text/css">
<! --
. Pt11 {
Font-size: 11pt;
Color: #333333;
}
-->
</Style>
</Head>

<Body>
<Form id = "form1" name = "form1" method = "post" enctype = "multipart/form-data" action = "">
<Table width = "500" border = "0" align = "center" cellspacing = "1" bgcolor = "#666699" class = "pt11">
<Tr>
<Td height = "25" bgcolor = "# 9999FF"> upload </td>
</Tr>
<Tr>
<Td height = "60" align = "center" bgcolor = "# F1F1F1">
<Input name = "file" type = "file" id = "file" size = "32"/>
</Td>
</Tr>
<Tr>
<Td height = "25" align = "center" bgcolor = "# E1E1E1">
<Input type = "submit" name = "Submit" value = "submit"/> </td>
</Tr>
</Table>
</Form>

</Body>
</Html>
<? Php }?>
 

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.