ThinkPHPʵÏÖͼƬÉÏ´«²Ù×÷µÄ·½·¨Ïê½â

5年以前  |  阅读数:668 次  |  编程语言:PHP 

±¾ÎÄʵÀý½²ÊoÁËThinkPHPʵÏÖͼƬÉÏ´«²Ù×÷µÄ½¨¡£*ÖÏi¸ø´o¼Ò¹(C)´o¼Ò²Î¿¼£¬¾ßÌaÈçÏ£º

Ö±½ÓÉϸoÀý×Ó£¬ÆaÖаuÀ¨Óе¥Í¼Æ¬ÎļþÉÏ´«¡¢¶aͼƬÎļþÉÏ´«¡¢ÒÔ¼°É¾³ýÎļþµÄÒ»Ð(C)²Ù×÷¡¢ÅÖÃɾ³ýÊý¾Ý¿aµÄʱºo£¬½o½oɾ³ýµoÁËÊý¾Ý¿aÖ®ÖеÄÎļþ¾¶¡¢¶ø²»ÊÇÒ»²¢É¾³ýþÎñÆ÷Ö®ÖеÄÎļþ¡¢ÅÖÃ*þÎñÆ÷±¬Õ¨¡¢¡¢

TPÀiÃaecommonÎļþ¼ÐÀiÃaefunction.phpÀiÃae×Ô¶¨Òa½¨£º


    <?php
    //ÎļþÉÏ´«Àa£¨¿ÉÒÔÉeÖöa¸o²ÎÊý£(C)
    function upload($file=null,$maxSize=0,$exts=0,$savePath='')
    {
      //µ÷ÓÃ
      $upload = new \Think\Upload();// ʵÀý»¯ÉÏ´«Àa
      $upload->maxSize  = $maxSize;// ÉeÖø½¼þÉÏ´«´oС
      $upload->exts   = $exts; //array('jpg', 'gif', 'png', 'jpeg'); ÉeÖø½¼þÉÏ´«ÀaÐÍ
      $upload->savePath = $savePath; // ÉeÖø½¼þÉÏ´«Ä¿Â¼
      // ÉÏ´«Îļþ
      //Èç¹uµ¥¸oÎļþ»¹ÊǶa¸oÎļþ
      if($file){
       $info = $upload->uploadOne($file);
      }else{
      $info = $upload->upload();
      }
      //Åж¨ÊÇ*ñÎļþÉÏ´«³É¹¦de
      if(!$info) {
        return false;
      }else{
      // ÉÏ´«³É¹¦,
        return $info;
      }
    }
    //ÉÏ´«Í¼Æ¬
    function fab_upload($files ,$maxSize = 0,$exts = null,$savePath = '')
    {
      //Åж¨ÎļþÐÅÏ¢ÊÇ*ñΪ¿Õ
      if(empty($files)){
        return false;
      }
      if($exts === null){
        $exts = array('jpg', 'gif', 'png', 'jpeg');
      }else{
        $exts = 0;
      }
      $tmp = array();
      //½«ÎļþÐÅÏ¢£¨Êý×e£(C)ÓÃforeachÑ­»*±eÀu£¬
      foreach($files as $k => $v){
      //Åж¨Îļþ´oÓÚ0Ö®ºo£¬½«±eÀuvalue×÷Ϊ²ÎÊý´«Èeupload*½*¨
        if($v['size'] > 0){
          $res = upload($v,$maxSize,$exts,$savePath);
          //Èç¹u´«Èe³É¹¦¾Í»a½«Îļþ´ae´¢Â*¾¶´«ÈeÊý×e$tmp[]Ö®ÖÐ
          if($res){
            $tmp[$k] = $res['savepath'].$res['savename'];
          }
        }
      }
      //½«´ae´¢´«ÈeÎļþÂ*¾¶µÄÊý×ereturn»ØÈ¥
      return $tmp;
    }
    ?>

ÆaʵÎÞÂÛÄĸoÎļþÉÏ´«¡¢¶¼ÊÇÐeÒªÓÃ$_FILES±aÁ¿Çø²Ù¿ØµÄ¡¢

ÉÏÃaeµÄ½¨ÊÇfab_uploadµ÷ÓÃupload½¨µÄ£»

ÔÚHTMLÉÏÎÒÃDZiµ¥Êǽ´×ÏдµÄ£º


    <form action="{:U('Index/infoupload')}" method="post"style="overflow: hidden;clear: both;" enctype="multipart/form-data">
    <div class="contact_r col-md-4">
      <label class="contact_rc contact_file"><span><b>Èę×C£º</b><input class="inp_zj1" type="file" name="rutaiimg" ></span></label>
      <!-- <a class="contact_sp fancybox" href="images/txz1.jpg" rel="external nofollow" rel="external nofollow" >ÈçˆDʾ</a> -->
    </div>
    <div class="contact_r col-md-4">
      <label class="contact_rc contact_file"><span><b>ͨÐÐ×C£º</b><input class="inp_zj2" type="file" name="tongxingimg" ></span></label>
      <!-- <a class="contact_sp fancybox" href="images/txz1.jpg" rel="external nofollow" rel="external nofollow" >ÈçˆDʾ</a> -->
    </div>
    </form>

