PHP File Upload _php Tutorial

Source: Internet
Author: User
Tags php file upload
A php file Upload instance Oh, he can support TXT,RAR,ZIP,JPG,JPEG,GIF,PNG,SWF,WMV,AVI,WMA,MP3,MID,JAR,JAD,EXE,HTML,HTM,CSS,JS, Doc ' uploads, music files, etc. are all possible.

PHP Tutorial File Upload instance Oh, he can support Txt,rar,zip,jpg,jpeg,gif,png,swf,wmv,avi,wma,mp3,mid,jar,jad,exe,html,htm,css tutorial, JS, Doc ' uploads, music files, etc. are all possible.





PHP File Upload



if (!$_post) Die ();
$state =uploadfile (' filedata ');
if ($state [' err ']) {
Die (');
}

Echo
';


function UploadFile ($inputname)
{
$immediate =$_get[' immediate '];
$attachdir = '. /pictures ';//upload file save path, end not with/
$urldir = ". /pictures ";
$dirtype =2;//1: Deposit by day Directory 2: Monthly deposit Directory 3: By the extension of the directory recommended to use the storage by day
$maxattachsize =2097152;//max upload size, default is 2m
$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 upload_max_filesize value defined by PHP.ini ';
Break
Case ' 2 ':
$err = ' file size exceeds the max_file_size value defined by HTML ';
Break
Case ' 3 ':
$err = ' File upload incomplete ';
Break
Case ' 4 ':
$err = ' no file upload ';
Break
Case ' 6 ':
$err = ' Missing temp folder ';
Break
Case ' 7 ':
$err = ' Write file failed ';
Break
Case ' 8 ':
$err = ' upload is interrupted by another extension ';
Break
Case ' 999 ':
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. ' bytes ';
Else
{
Switch ($dirtype)
{
Case 1: $attach _subdir = ' Day_ '. Date (' Ymd '); 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.2.0 ' && Mt_srand ((double) microtime () * 1000000);
$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 name extension must be: '. $upext;

@unlink ($temppath);
}
Return Array (' err ' = = $err, ' msg ' = $msg);
}
?>


http://www.bkjia.com/PHPjc/444851.html www.bkjia.com true http://www.bkjia.com/PHPjc/444851.html techarticle a php file upload instance Oh, he can support TXT,RAR,ZIP,JPG,JPEG,GIF,PNG,SWF,WMV,AVI,WMA,MP3,MID,JAR,JAD,EXE,HTML,HTM,CSS,JS, Doc ' uploads, music files, etc. are all possible. A PHP tutorial ...

  • Related Article

    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.