From surenkid, 10 Months ago, written in PHP.
This paste is a reply to PHP上传图片后端代码 from surenkid - view diff
Embed
  1. header("Access-Control-Allow-Origin: *");
  2. // 允许任意域名发起的跨域请求
  3. $baseUrl = 'http://zl.198510.xyz/t/up/';
  4.  
  5. if (isset($_FILES["file"]["type"])){
  6.         $validextensions=array ("jpeg","jpg","png","gif");
  7.         $temporary=explode(".",$_FILES["file"]["name"]);
  8.         $file_extension=end($temporary);
  9.         if ((($_FILES["file"]["type"]=="image/png") || ($_FILES["file"]["type"]=="image/jpg") || ($_FILES["file"]["type"]=="image/jpeg")) && ($_FILES["file"]["size"]<1024*1024*10)//Approx. 100kb files can be uploaded.
  10.          && in_array($file_extension,$validextensions)){
  11.              $filename = time().$_FILES["file"]["name"];
  12.                 if ($_FILES["file"]["error"]>0){
  13.             $img['code']=1;
  14.                 $img['msg']=$_FILES['file']['error'];
  15.                 }else {
  16.                         if (file_exists("upload/".$filename)){
  17.                 $img['code']=2;
  18.                 $img['msg']=$_FILES["file"]["name"].' already exists.';
  19.                         }else {
  20.                                 $sourcePath=$_FILES['file']['tmp_name'];
  21.                                 // Storing source path of the file in a variable
  22.                                 $targetPath="upload/".$filename;
  23.                                 // Target path where file is to be stored
  24.                                 move_uploaded_file($sourcePath,$targetPath);
  25.                                 // Moving Uploaded file
  26.                 $img['code']=0;
  27.                 $img['msg']='upload success!';
  28.                 $img['data']['src']=$baseUrl.$targetPath;
  29.                         }
  30.                 }
  31.         }else {
  32.             $img['code']=3;
  33.             $img['msg']='Invalid file Size or Type';
  34.         }
  35.         echo json_encode($img);
  36. }