我目前使用REST有一个php api,它有以下方法:
private function createVideoCast(){
if($this->get_request_method() != "POST"){
$error = array('status' => "Failed", "msg" => "Invalid Request Method");
$this->response($this->json($error), 406);
}
else{
$vid = (int)$this->getLastVideoCastId() + 1;
$date = $this->_request['date'];
$video = $this->_files['video'];
if(!empty($video['name']) && !empty($date)){
$vorder = (int)$this->getLastVideoCastVorder($date) + 1;
$vidExts = array("video/mp4");
if(!in_array($video['type'], $vidExts)){
$error = array('status' => "Failed", "msg" => "Video Must Be Of Type MP4");
$this->response($this->json($error), 400);
}
if($video['size'] > 1073741824){
$error = array('status' => "Failed", "msg" => "Video Maximum Size Is 1GB");
$this->response($this->json($error), 400);
}
$desired_base_dir = "videocasts/".$date;
if(is_dir($desired_base_dir) == false){
mkdir($desired_base_dir);
$video_name = $vorder.".mp4";
if(is_dir($desired_base_dir."/".$video_name) == false){
move_uploaded_file($video['tmp_name'], $desired_base_dir."/".$video_name);
}
else{
unlink($desired_base_dir."/".$video_name);
move_uploaded_file($video['tmp_name'], $desired_base_dir."/".$video_name);
}
$video_path = $desired_base_dir."/".$video_name;
$sql = "INSERT QUERY TO MY DATABASE";
if(mysql_query($sql, $this->db)){
$success = array('status' => "Success", "msg" => "Video Successfully Uploaded.");
$this->response($this->json($success), 200);
}
else{
$error = array('status' => "Failed", "msg" => mysql_error());
$this->response($this->json($error), 400);
}
}
}
else{
$error = array('status' => "Failed", "msg" => "Date & Video Required");
$this->response($this->json($error), 400);
}
}
}
我有一个通过ajax提交的html表单,如下所示:
<script type="text/javascript">
function _(el){
return document.getElementById(el);
}
function createVideoCast(){
var date = _("date").value;
var video = _("video").files[0];
var formdata = new FormData();
formdata.append("date", date);
formdata.append("video", video);
var ajax = new XMLHttpRequest();
ajax.upload.addEventListener("progress", progressHandler, false);
ajax.addEventListener("load", completeHandler, false);
ajax.addEventListener("error", errorHandler, false);
ajax.addEventListener("abort", abortHandler, false);
ajax.open("POST", "../newsApi.php?rquest=createVideoCast");
ajax.send(formdata);
}
function progressHandler(event){
var percent = (event.loaded / event.total) * 100;
_("progressBar").value = Math.round(percent);
_("upload-status").innerHTML = Math.round(percent)+"%";
}
function completeHandler(event){
console.log(event);
var message = JSON.parse(event.target.responseText);
_("message").innerHTML = message.msg;
_("progressBar").value = 0;
if(message.msg == "VideoCast Successfully Created."){
_("vc-form").reset();
}
}
function errorHandler(event){
console.log(event);
_("message").innerHTML = "Upload Failed";
}
function abortHandler(event){
_("message").innerHTML = "Upload Aborted";
}
</script>
适用于大约250MB以下的视频,但一旦视频超过该尺寸,我就会收到以下错误:
&#34;&lt; - SHTML Wrapper - 500服务器错误 - &gt;↵[处理此指令时出错]↵&#34;
我的php.ini文件设置设置为适合1GB文件。我希望有人可以提前为此提供帮助。