注意:未定义索引:文件上传视频

我正在尝试将视频上传到我的上传文件夹。我从这里的另一个问题得到了代码并且工作正常。但是我不断收到此通知错误,但我不知道如何解决。我一整天都在努力。我试图检查它是否是isset(),但仍然没有用。有人能帮助我吗 ?


<?php


$allowedExts = array("jpg", "jpeg", "gif", "png", "mp3", "mp4", "wma");

$_FILES = $_FILES['file'];

$extension = pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION);

$unique = date('Y-m-d_H-i-s');


if ((null !==($_FILES["file"]["type"] == "video/mp4")

    || (null !==($_FILES["file"]["type"] == "audio/mp3"))

    || ($_FILES["file"]["type"] == "audio/wma")

    || ($_FILES["file"]["type"] == "image/pjpeg")

    || ($_FILES["file"]["type"] == "image/gif")

    || ($_FILES["file"]["type"] == "image/jpeg"))


&& ($_FILES["file"]["size"] < 2000000)

&& in_array($extension, $allowedExts)) {


    if ($_FILES["file"]["error"] > 0) {


        echo "Return Code: " . $_FILES["file"]["error"] . "<br />";


    } else {


    echo 'File uploaded successfully';


    if (file_exists("upload/" . $_FILES["file"]["name"])) {


        echo $_FILES["file"]["name"] . " already exists. ";


    } else {


        $datetime = date('Y-m-d_H-i-s');


        move_uploaded_file($_FILES["file"]["tmp_name"],

        "uploads/" . $_FILES["file"]["name"] . $datetime . md5($_FILES["file"]["name"]));


    }

    }


} else {


    echo "Invalid file";

}

?>


<form action="profile.php" id="videoupload" method="post" enctype="multipart/form-data">


<label for="file"><span>Filename:</span></label>

<input type="file" name="file" id="file" /> 

<br />

<input type="submit" name="submit" value="Submit" />


</form>


偶然的你
浏览 152回答 2
2回答

牛魔王的故事

您正在尝试在上传文件之前处理文件上传。您必须先检查表格是否已发布。<?phpif (isset($_FILES['file'])) {&nbsp; &nbsp; $allowedExts = array("jpg", "jpeg", "gif", "png", "mp3", "mp4", "wma");&nbsp; &nbsp; // $_FILES = $_FILES['file'];&nbsp; // <-- remove this line&nbsp; &nbsp; $extension = pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION);&nbsp; &nbsp; $unique = date('Y-m-d_H-i-s');&nbsp; &nbsp; if ((null !==($_FILES["file"]["type"] == "video/mp4")&nbsp; &nbsp; &nbsp; &nbsp; || (null !==($_FILES["file"]["type"] == "audio/mp3"))&nbsp; &nbsp; &nbsp; &nbsp; || ($_FILES["file"]["type"] == "audio/wma")&nbsp; &nbsp; &nbsp; &nbsp; || ($_FILES["file"]["type"] == "image/pjpeg")&nbsp; &nbsp; &nbsp; &nbsp; || ($_FILES["file"]["type"] == "image/gif")&nbsp; &nbsp; &nbsp; &nbsp; || ($_FILES["file"]["type"] == "image/jpeg"))&nbsp; &nbsp; && ($_FILES["file"]["size"] < 2000000)&nbsp; &nbsp; && in_array($extension, $allowedExts)) {&nbsp; &nbsp; &nbsp; &nbsp; if ($_FILES["file"]["error"] > 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo "Return Code: " . $_FILES["file"]["error"] . "<br />";&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo 'File uploaded successfully';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (file_exists("upload/" . $_FILES["file"]["name"])) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo $_FILES["file"]["name"] . " already exists. ";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $datetime = date('Y-m-d_H-i-s');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; move_uploaded_file($_FILES["file"]["tmp_name"],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "uploads/" . $_FILES["file"]["name"] . $datetime . md5($_FILES["file"]["name"]));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; echo "Invalid file";&nbsp; &nbsp; }}?><form action="profile.php" id="videoupload" method="post" enctype="multipart/form-data"><label for="file"><span>Filename:</span></label><input type="file" name="file" id="file" />&nbsp;<br /><input type="submit" name="submit" value="Submit" /></form>

翻过高山走不出你

$_FILES['file']意味着你有输入类型=文件名为“文件”。如果您没有名为“文件”的输入,则会收到未定义的索引通知。如果你有那个,$_FILES&nbsp;=&nbsp;$_FILES['file'];这会给其他代码带来错误。因为它试图覆盖 $_FILES。
打开App,查看更多内容
随时随地看视频慕课网APP