¿ØÖÆÆ÷Ö®ÖÐÈçºÎ´¦ÀiÉÏ´«µÄÎļþ£¨Æ´½ÓÂ*¾¶ÒÔ¼°ÎļþÃu¡¢»¹ÓÐÈe¿aʧ°ÜÐeҪɾ³ýÎļþ£¬ÀaËƻص÷£(C)


    /*µ÷ÓÃдºÃµÄ*½*¨½øÐÐÑeÖ¤*/
    $new_thumb = fab_upload($_FILES);
    // var_dump($new_thumb);die;
    $input['data']['addtime']=time();//Éu³ÉÉeÇeʱ¼a
    $input['data']['pretime']=strtotime($input['data']['pretime']);//½«´«¹ýÀ´µÄÈÕÆÚת»»³Éʱ¼a´Á
    if($new_thumb && count($new_thumb) > 0){
        $input['data'] = array_merge($input['data'],$new_thumb);
    }
    $f = $customer->add($input['data']);
    if($f){
        $this->display('Index/infosuccess');
        // $this->success("Ìi¼Ó³É¹¦£¡",U('Index/infocheck',array('iccid'=>$input['data']['iccid'])));
    }else{//Êý¾ÝÌi¼Óʧ°Ü¼´É¾³ýÕÕƬ
        if($new_thumb){
          $p = C('UNLINK_PATH').$new_thumb;
          unlink($p);
        }
        $this->error("Ìi¼Óʧ°Ü£¡Ö¤¼þ¿ÉÄÜÒÑ´aeÔÚ");
    }

ÆaÖÐUNLINK_PATH±aÁ¿ÔÚThinkPHPÖ®ÖеÄconfigÎļþÀiÃae¶¨Òa¡¢ÊÇÂ*¾¶À´µÄ


    <?php
    return array(
      'DB_TYPE'  => 'mysql', // Êý¾Ý¿aÀaÐÍ
      'DB_HOST'  => 'localhost', // *þÎñÆ÷µØÖ*
      'DB_NAME'  => 'urban', // Êý¾Ý¿aÃu
      'DB_USER'  => 'root', // Óû§Ãu
      'DB_PWD'  => '123456', // ÃÜÂe
      'DB_PORT'  => 3306, // ¶Ë¿Ú
      'DB_PREFIX' => 'fab_', // Êý¾Ý¿a±iǰ׺
      'DB_CHARSET'=> 'utf8', // ×Ö*u¼¯
      'CHECK_ROOT' => true, //¿ªÆorbacȨÏÞ
      'TMPL_CACHE_ON' => false,    // ÊÇ*ñ¿ªÆoÄ£°a±aÒe»º´ae,ÉeΪfalseÔoÿ´Î¶¼»aÖØбaÒe
      'ACTION_CACHE_ON' => false, // ĬÈϹرÕAction »º´ae
      'HTML_CACHE_ON'  => false,  // ĬÈϹرվ²Ì¬»º´ae
      'FILE_PATH'=>'http://localhost/urban/Uploads/',
      'WEB_PATH'  =>  'http://localhost/urban/index.php/',
      'WEB_URL'  =>  'http://localhost/urban/',
      'UNLINK_PATH'  =>  './Uploads/',
      'PWD_KEY'  => 'jeiskAsdlLsdfqaiocvwphxzbtu',
      'AUTO_LOGIN_TIME'=>3600 * 24 * 7,
      'SHOW_PAGE_TRACE'=>true, //×*×Ùģʽ
      'MY_CATCH_DIR' =>'./cache/', //»º´aeĿ¼
      'CODE_PATH' =>'http://localhost/urban/fabp/phpqrcode/',  // ´ae*ŶþάÂeµÄĿ¼
      'qq_face' =>'http://localhost/urban/Public/site/images/arclist/',   //qq±iÇeÂ*¾¶
      'wxlogin' => array(
        'appid' => 'wx35f5b9e9b90539ae',
        'AppSecret' => '4de424bee1529a8abeda9c0c52aad3aa',
        'callback' => 'http://localhost/urban/index.php/Home/Login/call_back.html'
        ),
      'topic_pass'=>false,  //ÊÇ*ñ¿ªÆo»°ÌaÉoºË
    );

µ±Ìi¼ÓÒÔºo£¬×ÔÈ»ÐeÒªÔÚºǫ¹ÜÀiÄ£¿eÉÏÌi¼Óɾ³ýµÄfunction

ÉÏÃaeµÄÏÔʾͼƬµÄʱºo£¬ÓÃHTTPЭÒeµÄ¾ø¶ÔÂ*¾¶Æ´½Ó³oÀ´ÏÔʾͼƬ£»

