PHP AJAXFORM提交圖片上傳並顯示圖片源碼,

來源:互聯網
上載者:User

PHP AJAXFORM提交圖片上傳並顯示圖片源碼,

PHP dofile.php 檔案上傳源碼

<?php$file_upload = "upload/";$file_allow_ext='gif|jpg|jpeg|png|gif|zip|rar|ppt|xls|pdf|pptx|xlsx|docx';$file_allow_size = 5*1024*1024;if($_POST['submit']=="上傳"){  if(is_uploaded_file($_FILES['file']['tmp_name'])){     $file_name  = $_FILES['file']['name'];     $file_error = $_FILES['file']['error'];     $file_type  = $_FILES['file']['type'];     $file_tmp_name = $_FILES['file']['tmp_name'];         $file_size  = $_FILES['file']['size'];     $file_ext   = substr($file_name, strrpos($file_name, '.')+1);     switch($file_error){        case 0:        $data['status'] = 0;        $data['msg']   = "檔案上傳成功!";                break;        case 1:        $data['status'] = 1;        $data['msg']= "檔案上傳失敗,檔案大小".$file_size."超過限制,允許上傳大小".sizeFormat($file_allow_size)."!";             break;        case 3:        $data['status'] = 1;        $data['msg']   = "上傳失敗,檔案只有部份上傳!";             break;        case 4:        $data['status'] = 1;        $data['msg']   = "上傳失敗,檔案沒有被上傳!";              break;        case 5:        $data['status'] = 1;        $data['msg']   = "檔案上傳失敗,檔案大小為0!";             break;      }     if(stripos($file_allow_ext,$file_ext)===false){        $data['status'] = 1;        $data['msg'] = "該副檔名不允許上傳";           }     if($file_size>$file_allow_size){              $data['status'] = 1;        $data['msg']= "檔案大小超過限制,只能上傳".sizeFormat($file_allow_size)."的檔案!";        }        if($data['status']==1){        $data['status'] = 1;        $data['msg'] = $data['msg'];        exit(json_encode($data));       }     if($data['status']==0){        if(file_exists($file_upload)){                     $file_new_name = date("YmdHis").'_'.rand(10000,99999).'.'.$file_ext;            $file_save_path = $file_upload.$file_new_name;              $data['status'] = 0;                   $data['url'] = $file_save_path;            move_uploaded_file($file_tmp_name,$file_save_path);            exit(json_encode($data));                      }else{            exit(json_encode($data));        }             }           }}function sizeFormat($size){    $sizeStr='';    if($size<1024)    {        return $size."bytes";    }    else if($size<(1024*1024))    {        $size=round($size/1024,1);        return $size."KB";    }    else if($size<(1024*1024*1024))    {        $size=round($size/(1024*1024),1);        return $size."MB";    }    else    {        $size=round($size/(1024*1024*1024),1);        return $size."GB";    } }?>

HTML如下

<script type="text/javascript" src="http://lib.sinaapp.com/js/jquery/1.9.1/jquery-1.9.1.min.js"></script><script type="text/javascript" src="http://files.cnblogs.com/files/china-li/jquery.form.js"></script><form action="dofile.php" method="post" enctype="multipart/form-data" id="upfileimage"><input type="hidden" name="image[]"  /><label for="file">檔案:</label><input type="file" name="file" id="file" /><input type="submit" name="submit" value="上傳" /></form><script type="text/javascript">$("#upfileimage").submit(function(){    if($("input[type=file]").val()==""){        alert("請選擇要上傳的檔案");        return false;    }})$(function(){    var options = {             type:"POST",        dataType:"json",        resetForm:true,        success:function(o){          if(o.status==1){            alert(o.msg);          }else{            $("body").append("&nbsp;&nbsp;<img src='"+o.url+"' alt='' width='100' /><input type='hidden' name='image[]' value='"+o.url+"'  />");          }        },        error:function(o){            alert(o.message);        }           }    $("#upfileimage").ajaxForm(options).submit(function(){return false;});})</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.