如何修复:文件上传错误在本地主机?

时间:2018-09-14 08:41:45

标签: php mysql ajax

这是我用来将视频上传到我的网站的代码的一部分,该代码在我的服务器上运行良好,但是在localhost(xampp)中,它始终返回错误声明并且不上传视频,这是解析上传代码

<?php

$file = (isset($_FILES["image"]) ? $_FILES["image"] : 0);

if (!$file) { // if file not chosen
    die("ERROR: Please browse for a file before clicking the upload button");
}
if($file["error"]) {

echo '<script>';
  echo 'console.log('. json_encode( $file["error"] ) .')';
  echo '</script>';
    die("ERROR: File couldn't be processed");

}
        $path = $_FILES['image']['name'];
        $ext = pathinfo($path, PATHINFO_EXTENSION);
        $tmp_file = $_FILES['image']['tmp_name'];
        $fileName = $_FILES['image']['name'];
        $fileName = preg_replace("/[^a-zA-Z0-9.]/", "", $fileName);
        $thumb = explode('.', $fileName);
        $thumbname = $thumb[0];
        $thumbname = $thumbname . ".jpg";
        $file_path = "images/video/" . $fileName;
        $imagename = "category_" . time() . "." . $ext;
        move_uploaded_file($tmp_file, $file_path)
?>

这是上传视频的功能代码,它是POST请求

function uploadFile() {
  var file = _("image").files[0];
   //alert(file.name+" | "+file.size+" | "+file.type);
  var formdata = new FormData();
  formdata.append("image", file);
  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", "file_upload_parser.php"); // http://www.developphp.com/video/JavaScript/File-Upload-Progress-Bar-Meter-Tutorial-Ajax-PHP
  //use file_upload_parser.php from above url
  ajax.send(formdata);
}

这是错误消息

console.log(1)ERROR: File couldn't be processed

我的PHP版本是7.2.8

1 个答案:

答案 0 :(得分:0)

您检查了upload_max_filesize directive in php.ini吗? 。抱歉,我无法添加评论

并将其添加到检查部分之后并在失败的条件内

print_r($_FILES["image"]["error"]);

并向我们展示其结果。