Php file upload. A php file Upload instance. It Supports txt, rar, zip, jpg, jpeg, gif, png, swf, wmv, avi, wma, mp3, mid, jar, jad, exe, html, htm, css, js, doc Upload, music files, etc. A php Tutorial is a php file Upload instance. It Supports txt, rar, zip, jpg, jpeg, gif, png, swf, wmv, avi, wma, mp3, mid, jar, jad, exe, html, htm, css, js, doc, and music files.
A php Tutorial file Upload instance. It Supports txt, rar, zip, jpg, jpeg, gif, png, swf, wmv, avi, wma, mp3, mid, jar, jad, exe, html, htm, css tutorial, js, doc 'Upload, music files, etc.
Php file Upload
If (! $ _ Post) die ();
$ State = uploadfile ('filedata ');
If ($ state ['err']) {
Die ('script alert ("upload error: '. $ state ['MSG']. '"); history. go (-1); script');
}
Echo'
';
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 );
}
?>
Bytes. A php Tutorial...