原生文件上传

如果你的项目对上传不需要很强大的上传,就可以采用下面的函数,进行传输!

public function GrabImage($data) {

        $php_path = dirname(__FILE__) . ‘/‘;
        $php_url = dirname($_SERVER[‘PHP_SELF‘]) . ‘/‘;
        //文件保存目录路径
        $save_path = $php_path . ‘../../../upload/image/leavemessagimg/‘;

        //文件保存目录URL
        $save_url = $php_url . ‘../../../upload/image/leavemessagimg/‘;

        //新文件名
        $file_name = $_FILES[‘uploadfiles‘][‘name‘];
        $temp_arr = explode(".", $file_name);
        $file_ext = array_pop($temp_arr);
        $file_ext = trim($file_ext);
        $file_ext = strtolower($file_ext);

        $new_file_name = date("YmdHis") . ‘_‘ . rand(10000, 99999) . ‘.‘ . $file_ext;

        $file_path = $save_path . $new_file_name;

        if ((($data[‘uploadfiles‘]["type"] == "image/gif")
        || ($data[‘uploadfiles‘]["type"] == "image/jpeg")
        || ($data[‘uploadfiles‘]["type"] == "image/pjpeg"))
        && ($data[‘uploadfiles‘]["size"] < 1000000))
          {
          if ($data[‘uploadfiles‘]["error"] > 0)
            {
            echo "Return Code: " . $data[‘uploadfiles‘]["error"] . "<br />";
            }
          else
            {

            if (file_exists($file_path))
              {
              echo $data[‘uploadfiles‘]["name"] . " already exists. ";
              }
            else
              {
                  if(move_uploaded_file($data[‘uploadfiles‘]["tmp_name"],$file_path)==false)
                  {
                      echo ‘上传失败‘;exit;
                  }
                  @chmod($file_path, 0644);

                  //保存图片路径
                  $file_url = ‘/upload/image/leavemessagimg/‘. $new_file_name;
              }
            }
          }
        else
          {
          echo "Invalid file";
          }
          return $file_url;
    }

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。