-4

My HTML code is this:

<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>

My PHP code is this:

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

Problem is that my code never even gets to the third line. As soon as the code starts, it dies because of the first if which goes "$_FILES['file']['error'] !== UPLOAD_ERR_OK".

Why is this happening? How do I upload the video?

Any other suggestions to clean up my PHP code is much appreciated.

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
Yash Sharma
  • 31
  • 10

1 Answers1

0

Spot the differences:

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

$myFile = $_FILES["myFile"];
                   ^^^^^^
if ($_FILES['file']['error'] !== UPLOAD_ERR_OK) {
            ^^^^^^
Marc B
  • 356,200
  • 43
  • 426
  • 500