Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/268.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何为php文件上传创建RESTAPI_Php_Rest - Fatal编程技术网

如何为php文件上传创建RESTAPI

如何为php文件上传创建RESTAPI,php,rest,Php,Rest,我正在尝试用PHP构建RESTAPI。我的API需要能够上传文件并通过$\u POST方法获取用户信息。任何机构都可以帮助我创建RESTAPI,还可以帮助我找到创建API所需的组件 <?php header("content-type:application/json"); $userid=$_POST['user_id']; $email=$_POST['email']; $fname=$_POST['firstname']; $lname=$_POST['lastname'

我正在尝试用PHP构建RESTAPI。我的API需要能够上传文件并通过$\u POST方法获取用户信息。任何机构都可以帮助我创建RESTAPI,还可以帮助我找到创建API所需的组件

<?php

 header("content-type:application/json");
 $userid=$_POST['user_id'];
 $email=$_POST['email'];
 $fname=$_POST['firstname'];
 $lname=$_POST['lastname'];

 // include db connect class
    require_once __DIR__ . '/db_connect.php';

    // connecting to db
    $db = new DB_CONNECT();
 //$result=mysql_query("select * form user");
 $sql="UPDATE user SET email='$email',fname='$fname',lname='$lname' WHERE userid='$userid'";
 $result = mysql_query($sql);
 if ($result) {
        // successfully inserted into database
        $response["code"] = 1;
        $response["message"] = "successfully updated";

        // echoing JSON response
        echo json_encode($response);
    } else {
        // failed to insert row
        $response["code"] = 2;
        $response["message"] = "Oops! failed to insert data";

        // echoing JSON response
        echo json_encode($response);
    }

 //file uploading 
 if (empty($_FILES) || $_FILES['file']['error']) {
  //die('{"OK": 0, "info": "Failed to move uploaded file."}');
  $response["code"] = 2;
  $response["message"] = "Oops! An File uploading error occurred.";
  echo json_encode($response);
}

$chunk = isset($_REQUEST["chunk"]) ? intval($_REQUEST["chunk"]) : 0;
$chunks = isset($_REQUEST["chunks"]) ? intval($_REQUEST["chunks"]) : 0;

$fileName = isset($_REQUEST["name"]) ? $_REQUEST["name"] : $_FILES["file"]["name"];
$filePath = "uploads/$fileName";


// Open temp file
$out = @fopen("{$filePath}.part", $chunk == 0 ? "wb" : "ab");

if ($out) {
  // Read binary input stream and append it to temp file
  $in = @fopen($_FILES['file']['tmp_name'], "rb");

  if ($in) {
    while ($buff = fread($in, 4096))
          fwrite($out, $buff);
       //print($out);
     // echo sizeof($out);
  } else
  //  die('{"OK": 0, "info": "Failed to open input stream."}');
 $response["code"] = 2;
 $response["message"] = "Oops!  Failed to open input Stream error occurred.";
 echo json_encode($response);
  @fclose($in);

  @fclose($out);

  @unlink($_FILES['file']['tmp_name']);
} else{
//  die('{"OK": 0, "info": "Failed to open output stream."}');
 $response["code"] = 2;
        $response["message"] = "Oops! Failed to open output error occurred.";
        echo json_encode($response);
 }

// Check if file has been uploaded
if (!$chunks || $chunk == $chunks - 1) {
  // Strip the temp .part suffix off
  rename("{$filePath}.part", $filePath);
}


//die('{"OK": 1, "info": "Upload successful."}');
 $response["code"] = 0;
    $response["userid"]=$_POST['user_id'];
    $response["email"]=$_POST['email'];
    $response["firstname"]=$_POST['firstname'];
    $response["lastname"]=$_POST['lastname'];
    //$resopnse["file"]=$_POST['file'];
    $response["message"] = "Required field(s) is missing";

    // echoing JSON response
    echo json_encode($response);

?>

对于使用REST api上载图像,最可取的方法是在post请求中传递base64编码的图像数据,然后使用

文件内容()

作用

请参考示例代码片段

例如


对于使用REST api上载图像,最可取的方法是在post请求中传递base64编码的图像数据,然后使用

文件内容()

作用

请参考示例代码片段

例如


对于使用REST api上载图像,最可取的方法是在post请求中传递base64编码的图像数据,然后使用

文件内容()

作用

请参考示例代码片段

例如


对于使用REST api上载图像,最可取的方法是在post请求中传递base64编码的图像数据,然后使用

文件内容()

作用

请参考示例代码片段

例如


可能重复的可能重复的可能重复的可能重复的
base64
的可能重复不是一个好的选择,特别是当API将在高流量下处理大量文件时。。将文件上载到plase并链接到应用程序会更好@rubin porwel
base64
不是一个好选择,特别是如果API将在高流量下处理大量文件。。将文件上载到plase并链接到应用程序会更好@rubin porwel
base64
不是一个好选择,特别是如果API将在高流量下处理大量文件。。将文件上载到plase并链接到应用程序会更好@rubin porwel
base64
不是一个好选择,特别是如果API将在高流量下处理大量文件。。将文件上传到plase并链接到Destin会更好@rubin porwel
RewriteEngine On 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteRule ^(.*)$ %{ENV:BASE} index.php [QSA,L]
$img_data=$_POST['image'];

$img_info=explode(',',$img_data);

$image_content=base64_decode($img_info[1]);

$img_extension=substr($img_info[0],11,3);

$img_filename=$_SERVER['DOCUMENT_ROOT'].'/images/img_'.time().'.'.$img_extension;

file_put_contents($img_filename,$image_content);