大家好,我一直在尝试在我的CRUD rest api中上传2个文件。但是,我无法解决问题,我尝试了书中的所有内容,从更改标头类型到尝试通过邮递员上传。什么都没有。 这是我的index.php文件,用于将请求发送到端点。
function remotePost($data, $url){
$data_string = json_encode($data);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, 1);
$result = curl_exec($ch);
return $result;
}
if(isset($_POST['submitdemo'])){
$data = array(
"demo_name" => $_POST['demo_name'],
"demo_url" => $_POST['demo_url'],
"demo_content_url" => $_FILES['demo_content_url'],
"demo_preview_thumb" => $_FILES['demo_preview_thumb'],
"prod_id" => 123
);
var_dump(remotePost($data, "http://localhost/api/v1/demo-data/insert-demo"));
}
这是insert.php端点
header("Access-Control-Allow-Origin: *");
header("Content-Type: multipart/form-data; charset=UTF-8");
header("Access-Control-Allow-Methods: POST");
header("Access-Control-Max-Age: 3600");
header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");
// get database connection
include_once '../config/database.php';
// instantiate demo_data object
include_once '../objects/demo-data.php';
$jsonObject = json_decode(file_get_contents('php://input'));
$database = new Database();
$db = $database->getConnection();
$demo_data = new DemoData($db);
$uploadOk = 1;
$demo_content_dir = dirname(__DIR__) . "/uploads/content/";
$content_file = $demo_content_dir . basename($jsonObject->demo_content_url->name);
$demoContentFileType = strtolower(pathinfo($content_file, PATHINFO_EXTENSION));
$demo_thumbnail_dir = dirname(__DIR__) . "/uploads/thumbnails/";
$thumbnail_file = $demo_thumbnail_dir . basename($jsonObject->demo_preview_thumb->name);
$demoThumbnailFileType = strtolower(pathinfo($thumbnail_file, PATHINFO_EXTENSION));
// set user property values
$demo_data->demo_name = $jsonObject->demo_name;
$demo_data->demo_url = $jsonObject->demo_url;
$demo_data->demo_content_url = $jsonObject->demo_content_url->name;
$demo_data->demo_preview_thumb = $jsonObject->demo_preview_thumb->name;
$demo_data->prod_id = $jsonObject->prod_id;
if($demoContentFileType != "zip" && ($demoThumbnailFileType != "png" || $demoThumbnailFileType != "jpg" || $demoThumbnailFileType != "jpeg")) {
// set response code - 503 service unavailable
http_response_code(503);
// tell the user
echo json_encode(array("message" => "Thumbnail only supports .jpg, .jpeg, .png and .gif where as content only supports .zip"));
$uploadOk = 0;
}elseif($uploadOk) {
move_uploaded_file($jsonObject->demo_content_url->tmp_name, $content_file);
move_uploaded_file($jsonObject->demo_preview_thumb->tmp_name, $thumbnail_file);
if($demo_data->insert_demo()){
// set response code - 201 created
http_response_code(201);
// tell the user
echo json_encode(array("message" => "Demo Inserted."));
}else {
// set response code - 503 service unavailable
http_response_code(503);
// tell the user
echo json_encode(array("message" => "Unable to insert Demo."));
}
}
所有内容均按其应有的方式插入数据库,但文件未移动到各自的目录中。
move_uploaded_file($jsonObject->demo_content_url->tmp_name, $content_file);
总是返回假