我的PHP没有上传视频

时间:2016-06-27 14:54:22

标签: php html error-handling

我的HTML代码是:

<div id="videoinput" class="inputbox">
<form autocomplete="off" action="../upload/" name="textinput" method="POST" enctype="multipart/form-data">
    <input type="text" id="videotitlebox" name="texttitle" required class="inputtext" placeholder="Title of video" />
    <textarea type="text" id="textdescbox" name="textdesc" required class="inputtext" placeholder="Description"></textarea>
    <?php
    require("../common/category.php");
    ?>
    <input type="file" name="uploadvideo" value="Upload video" id="videoupload" required />
    <input type="submit" name="uploadvideo" value="Submit" class="submitbut" />
</form>

我的PHP代码是这样的:

if (isset($_POST['uploadvideo'])) {
if($_POST['category']!="" && $_POST['category']!="selectcategory") {
define("UPLOAD_DIR1", "../uploadedfiles/");
$myFile = $_FILES["myFile"];
if ($_FILES['file']['error'] !== UPLOAD_ERR_OK) {
    die("Upload failed with error code " . $_FILES['file']['error']);
}
$info = getimagesize($_FILES['file']['tmp_name']);
if ($info === FALSE) {
 die("File is not of an acceptable type. Only .GIF, .JPEG,.JPG and .PNG files are acceptable.");
}

if (($info[2] !== IMAGETYPE_GIF) && ($info[2] !== IMAGETYPE_JPEG) && ($info[2] !== IMAGETYPE_PNG)) {
 die("File is not an image file.");
}
// ensure a safe filename
$name = preg_replace("/[^A-Z0-9._-]/i", "_", $myFile["name"]);
$temp = explode(".", $name);
$name = $_SESSION['user_id'] . '.' . end($temp);
// don't overwrite an existing file
$parts = pathinfo($name);
$id = $_SESSION['user_id'] . Date("__h.m.s__d.m.y__") . "image";
$id = md5($id);
$name = md5($id) . "." . $parts["extension"];
// preserve file from temporary directory
$success = move_uploaded_file($myFile["tmp_name"],
  UPLOAD_DIR1 . $name);
chmod(UPLOAD_DIR1 . $name, 0644);
$time = date("h:i:s");
$mydate=getdate(date("U"));
$date = "$mydate[year].$mydate[mon].$mydate[mday]";

$title = $_POST['texttitle'];
$category = $_POST['category'];
$description = $_POST['textdesc'];

$sql = "INSERT INTO uploads(title, category, description, path, id, time, date, username, datetime, type)
                 VALUES (:title, :category, :description, :path, :id, :time, :date, :user, :datetime, :type)";
$query = $conn->prepare($sql);
$query->execute(array(':title'=>$title, ':category'=>$category, ':description'=>$description, ':path'=>$name, ':id'=>$id, ':time'=>$time, ':date'=>$date, ':user'=>$_SESSION['user_id'], ':datetime'=>$date . " " .     $time, ':type'=>"image") );
echo "Success.";
header('Location: ../home/');
}
else {
}
}

问题是我的代码甚至没有到达第三行。一旦代码开始,它就会因为第一个而去世,并且#34; $ _ FILES [&#39;文件&#39;] [&#39;错误&#39;]!== UPLOAD_ERR_OK&#34;。

为什么会这样?如何上传视频?

非常感谢任何其他清理我的PHP代码的建议。

1 个答案:

答案 0 :(得分:0)

发现差异:

<input type="file" name="uploadvideo" value="Upload video" id="videoupload" required />
                         ^^^^^^^^^^^

$myFile = $_FILES["myFile"];
                   ^^^^^^
if ($_FILES['file']['error'] !== UPLOAD_ERR_OK) {
            ^^^^^^