Configure Thinkphp3.2 to support seven cow image upload method

Source: Internet
Author: User
Tags md5 sha1

As a beginner in PHP, use seven cows on a company-based thinkphp framework project (originally using the C # version of the SDK). Now in the official find PHP official SDK, finally found that the original thinkphp has supported the driver type is Qiniu file upload.

Now describe the configuration.

Use seven Neu Yun private space to store files


Register Seven Cow cloud, create space, make space private
Something to write down:
Ak,sk,bucket

Add in config.php

Seven cows upload file settings
' Picture_upload_driver ' => ' Qiniu ',
Local upload file driver configuration
' Upload_local_config ' =>array (),
' Upload_qiniu_config ' =>array (
' AccessKey ' => ' l3n4q0xcqm0rssaamthryzyg-lnkmh ',
' Secrectkey ' => ' 7QPVAERASASAN2TJALQQAJF0H6JOT0D1JF ',
' Bucket ' => ' Yaasasnbao ',
' Domain ' => ' 7xasssa.com2.z0.glb.qiniucdn.com ',
' Timeout ' =>3600,
),

3, add an upload class model


<?php
// +----------------------------------------------------------------------
// | Onethink [WE CAN do it JUST it]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author:huajie <banhuajie@163.com>
// +----------------------------------------------------------------------
namespace Admin\model;
Use Think\model;
Use Think\upload;
/**
* Picture model
* Responsible for uploading the pictures
*/
Class Picturemodel extends model{
/**
* Automatic Completion
* @var Array
*/
Protected $_auto = Array (
Array (' status ', 1, Self::model_insert),
Array (' Create_time ', Now_time, Self::model_insert),
);
/**
* File Upload
* @param array $files The list of files to upload (usually the $_files array)
* @param array $setting file upload configuration
* @param string $driver upload driver name
* @param array $config upload driver configuration
* @return Array File upload information after the successful
*/
Public function upload ($files, $setting, $driver = ' local ', $config = null) {
/* Upload file * *

$Upload = new Upload ($setting, $driver, $config);
$info = $Upload->upload ($files);
if ($info) {//File uploaded successfully, log file information

return $info; File Upload Successful
} else {
$this->error = $Upload->geterror ();
return false;
}
}
/**
* Download the specified file
* @param number $root file storage root directory
* @param integer $id file ID
* @param string $args callback function argument
* @return Boolean false-download failed, otherwise output download file
*/
Public function Download ($root, $id, $callback = null, $args = null) {
/* Get download file information * *
$file = $this->find ($id);
if (! $file) {
$this->error = ' No such file exists! ';
return false;
}
* * Download File/*
Switch ($file [' location ']) {
Case 0://download Local files
$file [' rootpath '] = $root;
return $this->downlocalfile ($file, $callback, $args);
Case 1://todo: Downloading remote FTP files
Break
Default
$this->error = ' Unsupported file storage type! ';
return false;
}
}
/**
* Detects if the currently uploaded file already exists
* @param array $file file upload
* @return Boolean file information, false-the file does not exist
*/
Public function Isfile ($file) {
if (Empty ($file [' MD5 '])) {
throw new \exception (' Missing parameter: MD5 ');
}
/* Find File * *
$map = Array (' MD5 ' => $file [' MD5 '], ' SHA1 ' => $file [' SHA1 '],);
Return $this->field (true)->where ($map)->find ();
}
/**
* Download Local files
* Array $file file information @param array
* @param callable $callback Download callback function, commonly used to increase the number of downloads
* @param string $args callback function argument
* @return Boolean download failed to return false
*/
Private Function Downlocalfile ($file, $callback = null, $args = null) {
if (Is_file ($file [' RootPath ']. $file [' Savepath ']. $file [' Savename ']) {
/* Call callback function new download number * *
Is_callable ($callback) && call_user_func ($callback, $args);
/* Execute download///todo: Large file breakpoint continued transmission
Header ("Content-description:file Transfer");
Header (' Content-type: '. $file [' type ']);
Header (' Content-length: '. $file [' size ']);
if (Preg_match ('/msie/', $_server[' http_user_agent ')) {//for IE
Header (' Content-disposition:attachment filename= '. Rawurlencode ($file [' name ']). '"');
} else {
Header (' Content-disposition:attachment filename= '. $file [' name ']. '"');
}
ReadFile ($file [' RootPath ']. $file [' Savepath ']. $file [' savename ']);
Exit
} else {
$this->error = ' file has been deleted! ';
return false;
}
}
/**
* Erase data that exists in the database but does not exist locally
* @param $data
*/
Public Function Removetrash ($data) {
$this->where (Array (' ID ' => $data [' id '],))->delete ();
}
}

4, modify the Call upload class code


Public Function Imgupload ()
{
$this->error ("No papers!") ");
TODO: User logon detection
/* Call File Upload component upload file * *
$Picture = D (' picture ');
$pic _driver = C (' Picture_upload_driver ');
$info = $Picture->upload (
$_files,
C (' Picture_upload '),
C (' Picture_upload_driver '),
C ("upload_& #123;& #36;p ic_driver& #125; _config")
); TODO: Upload to remote server
/* Record picture information * *
if ($info) {
/* Returns JSON data * *
echo Json_encode ($info);

} else {
echo Json_encode ($info);
}
}

5, in the client use Ajaxfileupload invoke PHP method


Upload pictures
$ (document). On (' Change ', ' #upfile ', function () {
$.ajaxfileupload ({
URL: ' {: U (' Company/imgupload ')} ',
Secureuri:false,
Fileelementid: ' Upfile ',
DataType: ' JSON ',
Type: ' Post ',
Data: {fileelementid: ' Upfile '},
Success:function (data) {

$ (' #showimg '). attr (' src ', data.upfile.url);
$ (' #imageurl '). Val (Data.upfile.url);
}
})
})
It's best to use the data type of JSON for transmission

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.