【问题标题】:My PHP is not uploading videos [closed]我的 PHP 没有上传视频 [关闭]
【发布时间】:2016-10-29 15:44:23
【问题描述】:

我的 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 {
}
}

问题是我的代码甚至没有到达第三行。代码一开始,它就会死掉,因为第一个 if 是 "$_FILES['file']['error'] !== UPLOAD_ERR_OK"。

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

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

【问题讨论】:

  • 你能缩进你的代码吗?它使调试更容易。
  • 看看这个&lt;input type="file" name="uploadvideo"...$_FILES["myFile"]$_FILES['file']。我在这里根本看不到比赛。我称之为未定义的索引等。
  • 那么错误码是什么?
  • 没什么。它只是说“上传失败,错误代码”

标签: php html error-handling


【解决方案1】:

找出差异:

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

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

【讨论】:

  • 这应该是评论吗? :) 关闭为拼写错误?
  • @PraveenKumar 哦,你的意思是this?...哈哈
  • 非常感谢。我试过这个。但仍然会抛出一个它拒绝显示给我的错误。
  • 你上传的是什么类型的视频?如果您要上传 mp4/mkv/avi/etc,getimagesize() 完全没用......它只处理 gif/jpg/png/etc.. 标准 IMAGE 类型,而不是视频。
  • 不,我明白了。它正在工作。谢谢。
猜你喜欢
  • 2017-01-08
  • 2016-05-17
  • 2018-09-06
  • 1970-01-01
  • 1970-01-01
  • 2015-03-15
  • 1970-01-01
  • 2012-08-04
  • 1970-01-01
相关资源
最近更新 更多