未定义的索引,似乎已定义

时间:2015-08-24 08:29:17

标签: php html

尝试使用W3C文件上传脚本时遇到问题。

<?php
ini_set('display_errors', '1');
include_once '../includes/conn.php';

$target_dir = "../images/pp/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if image file is a actual image or fake image
if(isset($_POST["upload"])) {
    $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
    if($check !== false) {
        $uploadOk = 1;
    } else {
        echo "File is not an image.";
        $uploadOk = 0;
    }
}
// Check if file already exists
if (file_exists($target_file)) {
    echo "Sorry, file already exists.";
    $uploadOk = 0;
}
// Check file size
if ($_FILES["fileToUpload"]["size"] > 500000) {
    echo "Sorry, your file is too large.";
    $uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
    echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
    $uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
    echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
    if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
        echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
    } else {
        echo "Sorry, there was an error uploading your file.";
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <title>EpicOwl UK | CMS</title>
    <meta charset="utf-8">
    <link rel="shortcut icon" href="../images/favicon.ico" type="image/x-icon" />
    <link rel="stylesheet" type="text/css" href="../css/main.css">
</head>
<body>
<div id="header">
    <a href="./index.php"><img id="logo" src="../images/logo.png" /></a>
</div>
<div id="content">
    <div id="spacer1"></div>
    <div id="spacer2"></div><br /><br />
    <div id="profileboarder">
        <form method="post" action="#" enctype="multipart/form-data">
        <label class="green"><strong>Upload Profile Picture:</strong></label><br />
        <input type="file" name="fileToUpload" id="fileToUpload" /><br /><br />
        <input class="login" type="submit" name="upload" value="Upload" />
        </form>
    </div>
</div>
</body>
</html>

我试过了:

$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);

已更改为

$fileToUpload = $target_dir . basename($_FILES["fileToUpload"]["name"]);

然后,我将所有$target_dir替换为$fileToUpload,但无济于事。

该脚本不起作用,但在提交之前显示所有错误消息。

2 个答案:

答案 0 :(得分:1)

把:

$target_dir = "../images/pp/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);

if(isset($_POST["upload"])) {

因为您没有上传任何文件$_FILES["fileToUpload"]尚未定义。这就是你得到错误的原因。未定义的索引意味着尚未定义数组的索引。如果您上传文件并提交表单,则会填充$_FILES

答案 1 :(得分:1)

PHP在第一次调用时读取整个脚本(如果我是正确的)。因此,首次调用脚本时,索引fileToUpload未定义。提交html表单后,索引将被填充,因此不再发现。

你可以试试这个:

    ?php
ini_set('display_errors', '1');
include_once '../includes/conn.php';

if($_SERVER["REQUEST_METHOD"] == "POST")
{
    $target_dir = "../images/pp/";


// your code here.


} else {
?>
<!DOCTYPE html>
<html lang="en">
<head>`
<!-- your HTML here -->
<?php } ?>