Joke Collection Website - Blessing messages - The Php form uploads the picture to the cloud storage of Qiniu, and returns the address ... for the specific process ~ it is better to have the code.
The Php form uploads the picture to the cloud storage of Qiniu, and returns the address ... for the specific process ~ it is better to have the code.
Original address:/? p= 157
There are too few online tutorials about Qi Niu Cloud Storage, except the API documents of official website. After learning the API, you can now upload and download pictures and redirect them after uploading.
First of all, the functions realized in this paper are as follows:
1. With the form upload function, users can click the Select File button, select a local file, and set the name of the uploaded picture. Click the upload button to upload and save it to the cloud storage of Qiniu.
2. When clicking Upload, the suffix name of the file will be detected, which is limited to jpg and png format storage.
3. After the upload is successful, jump to a URL set by yourself and return file information, such as file name. Instead of a json display page showing seven cows and white flowers.
All right, let's get started. First, we need to have a seven-cow cloud storage account. If we don't, we can apply ourselves.
Qiniu Cloud Storage Portal:/
I. SDK download
/qiniu/php-sdk/tags
Poke this website to download SDK, which encapsulates the method of uploading and downloading files. After the introduction, we can call directly.
There is a qiniu folder in the SDK, which is full of SDK goods. This is the most important thing. We must first put this folder and the files in it into the project folder, for example, I put it here.
As you can see, there is a folder of seven cows. Well, this is resource support. Next, we need to implement the code.
Two. Upload files.
1. First, take the key of your cloud storage, click the account settings, and see that there is an AccessKey and a SecretKey, which are reserved for use.
2. Upload voucher generation.
Here we need to import the rs.php file first and find the corresponding path ourselves. The code is as follows:
Require_once (directory name (_ _ file _ _). "/../../qiniu/RS . PHP ");
Dirname () refers to an absolute path, and sometimes there are problems with relative paths. It is suggested to add the dirname method in front to obtain the absolute path.
Require_once is an import file, which means that the file is only imported once.
Then, pass in your access key and secret key.
The code is as follows:
$accessKey? =? imn 35k C5 prx 7 ov 3 scxbykvnk 6 oix 7 zwsbrp 16 '; ? //Change to your own key.
$secretKey? =? s 29 VC 9 tlcv 23 wrh 7 qscytuzcdmerokj 1 ddssz '; //Replace with your own key Qiniu _ setkeys ($ access key, $ secret key);
Then build an upload strategy object and put your bucket? Incoming, bucket is your space name.
$ bucket = ' designpartners
$ put policy = new Qiniu _ RS _ put policy($ bucket);
Then call this method to generate upload credentials.
$ upToken = $ put policy-& gt; Token (empty);
Then write an html form.
& lt form? method="post "? Action = ""? Name? =? "Form"? enctype="multipart/form-data " >
& ltul & gt
& lt input? type="hidden "? id="token "? name="token "? value = & lt? php? Echo? $upToken? & gt& gt
& lt Li>
& lt label? For = "key"> key: & lt/label & gt;;
& lt input? name="key "? value = " " & gt
& lt/ Li>
& lt Li>
& lt label? For = "bucket"> Photo:
& lt input? name="file "? type="file "? /& gt;
& lt/ Li>
& lt Li>
& lt input? type="submit "? Value= "submit"? & gt
& lt/ Li>
& lt/ul & gt;
& lt/form & gt; Action? Just fill in? Up.qiniu.com, the form provides an input box key to enter the name of the picture you want to save. This is the name uploaded to the seven cows.
Then file selection and submit button. The running results are as follows:
You can upload photos by entering key values and selecting photos. hahaha. Is it simple?
Third, file download.
The principle is similar to the file upload function.
Import file
Require_once (directory name (_ _ file _ _). "/../../qiniu/RS . PHP "); Declare your Qiniu cloud storage domain name and two keys, as well as the file name downloaded from.
$key? =? '00000';
$domain? =? design partners . qiniudn . com ';
$accessKey? =? io imn 35 c5p 3 scxxbykvnk 6 oixb 7 zwsbrp 16 ';
$secretKey? =? s 29 VC 9 tlcvs 23 wcdmibus 4 erokj 1z '; Note: The value of 1.key is the file name without suffix.
2. The domain is barrel plus qiniudn.com. In the example, designpartners is the bucket name I used when uploading pictures.
3. You can't replace 3.accessKey and secretKey with your own key, but you can't use mine directly ... because I modified it.
qiniu _ SetKeys($access key,? $ secret key); ?
$baseUrl? =? qiniu _ RS _ MakeBaseUrl($domain,? $ key);
$getPolicy? =? New? qiniu _ RS _ get policy();
$privateUrl? =? $ get policy-& gt; MakeRequest($baseUrl,null);
Echo? $privateUrl? . ? ”\ n”; Pass in these four values to generate the same url, and directly visit the url to download the picture.
Directly when introducing pictures
& ltimg src = " & lt? php echo $ privateUrl? >/& gt;
You can import pictures. It's simple.
Four, 303 redirection
In the above method, when we successfully upload the picture, we jump to up.qiniu.com, and a white webpage and a json string will be displayed. But in the actual website development, we definitely can't let users see this kind of webpage, so we use 303 jump. The SDK also encapsulates this method for us. It's actually very simple to use. Just add two lines of code to the code for uploading files.
$putPolicy? =? New? qiniu _ RS _ put policy($ bucket);
$ put policy-& gt; ReturnUrl? =? site_url()。 "/upload/receive info ";
$ put policy-& gt; ReturnBody='{"key ":? $(key)} '; Note: 1. ReturnUrl and ReturnBody must be specified, the first letter should be capitalized, and many people start with lowercase, which will lead to unsuccessful jump.
2.ReturnUrl must be a website accessible by the public network, and it is impossible to pass the local test. For example, if you write localhost, the seven cattle server can't locate it.
3. will there be one after this ReturnUrl link? Upload_ret=XXX, you can get this upload _ ret by the get method. The content of upload_ret is json key value of base64 security encoding.
Numerical analysis: For example, the file name I uploaded is 555.
Upload/receive information? upload _ ret = eyjrzxkioiaintu 1in 0 =
The URL suffix is shown above. Copy upload_ret and decode it with base64, and the following results will appear:
{"key": "555"}
Therefore, the code of the value 555 we want to get is as follows, that is, the parsing code is as follows:
$upload_ret? =? $ _ GET[' upload _ ret '];
$json_ret? =? base64 _ decode($ upload _ ret);
$ result = JSON _ decode($ JSON _ ret);
Echo? "key" $ result-& gt; Key; Then, after you get this key value, you can choose to save it in the database or do other operations.
Verb (abbreviation for verb) verifies the file type before uploading.
We can use js to verify the file suffix,
Add to the properties of the form
onsubmit = " return is validate file(' file ');"
Add a js method.
& lt script & gt
Function? isValidateFile(obj)? {
var? Extension? =? document . form . file . value . substring(document . form . file . value . lastindexof(" . ")? +? 1);
What if? (extension? ==? "")? {
Alert ("Please select an avatar");
Return? Fake;
}
Or what? {
What if? (! (extension? ==? “jpg”? ||? Extension? ==? " png "))? {
Alert ("Please upload a file with a suffix of jpg or png!" );
Return? Fake;
}
}
Return? True;
}
& lt/script & gt; You can verify whether its type is legal.
Attachment: CI code implementation:
Get up Ken:
Function? getUptoken(){
Require_once (directory name (_ _ file _ _). "/../../qiniu/RS . PHP ");
//Remote storage space name
$bucket? =? design partners ';
$accessKey? =? io imn 35 crx 7 ov 3 scvnk 6 oixb 7 zwsbrp 16 ';
$secretKey? =? s 29 VC 9 tlcv 23 wrhtuzcdmibus 4 erokj 1z ';
qiniu _ SetKeys($access key,? $ secret key);
$putPolicy? =? New? qiniu _ RS _ put policy($ bucket);
Echo? site _ URL();
$ put policy-& gt; ReturnUrl? =? site_url()。 "/upload/receive info ";
$ put policy-& gt; ReturnBody='{"key ":? $(key)} ';
$upToken? =? $ put policy-& gt; Token (empty);
Return? $ upToken
} file upload:
Public? Function? uploadPic(){
$upToken? =? $ this-& gt; getUptoken();
$data['upToken']? =? $ upToken
$ this-& gt; Load->; view('upload ',$ data);
}303 Redirection resolution:
Public? Function? Receive information () {
$upload_ret? =? $ _ GET[' upload _ ret '];
$json_ret? =? base64 _ decode($ upload _ ret);
$ result = JSON _ decode($ JSON _ ret);
Echo? "key" $ result-& gt; Key; ?
} file download:
Public? Function? downloadPic(){
Require_once (directory name (_ _ file _ _). "/../../qiniu/RS . PHP ");
$key? =? '00000';
$domain? =? design partners . qiniudn . com ';
$accessKey? =? io imn 35 KC 57 ov 3 scxbykvnk 6 oixb 7 zwsbrp 16 ';
$secretKey? =? s 29 VC 9 tlcvsh 7 qscytuzcdmibus 4 erokj 1z ';
qiniu _ SetKeys($access key,? $ secret key); ?
$baseUrl? =? qiniu _ RS _ MakeBaseUrl($domain,? $ key);
$getPolicy? =? New? qiniu _ RS _ get policy();
$privateUrl? =? $ get policy-& gt; MakeRequest($baseUrl,null);
Echo? “= = = = = >; ? getPolicy? Result:? \ n ";
Echo? $privateUrl? . ? ”\ n”;
} Form:
& lt script & gt
Function? isValidateFile(obj)? {
var? Extension? =? document . form . file . value . substring(document . form . file . value . lastindexof(" . ")? +? 1);
What if? (extension? ==? "")? {
Alert ("Please select an avatar");
Return? Fake;
}
Or what? {
What if? (! (extension? ==? “jpg”? ||? Extension? ==? " png "))? {
Alert ("Please upload a file with a suffix of jpg or png!" );
Return? Fake;
}
}
Return? True;
}
& lt/script & gt;
& lt form? method="post "? Action = ""? Name? =? "Form"? enctype="multipart/form-data "? onsubmit="return? is validate file(' file ');" & gt
& ltul & gt
& lt input? type="hidden "? id="token "? name="token "? value = & lt? php? Echo? $upToken? & gt& gt
& lt Li>
& lt label? For = "key"> key: & lt/label & gt;;
& lt input? name="key "? value = " " & gt
& lt/ Li>
& lt Li>
& lt label? For = "bucket"> Photo:
& lt input? name="file "? type="file "? /& gt;
& lt/ Li>
& lt Li>
& lt input? type="submit "? Value= "submit"? & gt
& lt/ Li>
& lt/ul & gt;
& lt/form & gt;
- Related articles
- Headline front-end interview with both sides-hang up
- Mobile e-Netcom network, can I apply for the uplink speed-up package?
- Brief introduction of dragon boat festival greetings
- What preparations do you need to make before getting engaged? What process?
- Happy Teachers' Day to university teachers.
- Should I confess? . .
- Central SMS report
- Greetings on New Year's Eve morning
- Why didn't the girl reply to the text message?
- Suddenly I received a short message from China Life Association informing me that I have become a member. How to cancel it?