php新浪云链接mysql与storage
1、首先要有一个新浪云服务器
2、链接数据库获取数据
mysql
CREATE TABLE Persons(FirstName varchar(255),LastName varchar(255)); insert into Persons values("张三","大张三"),("李四","小李四");
php
<?php header('Content-Type: application/json'); $output = []; //$conn =@mysqli_connect("localhost","root","","nfit2017"); $conn = mysqli_connect(SAE_MYSQL_HOST_M, SAE_MYSQL_USER, SAE_MYSQL_PASS, SAE_MYSQL_DB, SAE_MYSQL_PORT); $sql = 'SET NAMES UTF8'; mysqli_query($conn, $sql); $sql = "SELECT FirstName FROM Persons"; $result = mysqli_query($conn, $sql); //从结果集中读取一行记录 while( true ){ //从结果集中读取一行记录 $row = mysqli_fetch_assoc($result); if(! $row ){ //没有获取到更多记录行 break; } $output[] = $row; } echo json_encode($output); ?>
3、上传文件利用storage
html
<html> <head> <meta http-equiv="Content-Type" content="text/html;charset=utf-8" /> <title>上传文件</title> </head> <body> <p>所有文件</p> <form enctype="multipart/form-data" action="index.php" method="post" class="upload"> <input name='myfile' type='file' class="dropify" data-default-file="url_of_your_file"/> <input type="submit" value="上传"/> </form> <script src="js/jquery-3.2.0.min.js"></script> <script> //防止页面自动跳转的方法,注意改好class名字 $('form').submit(function (event) { event.preventDefault(); var form = $(this); if (!form.hasClass('upload')) { //普通表单 $.ajax({ type: form.attr('method'), url: form.attr('action'), data: form.serialize(), success:function(){ //成功提交 } }) }else { // mulitipart form,如文件上传类 var formData = new FormData(this); $.ajax({ type: form.attr('method'), url: form.attr('action'), data: formData, mimeType: "multipart/form-data", contentType: false, cache: false, processData: false, success:function(){ //成功提交 } }) } }); </script> </body> </html>
php
<?php $s2 = new SaeStorage(); $name =$_FILES['myfile']['name']; $s2->upload('test',$name,$_FILES['myfile']['tmp_name']);//把用户传到SAE的文件转存到名为test的storage ,$_FILES["file"]["tmp_name"] - 存储在服务器的文件的临时副本的名称 // echo $s2->getUrl("test",$name);//输出文件在storage的访问路径 // echo '<br/>'; // echo $s2->errmsg(); //输出storage的返回信息 ?>