Tp3 How to implement local video files uploaded to seven kn cloud

Source: Internet
Author: User

For the use of seven Qiniu, it is estimated that some new contact with the development of the technical staff is not clear, so do not say how to achieve local video upload to seven cow cloud dumps, in fact, this technology is not as difficult as imagined, the following to tell you in detail about the process:

1, first we have to look for the seven Qiniu SDK URL, open after the direct download on the line;

2, after the download is completed, we put it in the TP framework under the./thinkphp/extend/vendor/directory;

3, after the completion of the preparatory work is completed, the next is their own handwriting code:

First of all, you must first create a form in the template section:

* Video upload:

We will submit it to the Do_add in the news controller in this way:

Public Function Do_add () {

if (!empty ($_files[' video ' [' Name '])) {//first determine if the data is empty

Import (' ORG.Net.UploadFile ');

$upload = new UploadFile ();//instantiation of upload class

$upload->maxsize = 31457281;//Set attachment upload size

$upload->allowexts = Array (' mp4 ');//Set the attachment upload type if it's MP3, change mp3 to OK.

$upload->savepath = './public/uploads/video/';//Set the attachment upload directory (upload the file to the local server first)

$upload->savename = I (' uid '). ' _ '. Time ();//Set Attachments upload Directory

if (! $upload->upload ()) {//Upload error error message

$this->error ($upload->geterrormsg ());

}else{//Upload successfully uploaded file information

$info = $upload->getuploadfileinfo ();

}

Import Class Auto Loader

Require_once './thinkphp/extend/vendor/qiniusdk/autoload.php ';

The key of the seven cows

$accessKey = ' xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx ';

$secretKey = ' xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx ';

$bucket = ' haha '; This is the space you're uploading.

$savepath = "http://www.xxxxxxxxxx.com/";//This is your path to seven cows.

$auth = new Auth ($accessKey, $secretKey);

You can use the SaveAs parameter to customize the name of the transcoded file, or you can not specify that the file will be named by default and saved in the

$mediaid = Time (). Uniqid ();

$savekey = \qiniu\base64_urlsafeencode (haha: ' $mediaid. '. MP4 ');

$fops = $fops. ' | saveas/'. $savekey;

$policy = Array (

' Persistentops ' = $fops,

);

$uptoken = $auth->uploadtoken ($bucket, NULL, 3600, $policy);

Local path for uploading files

$filePath = './public/uploads/video/'. $info [0][' Savename '];

$key = $mediaid. '. MP4 ';

$UPLOADMGR = new UploadManager ();

List ($ret, $err) = $uploadMgr->putfile ($uptoken, $key, $filePath);

if ($err!== null) {

$dataname ["Info"]= "error";

} else {

$savename = $ret [' key '];

$file = $savepath. $savename;

$dataname ["Info"]= $file;

}

}else{

$dataname ["Info"]= "error";

}

Unlink ($filePath);//delete files on the local server after successful upload

$audio =d ("audio");

$WH [' Aid ']=i ("aid");

$data [' Video_path ']= $file;

$re = $audio->add ($data); Path saved to data table

if ($re) {

Success (' Add Success ', U (' news/newslist '));

}else{

Error ("Add failed");

}

echo Json_encode ($dataname); Return data structure self-encapsulation

}

After running to this place or error can not find Auth this class, groped for a while to find the namespace less, add to the top of the controller:

Use Qiniu\storage\uploadmanager;

Use Qiniu\auth;

Normal after operation.

At this point has been uploaded, upload completed to their own seven cattle to see a look on the ok! need to explain is MP3 and MP4 similar, just need to put the code in the MP4 for MP3 can, then now you can start to try it, if there are doubts, you can leave a message, we receive the first time to reply. This article by the Professional Zhengzhou app development company Brigitte Xuan Science and technology finishing publish, if need reprint please indicate source.

Tp3 How to implement local video files uploaded to seven kn cloud

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.