尝试将文件上传到服务器时,php undefined index错误

时间:2014-06-24 06:52:25

标签: php

我试图将数据插入到数据库表中,哪个商店引用我的图库图像,但是当用户无法插入数据时,我想返回自定义错误消息,但它会继续显示此错误(或通知)。

   Undefined index: gallery in C:\xampp\htdocs\unik\admin\add_gallery.php

以下是使用

的代码示例
  if (isset($_POST['submit'])) {

    $input['caption']   =   escape($_POST['caption']);

    if ($input['caption'] == '' || $_FILES['gallery']['error']) {

        if ($input['caption'] == '')     { $error['caption']    =   "Image Caption required!"; }
        if ($_FILES['gallery']['error']) { $error['gallery']    =   $upload_errors[$_FILES['gallery']['error']]; }

        $error['alert'] =   "Please fill in all the required fields!";

        include 'gallery_form.php';

    } else {
        $tmp_file       =   $_FILES['gallery']['tmp_name'];
        $target_file    =   basename($_FILES['gallery']['name']);

        if (file_exists($image_path.$target_file)) {
            unlink($image_path.$target_file);
        }

        if (move_uploaded_file($tmp_file, $image_path.$target_file)) {

            // continue here

        } else {
            $error['alert']             = "There was a problem with the file upload.";

            $error['gallery']               = $upload_errors[$_FILES['gallery']['error']];

            include 'gallery_form.php';
        }
    }

这是表单代码

   <form action="" method="POST" autocomplete="off">

        <?php if (isset($error['alert'])) { ?>
            <div class="alert"><?php echo $error['alert']; ?></div>
        <?php } ?>

        <div>
            <label for="gallery">Image*</label>
            <input type="hidden" name="MAX_FILE_SIZE" value="1048576" />
            <input type="file" name="gallery" id="gallery"/>

            <?php if (isset($error['gallery'])) { ?><div class="error"><?php echo $error['gallery']; ?></div><?php } ?>

            <label for="caption">Image Caption</label>
            <input type="text" name="caption" id="caption" placeholder="Caption" value="<?php if (isset($input['caption'])) { echo $input['caption']; } ?>" />

            <?php if (isset($error['caption'])) { ?><div class="error"><?php echo $error['caption']; ?></div><?php } ?>

            <p class="required">* Required</p>

            <input type="submit" name="submit" id="submit" class="submit" value="Submit" />
        </div>
    </form>

错误来自此处的代码

            if ($_FILES['gallery']['error']) { $error['gallery']    =   $upload_errors[$_FILES['gallery']['error']]; }

所以我该如何解决呢。

0 个答案:

没有答案