php 圖片上傳並預覽效果

來源:互聯網
上載者:User

if(!isset($_session))session_start();

/**2010-6-22
 * $data 數群組類型    包含以下變數
 -------------------------------
 * $sub_type        submit類型(upload:上傳按鈕    delete:刪除按鈕),預設upload
 * $file            通過表單擷取的$_files['filename']數組
 * $img_tag_id        預覽圖片的<img>的id值
 * $img_dir            上傳圖片的目錄
 * $max_file_size    單位m(兆),預設:1m
 * $type_array        允許的上傳的圖片類型(預設:image/pjpeg、image/jpeg、image/gif)
 **/
function upload($data){
    if(!$data['file']){
        echo '<script>alert("file不可為空!");</script>';exit;
    }
    if(!$data['img_tag_id']){
        echo '<script>alert("預覽圖片標籤id不可為空!");</script>';exit;
    }
    if(!$data['img_dir']){
        echo '<script>alert("圖片上傳目錄不可為空!");</script>';exit;
    }
    if(!isset($data['max_file_size'])){
        $data['max_file_size'] = 1024 * 1024;
    }else{
        $data['max_file_size'] = $data['max_file_size'] * 1024 * 1024;
    }
    if(!isset($data['type_array'])){
        $data['type_array'] = array('image/pjpeg', 'image/jpeg', 'image/gif');
    }
    if(!isset($data['sub_type'])){
        $data['sub_type'] = 'upload';
    }

    $imgpath = '';
    if(isset($data['sub_type']) && $data['sub_type'] == 'delete'){
        if(isset($_session['name']) && $_session['name']){
            if(is_file($_session['imgpath'])){
                $b = unlink($_session['imgpath']);
            }
            unset($_session['name'], $_session['imgpath']);
            if(!isset($_session['name'])){
                echo '<script>alert("刪除成功!");</script>';
                echo '<script>parent.document.getelementbyid("'.$data['img_tag_id'].'").style.display = "none";</script>';
            }else{
                echo '<script>alert("刪除失敗!");</script>';
            }
        }else{
            echo '<script>alert("沒有稿件!");</script>';
        }exit;
    }

    if(isset($_session['imgpath']) && $_session['imgpath']){
        echo '<script>alert("稿件已經存在,要想重新上傳請刪除原來的稿件!");</script>';exit;
    }
    if(!in_array($data['file']['type'], $data['type_array'])){
        echo '<script>alert("稿件類型不符,請上傳.jpg、.gif和.png格式的圖片!");</script>';exit;
    }
    if($data['file']['size'] > $data['max_file_size']){
        echo '<script>alert("您上傳的稿件過大,請選擇2m以下的圖片上傳!");</script>';exit;
    }

    if(!is_dir($data['img_dir'])){
        @mkdir($data['img_dir'], 0777, true);
    }
    $imgpath    = $data['img_dir'].'/'.date('his', time()).rand(100, 999).$data['file']['name'];
    $isupload    = move_uploaded_file($data['file']['tmp_name'], $imgpath);
    if(!$isupload){
        echo '<script>alert("稿件上傳失敗,請嘗試重新上傳!");</script>';exit;
    }else{
        echo '<script>alert("稿件上傳成功!");</script>';
    }
    $_session['name']        = $data['file']['name'];
    $_session['imgpath']    = $imgpath;

    return $imgpath;
}

/*test_start*/
$sub_type = '';
if(isset($_post['submit_upload']))$sub_type = 'upload';
if(isset($_post['submit_delete']))$sub_type = 'delete';
if($sub_type){//echo '<script>alert("'.$sub_type.'");</script>';exit;
    $data = array(    'sub_type'        => $sub_type,
                    'file'            => $_files['file'],
                    'img_tag_id'    => 'picview',
                    'img_dir'        => 'upload_img',
                  );
    $imgpath = upload($data);
}else{
    $imgpath = isset($_session['imgpath'])? $_session['imgpath']: '';
}
?>

<form action="ad.php" method="post" enctype="multipart/form-data" target="frame">
    <input type="file" name="file" class="input">
    <input type="submit" name="submit_upload" value="上  傳">
    <input type="submit" name="submit_delete" value="刪  除">
    <iframe id="frame" name="frame" width="0" height="0" marginwidth="0" frameborder="0" src="about:blank"></iframe>
</form>

<img id="picview" height="100" style="display:none;">

<script>
if("<?php echo $imgpath; ?>"){
    parent.document.getelementbyid("picview").src = "<?php echo $imgpath; ?>";
    parent.document.getelementbyid("picview").style.display = "block";
}
</script>

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.