This question already has answers here:
What does enctype='multipart/form-data' mean?
(9个答案)
四年前关闭。
我试图创建一个脚本来上传一个.mp3文件,并将文件名插入数据库。每当我试图提交表单时,我都会收到错误The FILES global variable was not set,这是我指定的错误,告诉我$_FILES未设置。
if(empty($_POST['postTitle'])){
  $error[] = "Please enter a title.";
}
if(empty($_POST['image'])) {
  $error[] = "Please select a file.";
}
if(empty($_POST['postAudio'])) {
  $error[] = "Please enter some text";
}

$allowedExts = array("jpg", "jpeg", "gif", "png", "mp3", "mp4", "wma"); // Universal between the image and audio

if(isset($_FILES['image'])){
  $imageName = $_FILES['image']['name'];
  $extension = substr($imageName, strrpos($imageName, '.') + 1);

  if(in_array($extension, $allowedExts)) {
  if($_FILES['image']['error'] > 0) {
    echo "Return Code: " . $_FILES['image']['error'] . "<br />";
  } else {
    echo "Upload: " . $_FILES['image']['name'] . "<br />";
    echo "Type: " . $_FILES['image']['type'] . "<br />";
    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
    echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";

    if (file_exists("upload/" . $_FILES["file"]["name"])) {
      echo $_FILES["file"]["name"] . " already exists. ";
    } else {
      move_uploaded_file($_FILES["file"]["tmp_name"],"upload/" . $_FILES["file"]["name"]);
      echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
     }
   }
  }
else
                {
                $error[] = "There was an error trying to get information about one of your uploads.";
                }
            } else {
                $error[] = "The FILES global variable was not set.";
            }

            if(!isset($error)){
                try {

                    $query = "INSERT INTO blog_podcasts (postTitle,postImage,postAudio,postDate) VALUES (:postTitle, :postImage, :postAudio, :postDate)";
                    $query_params = array(
                        ':postTitle' => $_POST['postTitle'],
                        ':postImage' => $imageName,
                        ':postAudio' => $_POST['postAudio'],
                        ':postDate' => date('Y-m-d H:i:s')
                        );
                    $stmt = $db->prepare($query);
                    $result = $stmt->execute($query_params);
                } catch(PDOException $e) {
                    echo $e->getMessage();
                }
                    header('Location: index.php?action=added');
                    exit;
            }
        }
        ?>
        <?php
        //check for any errors
            if(isset($error)){
                foreach($error as $error){
                    echo '<p class="error">'.$error.'</p>';
                }
            }
        ?>
        <form action='' method='post'>

            <p><label>Title</label><br />
            <input type='text' name='postTitle' value='<?php if(isset($error)){ echo $_POST['postTitle'];}?>'></p>

            <p><label for="file"><span>Filename: </span></label>
            <input type="file" name="image" id="file" />

            <p><label>Audio:<br />
            <input type="text" name="postAudio" /></p>



            <p><input type='submit' name='submit' value='Submit'></p>
        </form>

最佳答案

在表单标记中使用enctype="multipart/form-data"属性。
this article might be helpfull

08-28 06:37