Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 158 lines (130 sloc) 6.093 kB
84c398c @VasekPurchart PlUpload 1.5.4
VasekPurchart authored
1 <?php
2 /*
3 In order to upload files to S3 using Flash runtime, one should start by placing crossdomain.xml into the bucket.
4 crossdomain.xml can be as simple as this:
5
6 <?xml version="1.0"?>
7 <!DOCTYPE cross-domain-policy SYSTEM "http://www.macromedia.com/xml/dtds/cross-domain-policy.dtd">
8 <cross-domain-policy>
9 <allow-access-from domain="*" secure="false" />
10 </cross-domain-policy>
11
12 In our tests SilverLight didn't require anything special and worked with this configuration just fine. It may fail back
13 to the same crossdomain.xml as last resort.
14
15 !!!Important!!! Plupload UI Widget here, is used only for demo purposes and is not required for uploading to S3.
16 */
17
18 // important variables that will be used throughout this example
19 $bucket = 'BUCKET';
20
21 // these can be found on your Account page, under Security Credentials > Access Keys
22 $accessKeyId = 'ACCESS_KEY_ID';
23 $secret = 'SECRET_ACCESS_KEY';
24
25
26 // hash_hmac — Generate a keyed hash value using the HMAC method
27 // (PHP 5 >= 5.1.2, PECL hash >= 1.1)
28 if (!function_exists('hash_hmac')) :
29 // based on: http://www.php.net/manual/en/function.sha1.php#39492
30 function hash_hmac($algo, $data, $key, $raw_output = false)
31 {
32 $blocksize = 64;
33 if (strlen($key) > $blocksize)
34 $key = pack('H*', $algo($key));
35
36 $key = str_pad($key, $blocksize, chr(0x00));
37 $ipad = str_repeat(chr(0x36), $blocksize);
38 $opad = str_repeat(chr(0x5c), $blocksize);
39 $hmac = pack('H*', $algo(($key^$opad) . pack('H*', $algo(($key^$ipad) . $data))));
40
41 return $raw_output ? $hmac : bin2hex($hmac);
42 }
43 endif;
44
45 // prepare policy
46 $policy = base64_encode(json_encode(array(
47 // ISO 8601 - date('c'); generates uncompatible date, so better do it manually
48 'expiration' => date('Y-m-d\TH:i:s.000\Z', strtotime('+1 day')),
49 'conditions' => array(
50 array('bucket' => $bucket),
51 array('acl' => 'public-read'),
52 array('starts-with', '$key', ''),
53 // for demo purposes we are accepting only images
54 array('starts-with', '$Content-Type', 'image/'),
55 // "Some versions of the Adobe Flash Player do not properly handle HTTP responses that have an empty body.
56 // To configure POST to return a response that does not have an empty body, set success_action_status to 201.
57 // When set, Amazon S3 returns an XML document with a 201 status code."
58 // http://docs.amazonwebservices.com/AmazonS3/latest/dev/HTTPPOSTFlash.html
59 array('success_action_status' => '201'),
60 // Plupload internally adds name field, so we need to mention it here
61 array('starts-with', '$name', ''),
62 // One more field to take into account: Filename - gets silently sent by FileReference.upload() in Flash
63 // http://docs.amazonwebservices.com/AmazonS3/latest/dev/HTTPPOSTFlash.html
64 array('starts-with', '$Filename', ''),
65 )
66 )));
67
68 // sign policy
69 $signature = base64_encode(hash_hmac('sha1', $policy, $secret, true));
70
71 ?>
72 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
73 <html xmlns="http://www.w3.org/1999/xhtml" dir="ltr">
74 <head>
75 <meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
76 <title>Plupload to Amazon S3 Example</title>
77
78 <style type="text/css">
79 body {
80 font-family:Verdana, Geneva, sans-serif;
81 font-size:13px;
82 color:#333;
83 background:url(../bg.jpg);
84 }
85 </style>
86
87 <link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.9/themes/base/jquery-ui.css" type="text/css" />
88 <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" type="text/javascript"></script>
89 <script src=" https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.9/jquery-ui.min.js"></script>
90
91 <!-- Load plupload and all it's runtimes and finally the UI widget -->
92 <link rel="stylesheet" href="../../js/jquery.ui.plupload/css/jquery.ui.plupload.css" type="text/css" />
93
94 <script type="text/javascript" src="../../js/plupload.js"></script>
95 <script type="text/javascript" src="../../js/plupload.gears.js"></script>
96 <script type="text/javascript" src="../../js/plupload.silverlight.js"></script>
97 <script type="text/javascript" src="../../js/plupload.flash.js"></script>
98 <script type="text/javascript" src="../../js/plupload.browserplus.js"></script>
99 <script type="text/javascript" src="../../js/plupload.html4.js"></script>
100 <script type="text/javascript" src="../../js/plupload.html5.js"></script>
101 <script type="text/javascript" src="../../js/jquery.ui.plupload/jquery.ui.plupload.js"></script>
102 <!--<script type="text/javascript" src="http://getfirebug.com/releases/lite/1.2/firebug-lite-compressed.js"></script>-->
103
104 </head>
105 <body>
106
107 <h1>Plupload to Amazon S3 Example</h1>
108
109 <div id="uploader">
110 <p>You browser doesn't have Flash, Silverlight, Gears, BrowserPlus or HTML5 support.</p>
111 </div>
112
113 <script type="text/javascript">
114 // Convert divs to queue widgets when the DOM is ready
115 $(function() {
116 $("#uploader").plupload({
117 runtimes : 'flash,silverlight',
118 url : 'http://<?php echo $bucket; ?>.s3.amazonaws.com/',
119 max_file_size : '10mb',
120
121 multipart: true,
122 multipart_params: {
123 'key': '${filename}', // use filename as a key
124 'Filename': '${filename}', // adding this to keep consistency across the runtimes
125 'acl': 'public-read',
126 'Content-Type': 'image/jpeg',
127 'success_action_status': '201',
128 'AWSAccessKeyId' : '<?php echo $accessKeyId; ?>',
129 'policy': '<?php echo $policy; ?>',
130 'signature': '<?php echo $signature; ?>'
131 },
132
133 // !!!Important!!!
134 // this is not recommended with S3, since it will force Flash runtime into the mode, with no progress indication
135 //resize : {width : 800, height : 600, quality : 60}, // Resize images on clientside, if possible
136
137 // optional, but better be specified directly
138 file_data_name: 'file',
139
140 // re-use widget (not related to S3, but to Plupload UI Widget)
141 multiple_queues: true,
142
143 // Specify what files to browse for
144 filters : [
145 {title : "JPEG files", extensions : "jpg"}
146 ],
147
148 // Flash settings
149 flash_swf_url : '../../js/plupload.flash.swf',
150
151 // Silverlight settings
152 silverlight_xap_url : '../../js/plupload.silverlight.xap'
153 });
154 });
155 </script>
156
157 </body>
158 </html>
Something went wrong with that request. Please try again.