¶øɾ³ýͼƬÔoÊÇ£¬ÒÔÈe¿ÚÎļþindex.phpΪ׼£¬Êǵ±Ç°Îļþ¼ÐÏÂÃaeµÄuploadÎļþ¼Ð£»

¼Çסµ÷ÓÃThinkPHPÖ®ÖеÄupload¡¢uploadone½¨*µ»ØÀ´µÄÖ»ÊÇÉÏ´«ÎļþÔÚuploadÎļþ¼ÐÏÂÃaeµÄ´ae´¢Î»Öᢡ°'2016-09-02/57c94e71f0916.png'¡±£¨Èe¿aÒ²Õa¸o°É£(C)

ËuÒÔÎÞÂÛɾ³ý»¹ÊÇÏÔʾ¶¼ÐeÒªÓÃC½¨Æ´½ÓÒ»ÏÂ


    if(IS_POST){
        $input=I('post.');
        $ids=implode(',',$input['id']);
        $brand=D('brand');
        $img=$brand->where("brand_id in ($ids)")->getField('thumb',true);
        foreach($img as $v){
          $p = C('UNLINK_PATH').$v;
          unlink($p);
        }
        $res=$brand->where("brand_id in ($ids)")->delete();
        if($res){
          $this->success("ɾ³ýÔËÓªÉÌÆ*ÅƳɹ¦£¡");
        }else{
          $this->error("ɾ³ýÔËÓªÉÌÆ*ÅÆʧ°Ü!");
        }
    }

Ö®ËuÒÔÓÃÁËÄǸoforeach£»ÊÇÒoΪ´«¹ýÀ´µÄid²»ÊÇΨһһ¸o£»ÊǶaÑ¡£¬É¾³ý£»

¶aÑ¡£¬²¢ÇÒ´«¹ýÈ¥ÏaÓ¦À¸Ä¿IDµÄÖµÊÇÈçºÎʵÏÖµÄÄØ


    <foreach name="list" item="v">
      <tr>
        <td class="center" width="80px">
          <label>
            <input type="checkbox" class="ace" name="id[]" value="{$v.brand_id}"/>
            <span class="lbl"></span>
          </label>
        </td>
        <td>{$v.brand_name}</td>
      </tr>
    </foreach>
    <tr>
        <td colspan="2">
          <button class="btn btn-xs btn-danger" onclick="return tijiao('del')">
            <i class="icon-trash bigger-110"></i>
            ɾ³ý
          </button>
         </td>
      </tr>

ÉÏÃaeɾ³ýµÄjavascript½¨ÊÇÕaÑuдµÄ£º


    <script type="text/javascript">
    function tijiao(type){
      if(type == 'del'){
        $('#my_form').attr('action',"{:U('Admin/Brand/brand_del')}");
      }else if(type == 'sort'){
        $('#my_form').attr('action',"{:U('Admin/Brand/brand_sort')}");
      }
      return true;
    }
    </script>

¸½¼Ó£ºÆaʵÅж¨ÎļþÊÇ*ñÓÐÉÏ´«×iºÃÓÃÕa¸oÊý¾Ý£º


    $_FILES['input_name']['size']

ÊÇ*ñ´oÓÚÁa£»

I can see a bigger world.

¸u¶a¹ØÓÚthinkPHPÏa¹ØÄÚÈݸÐÐËȤµÄ¶ÁÕ߿ɲe¿´±¾Õ¾×¨Ìa£º¡¶ThinkPHPÈeÃŽ̡̳¡¢¡¶thinkPHPÄ£°a²Ù×÷¼¼ÇÉ×ܽa¡¡¢¡¶ThinkPHP³£Óý¨×ܽa¡¡¢¡¶codeigniterÈeÃŽ̡̳¡¢¡¶CI(CodeIgniter)¿o¼Ü½ø½×½Ì³Ì¡¡¢¡¶Zend FrameWork¿o¼ÜÈeÃŽ̡̳¼°¡¶PHPÄ£°a¼¼Êo×ܽa¡*¡£

Ï£Íu±¾ÎÄËuÊo¶Ô´o¼Ò»uÓÚThinkPHP¿o¼ÜµÄPHP³ÌÐoÉe¼ÆÓÐËu°iÖu¡£

 相关文章:
PHP分页显示制作详细讲解
SSH 登录失败:Host key verification failed
获取IMSI
将二进制数据转为16进制以便显示
获取IMEI
文件下载
贪吃蛇
双位运算符
PHP自定义函数获取搜索引擎来源关键字的方法
Java生成UUID
发送邮件
年的日历图
提取后缀名
在Zeus Web Server中安装PHP语言支持
让你成为最历害的git提交人
Yii2汉字转拼音类的实例代码
再谈PHP中单双引号的区别详解
指定应用ID以获取对应的应用名称
Python 2与Python 3版本和编码的对比
php封装的page分页类完整实例