$_FILES undefined PHP upload form. Can't figur

2019-03-07 01:23发布

Possible Duplicate:
Getting ‘undefined index’ error while trying to use $_FILE in PHP

I have made a php upload form for images. It uses session variables to determine the upload directory. There are two options for the uploads, the slider dir or the side dir so I have an if statement that determines the directory. If i remove this from the form then the whole thing works ok however with it in, $_FILES seems to not be declared and returns as an undefined index error.

The code can be found is as follows:

upload.php

<?php
include("resize-class.php");
$allowedExt = array('jpg', 'jpeg', 'JPG', 'JPEG');
$tmps = explode(".", $_FILES['file']['name']);
$extension = end($tmps);
session_start();
if ($_POST['dir'] == 'side'){
    $dirent = $_SESSION['sideDir'];
}
else if($_POST['dir'] == 'slider'){
    $dirent = $_SESSION['sliderDIR'];
}
else{
    die();
}
echo $_POST['dir'];
print_r($_FILES);

if (($_FILES["file"]["type"] == "image/jpeg")&& ($_FILES["file"]["size"] < 4000000000)&& in_array($extension, $allowedExt)) {
    if ($_FILES["file"]["error"] > 0) {
        echo "Error: " . $_FILES["file"]["error"] . "<br />";
        echo 'here';
    } else {
        echo "Upload: " . $_FILES["file"]["name"] . "<br />";
        echo "Type: " . $_FILES["file"]["type"] . " <br />\n";
        echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br /> \n";
        echo "Stored in: " . $_FILES["file"]["tmp_name"]. "<br />\n";

    }
    if (file_exists($dirent. $_FILES["file"]["name"])) {
        echo $_FILES["file"]["name"] . "already exists";
    } else {
        $fName = $_FILES["file"]["name"];
        $tmpname = $_FILES["file"]["tmp_name"];
        move_uploaded_file($_FILES["file"]["tmp_name"], $dirent . $_FILES["file"]["name"]);
        $number = FileCounter($dirent);
        echo "Number of images in DIR: " . $number. "   <br />\n  ";
        $number +1;
        $resizeObj = new resize($dirent.$fName);
        $resizeObj -> resizeImage(250, 150, 'crop');  
        $resizeObj -> saveImage($dirent.$number.".jpg", 100); 
        unlink ($dirent.$_FILES["file"]["name"]);
    }
} else {
    echo "Error: " . $_FILES["file"]["error"] . "<br />";
}
function FileCounter($dir) {
            $counter = 0;
            $iterator = new DirectoryIterator(dirname($dir));
            foreach ($iterator as $fileinfo) {
                if ($fileinfo->isFile()) {
                    if ($fileinfo->getExtension() == "jpg") {
                        $counter++;
                        echo $counter . "\n";
                    }
                }
            }
            return $counter;
        }
?>

HTML FORM:

<form action="includes/upload.php" method="post">
  <label for="file">Filename:</label>
  <input type="file" name="file" id="file" />
  <div class="styled-select">
    <label for="dir"> Upload to:</label>
    <select size="2" name="dir" multiple="yes" id="dir">
      <option value="side" >Side Images</option>
      <option value="slider" >Slider Images</option>
    </select>
  </div>
  <br />
  <input type="submit" name="submit" value="Submit" />
</form>

I am guessing that there is either a really stupid error in my code that I am overlooking as I have been staring at it for an hour now or that there is something that I do not know about $_FILES and $_POST. (or possibly I have coded the form like a moron!).

1条回答
孤傲高冷的网名
2楼-- · 2019-03-07 02:06

Your form is missing

enctype="multipart/form-data"
查看更多
登录 后发表回答