谁能给段PHP上传图片并浏览的代码???

解决方案 »

  1.   

    一下代码为混写(在一个叫ad.php的页面中---包括js和html),单图片上传,不知道楼主想要是单的还是多的
    <?phpif(!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>
      

  2.   

    那没办法了,这个代码已经够少的了,而且还包括了html的
      

  3.   


    其实手册那例子就可以了
    复杂可能看我的框架图片上传类。
    可以加水印,切头像图和自动缩放<?php 
    class uploadRouter extends controller{ 
      function index() 
      { 
        //输出上传表单,详见视图文件
      } 
      function webimages() 
      { 
        print_r($_FILES); 
        $img=C("img"); 
        $img->setInfo(  //设置上传文件属性
              array("files"=>"upload", //上传文件框名name
                    "uploadpath"=>$GLOBALS['config']['webprojectpath']."upimages/", //上传目录
                    "icopath"=>$GLOBALS['config']['webprojectpath']."upimages/",    //小图目录 如果不设置将是使用上传目录             
                    "icowidth"=>"128",  //小图宽
                    "icoheight"=>"98",  //小图高
                    "fangpath"=>$GLOBALS['config']['webprojectpath']."upimages/_ico/", //切方图,就是切头像等
                    "fangsize"=>"75", //方图多大 如果设置了切方图那么上传时候会生成切方图,不要可以不设置
                    "nzsize"=>"180",   //自动设置宽或高不超这个 
                    "uploadsize"=>420000 //上传图片大小
                    ) 
              )->setBasename($_FILES['upload']['name'],true)->init();//,'size_ico','auto_ico','fix_ico','fill_size''fix_side' 
             //可以自己设置setBasename名不然会自动生成日期名字,其中会自动把中文换掉成拼音和过滤最后初始化init()
             //初始化完毕,可以上传了
        if($img->upload(array('fix_side'))) //上传了,设置要不要生成小图。有多种格式 如果是空那么只上传图片其它不理。
        { 
          echo("上传成功"); 
        }else{ 
          echo("上传失败"); 
          echo $img->message; 
        } 
        return false; 
      } 

    ?> <html>
    <html>
    <head>
      <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
      <meta name="author" content="Verdana Core, phpdoc.net Inc.">
      <title>上传文件测试</title>
      </head>
    <body>
      <FORM METHOD=POST  enctype="multipart/form-data" ACTION="<?php echo url_for("upload/webimages",true);?>">
      <input type="file" name="upload" id="upload" onchange="preview()"; />
      <br /><br />
      <INPUT TYPE="submit" value="上传图片">
      </FORM>
      </body>
    </html>
    http://code.google.com/p/queryphp/downloads/listframework\lib\img.class.php中的img类
      

  4.   

    php中is_uploaded_file()用它可以获取上传图片的信息吗?
      

  5.   

    is_uploaded_file目前这个函数没多大用处
      

  6.   

    帮我看下为什么我的这代码有什么问题<?php

    if($_SERVER['REQUEST_METHOD']=="POST")
    {
    if(is_uploaded_file($_FILES['dt']))
    {
    echo"<script>alert('ac');history.back()</script>";
    }
    else
    {
    echo"<script>alert('请选择上传的图片');history.back()</script>";
    }

    ?>当我上传的那个文件选择了时也是执行的else语句。这是为什么?
      

  7.   

    is_uploaded_file($_FILES['dt']['tmp_name'])
      

  8.   

    'tmp_name'上传文件的临时文件名(唯一)
      

  9.   

    //html
    <input type="file" name="myfile">//php
    $_FILES['myfile']['tmp_name'];
      

  10.   

    html代码:<form name="myform" method="post" action="">
             <input type="file" name="dt">
             <input type="submit" value="a"/>
             </form>
     
     php代码:<?php

    if($_SERVER['REQUEST_METHOD']=='POST')
    {
    if(is_uploaded_file($_FILES['dt']['tmp_name']))//'tmp_name'上传文件的临时文件名(唯一)
    {
    echo"a";
    }
    else
    {
    echo"<script>alert('请选择上传的图片');history.back()</script>";
    }

    ?>
    还是只执行else语句。
      

  11.   

    <form name="myform" method="post" action="" enctype="multipart/form-data">
      

  12.   

    必须加enctype="multipart/form-data",表示该表单属于文件传输而不是文本传输