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的返回資訊 ?>