未定义的索引:图像 1 以 $_FILES 为单位


Undefined index: image1 in $_FILES

提交表单时出现错误。我尝试在输入图像中上传图像1我已经指定了多部分/表单数据,并指定了输入的名称值:image1我也尝试了使用post方法和get方法,但$ _FILES仍然是空的 错误是:注意:未定义的索引:图像1

这是 HTML 代码

<form  enctype="multipart/form-data" class="formulaire" method="get" action="../controllers/import_controller.php">
                    <table class="table">
                    <tr bgcolor="#f39c12">
                        <th align="center" width="33%">CATEGORIE</th>
                        <th align="center" width="33%">SOUS CATEGORIE</th>
                    </tr>
                    <tr>
                      <td><select type="text" name="categorie" placeholder="Categorie" onchange="liste_sous_categorie(this.value)" required/><?php liste_categorie(); ?></select></td>
                      <td><div id="liste_sous_categorie"><select required/></select></div></td>
                    </tr>
                    </table>
                    <table  id="image_add" class="table">
                      <tr>
                      <td><span>LIBELLE</span></td>
                      <td><input type="text" name="libelle" required/></td>
                      </tr>
                      <tr>
                      <td><span>DESCRIPTION</span></td>
                      <td><textarea name="description" required></textarea></td>
                      </tr>
                      <tr>
                      <td><span>PRIX</span></td>
                      <td><input min="1" type="number" name="prix" required/></td>
                      </tr>
                      <tr>
                      <td><span>STOCK</span></td>
                      <td><input min="1" type="number" name="stock" required/></td>
                      </tr>
                      <tr>
                      <td><span>IMAGE 1</span></td>
                      <td><input type="file" name="image1" accept="image/*" required/></td>
                      </tr>
                    </table>
                    <input type="hidden" value="1" name="nbre_image_article" id="nbre_image_article">
                    <input type="hidden" value="1" name="article_add">
                    <center><input align="center" type="submit" value="VALIDER" class="btn btn-primary" style="margin-right: 5px;"></input><i class="fa fa-task"></i></center>
                    <br>
                 </form> 

和PHP代码

if(isset($_GET['article_add']))
{
$libelle=addslashes(htmlentities($_GET['libelle']));
$description=addslashes(htmlentities($_GET['description']));
$prix=$_GET['prix'];
$stock=$_GET['stock'];
$sous_categorie_id_sous_categorie=$_GET['sous_categorie'];
$nbre_image_article=$_GET['nbre_image_article'];
session_start();
$uploaddir = '../../views/images/articles/';
$uploadfile = $uploaddir . basename($_FILES['image1']['name']);
echo $_FILES['image1']['error'];
echo '<pre>';
if (move_uploaded_file($_FILES['image1']['tmp_name'], $uploadfile)) {
    echo "Le fichier est valide, et a été téléchargé
           avec succès. Voici plus d'informations :'n";
} else {
    echo "Attaque potentielle par téléchargement de fichiers.
          Voici plus d'informations :'n";
}
}

上传文件需要使用 POST 方法,而不是 GET。

因此,您需要将表单的方法更改为该method="post"

您还需要将所有$_GET实例更改为 $_POST

  • 在您最喜欢的编辑器中快速查找和替换,将立即更改它们。

如果不这样做,您将面临更多未定义的索引通知。

参考:

  • http://php.net/manual/en/features.file-upload.post-method.php

您还需要确保您尝试上传到的文件夹具有为其设置的适当权限。

参考:

  • http://php.net/manual/en/function.chmod.php

或者,通过 FTP 修改文件夹的权限。

将错误报告添加到文件顶部,这将有助于查找错误

<?php 
error_reporting(E_ALL);
ini_set('display_errors', 1);
// Then the rest of your code

旁注:显示错误只能在暂存中完成,而不应在生产中完成。


福笔记:

我注意到您正在使用session_start();.

确保你没有在标头之前输出,如果上面有任何输出。

最好将其放在(完整(代码的顶部。

如果您看到标头发送通知,您可以在 Stack 上查阅以下内容:

  • 如何修复PHP中的"标头已发送"错误

如果您没有使用任何$_SESSION数组(您发布的代码中没有数组(,则可以安全地从代码中删除session_start();