Simple PHP file Upload instance

Source: Internet
Author: User
The code is as follows: Copy code

<! 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 = utf-8"/>
<Title> php file Upload </title>
</Head>

<Body>
<Form action = "uploadsir. php "method =" post "enctype =" multipart/form-data "> <input name =" filedata "type =" file "id =" filedata "/>
<Input type = "submit" name = "submit" value = "upload"/> </form>
<? Php
If (! $ _ Post) die ();
$ State = uploadfile ('filedata ');
If ($ state ['err']) {
Die ('<script> alert ("upload error :'. $ state ['MSG ']. '"); history. go (-1); </script> ');
}

Echo '<object type = "application/x-shockwave-flash" data = "template/images/copy.swf? U = '. weburl. $ state ['MSG']. '& quot; width = "100 & quot; height = & quot; 40 & quot;>
<Param name = "movie" value = "copy.swf? U = '. weburl. $ state ['MSG']. '"/> ';


Function uploadfile ($ inputname)
{
$ Immediate = $ _ get ['immediate'];
$ Attachdir = '../pictures'; // save path of the uploaded file. Do not include/
$ Urldir = "../pictures ";
$ Dirtype = 2; // 1: save the directory by day; 2: save the directory by month; 3: save the directory by extension. It is recommended to save the directory by day.
$ Maxattachsize = 2097152; // maximum upload size. The default value is 2 MB.
$ Upext = 'txt, rar, zip, jpg, jpeg, gif, png, swf, wmv, avi, wma, mp3, mid, jar, jad, exe, html, htm, css, js, doc '; // Upload extension
 
$ Err = "";
$ Msg = "";
$ Upfile = $ _ files [$ inputname];
If (! Empty ($ upfile ['error'])
 {
Switch ($ upfile ['error'])
  {
Case '1 ':
$ Err = 'File size exceeds the upload_max_filesize value defined by php. ini ';
Break;
Case '2 ':
$ Err = 'File size exceeds the max_file_size value defined in html ';
Break;
Case '3 ':
$ Err = 'incomplete file upload ';
Break;
Case '4 ':
$ Err = 'No file upload ';
Break;
Case '6 ':
$ Err = 'temporary folder missing ';
Break;
Case '7 ':
$ Err = 'File writing failed ';
Break;
Case '8 ':
$ Err = 'upload interrupted by other extensions ';
Break;
Case '20140901 ':
Default:
$ Err = 'no valid error code ';
  }
 }
Elseif (empty ($ upfile ['tmp _ name']) | $ upfile ['tmp _ name'] = 'none') $ err = 'No file upload ';
Else
 {
$ Temppath = $ upfile ['tmp _ name'];
$ Fileinfo = pathinfo ($ upfile ['name']);
$ Extension = $ fileinfo ['extension'];
If (preg_match ('/'. str_replace (',', '|', $ upext). '/I', $ extension ))
   {
$ Filesize = filesize ($ temppath );
If ($ filesize> $ maxattachsize) $ err = 'File size exceeds '. $ maxattachsize.' Byte ';
Else
    {
Switch ($ dirtype)
     {
Case 1: $ attach_subdir = 'Day _ '. date ('ymmd'); break;
Case 2: $ attach_subdir = 'month _ '. date ('ym'); break;
Case 3: $ attach_subdir = 'ext _ '. $ extension; break;
     }
$ Attach_dir = $ attachdir. '/'. $ attach_subdir;
If (! Is_dir ($ attach_dir ))
     {
@ Mkdir ($ attach_dir, 0777 );
@ Fclose (fopen ($ attach_dir. '/index.htm', 'w '));
     }
Php_version <'4. 100' & mt_srand (double) microtime () * 2.0 );
$ Filename = date ("ymdhis"). mt_rand (1000,9999). '.'. $ extension;
$ Target = $ urldir. '/'. $ attach_subdir. '/'. $ filename;
     
Move_uploaded_file ($ upfile ['tmp _ name'], $ target );
If ($ immediate = '1') $ target = '! '. $ Target;
$ Msg = str_replace ('../', "", $ target );
    }
   }
Else $ err = 'upload file extension must be '. $ upext;

@ Unlink ($ temppath );
 }
Return array ('err' => $ err, 'MSG '=> $ msg );
}
?>
</Body>
</Html>

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.