file_size = $file_size; $this->upload(); }"/>
PHP头条
热点:

PHP实现Ajax无刷新图片上传-PHP源码


AjaxUpload.class.php

form_name = $form_name;
        $this->ext_arr = array(
            '.jpg',
            '.png',
            '.gif',
            '.jpeg'
        );
        $this->upload_dir = dirname(__FILE__)."/upload";
        $this->file_size = $file_size;
        $this->upload();
    }

    public function __set($key, $val) {
        $this->$key = $val;
    }

    /**
     * Ajax 无刷新上传图片(jpg|gif|png)
     * @param bool $return_arr 是否返回数组,前提是上传成功
     * @return (array)? || output
     */
    public function upload($return_arr=false) {
        if (!is_dir($this->upload_dir)) mkdir($this->upload_dir, 0777); //上传目录不存在则创建
        $file = $_FILES[$this->form_name];
        if ($file['error']==1 || $file['size']>($this->file_size*1024)) exit('1'); //上传失败,图片不能大于 $this->file_size k!
        switch ($file['error']) {
            case 3:
                exit('3'); //图片只有部分文件被上传,请重新上传!
                break;
            case 4:
                exit('4'); //没有任何文件被上传!
                break;
        }
        $ext = $this->getExt($file['name']);
        if (!in_array($ext, $this->ext_arr)) exit('5'); //非图片类型,请上传jpg|png|gif图片!
        $fname = str_replace('.', '', microtime(true)) . rand(1, 9999) . $ext; //图片名称
        $filename = $this->upload_dir . '/' . $fname;
        if (!move_uploaded_file($file['tmp_name'], $filename)) { //执行上传
            exit('upload error!'); //上传失败,错误未知
        } else {
            $arr = array('ok'=>1, 'filename'=> 'upload/' . $fname, 'size'=>$file['size']);
            if ($return_arr) return $arr;
            else {
                echo json_encode(array('ok'=>$arr['ok'], 'filename'=>$arr['filename'])); exit;
            }
        }
    }

    /**
     * 获取文件后缀名
     * @param string $file_name 文件名称
     * @return string
     */
    private function getExt($file_name) {
        $ext = strtolower(strrchr($file_name, "."));
        return $ext;
    }
}
?>

2. [文件] php 实现无刷新上传.rar ~ 2MB

php+实现无刷新上传.rar

www.phpzy.comtrue/php/35713.htmlTechArticlePHP实现Ajax无刷新图片上传-PHP源码 AjaxUpload.class.php form_name = $form_name; $this->ext_arr = array( .jpg, .png, .gif, .jpeg ); $this->upload_dir = dirname(__FILE__)."/upload"; $this->file_size = $file_size; $this->upload(); }...

相关文章

PHP之友评论

今天推荐