我目前有一个使用 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 以下的视频,但是一旦视频超过该大小,我就会收到以下错误:
"<-- SHTML Wrapper - 500 Server Error -->↵[处理此指令时发生错误]↵"
我的 php.ini 文件设置设置为适合 1GB 文件。我希望有人可以为此提供一些帮助,在此先感谢。