亚洲免费乱码视频,日韩 欧美 国产 动漫 一区,97在线观看免费视频播国产,中文字幕亚洲图片

      1. <legend id="ppnor"></legend>

      2. 
        
        <sup id="ppnor"><input id="ppnor"></input></sup>
        <s id="ppnor"></s>

        PHP圖片、文件批量上傳代碼

        字號(hào):


            不管是文件還是圖片批量上傳我們第一個(gè)是在html中做name=userfile[]這種數(shù)組變量,在php接受中我們做for ($_i=0; $_i<$_cont; $_i++)遍歷這樣就可以實(shí)現(xiàn)文件批量上傳了,下面我來(lái)看一個(gè)實(shí)例
            例子
            代碼如下:
            <?php
            session_start();
            ?>
            <!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文件批量上傳</title>
            <style>
            * {margin:0; padding:0; list-style:none;}
            .content {width:400px; height:auto; margin:0 auto; margin-top:60px; padding-bottom:30px; background:#ffd3b6; border:dashed 1px #f90}
            .content h1 { width:400px; height: 30px; line-height:30px; text-align:center; font-family:"微軟雅黑"; font-size:14px; color:#000}
            .content .error {width:300px; height:auto; line-height:30px; text-align:center; margin:0 auto; color:#f00}
            .content .con {width:340px; height:auto; margin:0 auto; font-size:12px;}
            .content #file { width:280px; height:20px; border:solid 1px #ccc; background:#fff; margin:10px 0px 6px 0; font-size:12px;}
            .content #send { width:60px; height:22px; border:solid 1px #ccc; background:#fff; font-size:12px; margin-top:10px;}
            </style>
            <script type="text/javascript" src="js/jquery.js"></script>
            <script>
            $(document).ready(function() {
            $('#cont').val($('.file #file').size());
            $('#send').eq(0).click(function() {
            var filesize=$('.file #file').size();
            $('.file').append("<input type='file' name='userfile[]' id='file'/>");
            $('#cont').val(filesize+1);
            });
            });
            </script>
            </head>
            <body>
            <div>
            <h1>PHP文件批量上傳</h1>
            <div>
            <div>
            <?php
            if ($_GET['up']==up) {
            //防止重復(fù)提交
            if ($_SESSION['file']==$_GET['irand']) {
            $_cont=intval($_POST['cont']); //將file框總數(shù)接收并轉(zhuǎn)換成整型
            $_size=20000; //設(shè)置限制文件大小
            $_dir='pdir/'; //文件保存目錄
            function size($_size) {
            //判斷文件大小是否大于1024bit 如果大于,則將大小取值為KB,以此類推
            if ($_size>1024*1024) {
            return round($_size/1024/1024,2).' MB';
            }else if ($_size>1024) {
            $_size=$_size/1024;
            return ceil($_size).'KB';
            }else {
            return $_size.' bit';
            }
            }
            //設(shè)置上傳圖片的類型,設(shè)置圖片上傳大小
            $_upfiles = array('image/jpeg','image/pjpeg','image/png','image/x-png','image/gif');
            //利用for循環(huán)上傳文件
            for ($_i=0; $_i<$_cont; $_i++) {
            if (is_array($_upfiles)) {
            if (!in_array($_FILES['userfile']['type'][$_i],$_upfiles)) {
            exit('請(qǐng)上傳格式為:jpg,png,gif的文件<br /><a href="pupload.php">返回</a>');
            }
            }
            //判斷文件大小
            if ($_FILES['userfile']['size'][$_i]>$_size) {
            exit('上傳文件不能超過(guò):'.size($_size));
            }
            //檢測(cè)文件是否已經(jīng)上傳
            if ($_FILES['userfile']['error'][$_i]>0) {
            switch ($_FILES['userfile']['error'][1]) {
            case 1: echo '上傳的文件超過(guò)了 php.ini 中 upload_max_filesize 選項(xiàng)限制的值';
            break;
            case 2: echo '上傳文件的大小超過(guò)了 HTML 表單中 MAX_FILE_SIZE 選項(xiàng)指定的值';
            break;
            case 3: echo '文件只有部分被上傳';
            break;
            case 4: echo '沒(méi)有文件被上傳';
            break;
            case 6: echo '找不到臨時(shí)文件夾';
            break;
            case 7: echo '文件寫入失敗';
            break;
            }
            exit;
            }
            //獲取文件擴(kuò)展名
            if (!is_dir($_dir)) {
            mkdir($_dir,0700);
            }
            //生成隨筆數(shù)
            $_rand=mt_rand(0,100000);
            //獲取文件擴(kuò)展名
            $_n=explode('.',$_FILES['userfile']['name'][$_i]); //將文件名分割
            $_file_len=count($_n); //返回?cái)?shù)組長(zhǎng)度
            //確保獲取的擴(kuò)展名是最后一個(gè).后面的
            $_name=$_dir.time().'_'.$_rand.'.'.$_n[$_file_len-1];
            //移動(dòng)文件到指定的目錄
            if (is_uploaded_file($_FILES['userfile']['tmp_name'][$_i])) {
            if (!@move_uploaded_file($_FILES['userfile']['tmp_name'][$_i],$_name)) {
            exit('文件移動(dòng)失敗');
            }else {
            echo '文件上傳成功<br />';
            echo '文件路徑:'.$_name.'<br />';
            echo '文件大小:'.size(filesize($_name));
            echo '<br /><a href="pupload.php">返回繼續(xù)上傳</a>';
            }
            }else {
            exit('上傳的臨時(shí)文件不存在,無(wú)法將文件移動(dòng)到指定文件夾');
            }
            }
            //銷毀session變量,有幾種方法
            //第一種,銷毀所有session變量:session_destroy();
            //第二種:銷毀單個(gè)如:$_SESSION['file']=''
            session_destroy();
            exit;
            }else {
            exit('您已經(jīng)提交過(guò)了,不能重復(fù)提交<br /><a href="pupload.php">返回</a>');
            }
            }
            ?>
            </div>
            <?php $_irand=mt_rand(0,1000000); $_SESSION['file']=$_irand; ?>
            <form action="?up=up&irand=<?php echo $_irand; ?>" method="post" enctype="multipart/form-data">
            <input type="hidden" name="MAX_FILE_SIZE" value="1000000" />
            <input type="hidden" name="cont" value="" id="cont" />
            <div>
            <input type="file" name="userfile[]" id="file"/>
            <input type="file" name="userfile[]" id="file"/>
            <input type="file" name="userfile[]" id="file"/>
            <input type="file" name="userfile[]" id="file"/>
            </div>
            <br />
            <input type="button" name="send" value=" 添加一個(gè) " id="send"/>
            <input type="submit" name="send" value=" 點(diǎn)擊上傳 " id="send"/>
            </form>
            </div>
            </div>
            </body>
            核心原理分析
            在多文件上傳中我們前臺(tái)最重要的是
            代碼如下:
            <input type="file" name="userfile[]" id="file"/>
            <input type="file" name="userfile[]" id="file"/>
            <input type="file" name="userfile[]" id="file"/>
            <input type="file" name="userfile[]" id="file"/>
            這幾行代碼有細(xì)節(jié)的朋友會(huì)發(fā)現(xiàn)name="userfile[]"了,這個(gè)是以數(shù)組存儲(chǔ)了,這樣我們?nèi)绻褂胘s也可以這樣增加就可以了,那么在php是如何獲取的呢
            在多文件上傳中php處理是一個(gè)關(guān)鍵
            代碼如下:
            for ($_i=0; $_i<$_cont; $_i++) {
            if (is_uploaded_file($_FILES['userfile']['tmp_name'][$_i])) {
            if (!@move_uploaded_file($_FILES['userfile']['tmp_name'][$_i],$_name)) {
            exit('文件移動(dòng)失敗');
            }else {
            echo '文件上傳成功<br />';
            echo '文件路徑:'.$_name.'<br />';
            echo '文件大?。?.size(filesize($_name));
            echo '<br /><a href="pupload.php">返回繼續(xù)上傳</a>';
            }
            }
            這里顯示很簡(jiǎn)單我們會(huì)看到有一個(gè)for,for就是遍歷數(shù)組,遍歷userfile[]數(shù)組,然后再由$_FILES['userfile']['tmp_name'][$_i]來(lái)獲取不同文件圖片再進(jìn)行上傳即可,注意[$_i]就是你的多文件上傳項(xiàng)了,只是保存在了數(shù)組中。