阿里云 OSS 图片上传iframe 实现异步上传 相关代码:
index.php:
代码如下 | 复制代码 |
<?php /* * To change this template, choose Tools | Templates * and open the template in the editor. */ if (isset($_POST) && !empty($_POST)) { echo "<pre>"; var_dump($_POST); die; } ?> <!DOCTYPE html> |
upload.php
代码如下 | 复制代码 |
<?php $access_id = ''; $access_key = ''; $policy = '{"expiration": "2120-01-01T12:00:00.000Z","conditions":[{"bucket": "ioutsider" },["content-length-range", 0, 104857600]]}'; $policy = base64_encode($policy); $signature = base64_encode(hash_hmac('sha1', $policy, $access_key, true)); $file_name = date('Y') . '/' . date('m') . '/' . md5(microtime(true)) . '.jpg'; //print_r(get_headers('http://ioutsider.oss-cn-shenzhen.aliyuncs.com')); ?> <!DOCTYPE html> <html> <head> <title></title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <script src="http://filela.b0.upaiyun.com/js/jquery-1.9.0.min.js"></script> </head> <body> <form id="upload" action="http://ioutsider.oss-cn-shenzhen.aliyuncs.com" method="POST" enctype="multipart/form-data"> <label for="file">选择文件:</label> <input type="hidden" name="OSSAccessKeyId" id="OSSAccessKeyId" value="<?php echo $access_id; ?>" /> <input type="hidden" name="policy" id="policy" value='<?php echo $policy; ?>' /> <input type="hidden" name="Signature" id="Signature" value="<?php echo $signature; ?>" /> <!--<input type="hidden" name="key" id="key" value="upload/${filename}" />--> <input type="hidden" name="key" id="key" value="<?php echo $file_name; ?>" /> <input type="hidden" name="success_action_redirect" value="http://iphotos.me/res.php?img=<?php echo $file_name; ?>" /> <input type="file" name="file" id="file" /> <input type="submit" name="upload" value="上传" /> </form> </body> </html> |
上传成功后跳转的页面:
代码如下 | 复制代码 |
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <script src="http://filela.b0.upaiyun.com/js/jquery-1.9.0.min.js"></script> </head> <body> <?php $img = $_GET['img']; ?> <?php if ($img): ?> <img src='http://ioutsider.oss-cn-shenzhen.aliyuncs.com/<?php echo $img; ?>' width='200' height='200' /> <script> $(function(){ alert('上传成功'); window.parent.$('#res').append("<input type='hidden' value='<?php echo $img ?>' name='path'>") }) </script> <?php endif; ?> </body> </html> |
时间: 2024-09-19 09:09:31