Trying to upload file, but php doesn't find the files that i attach in my form despite having the encpart attribut correctly set?

huangapple go评论66阅读模式
英文:

Trying to upload file, but php doesn't find the files that i attach in my form despite having the encpart attribut correctly set?

问题

Here's the translation of the code parts you provided:

// Tests if the file exists and doesn't have errors
if (isset($_FILES['idavant']['tmp_name']) && $_FILES['idavant']['error'] == 0) {
    // Tests if the file isn't too big
    if ($_FILES['idavant']['size'] <= 15000000)
    {
        // Tests if it is an authorized file extension
        $fileInfo = pathinfo($_FILES['idavant']['name']);
        $extension = $fileInfo['extension'];
        $allowedExtensions = ['jpg', 'jpeg', 'gif', 'png'];
        if (in_array($extension, $allowedExtensions))
        {
            // If all conditions are satisfied, file is uploaded definitively to the server
            move_uploaded_file($_FILES['idavant']['tmp_name'], 'uploads/' . basename($_FILES['idavant']['name']));
            echo "The upload was successful!";
        } else {
            error_log("Error uploading the file\n", 3, "log1.txt");
        }
    } else {
        error_log("Error uploading the file: The file is too large;\n", 3, "log1.txt");
    }
} else {
    error_log("Error uploading the file: Error in the transfer process between pages;\n", 3, "log1.txt");
    // Tests if the file exists. Always throws the file doesn't exist error.
    if (isset($_FILES['idavant']['tmp_name'])) {
        echo "The file exists.";
    } else {
        echo "The file does not exist";
    }
    // Tests if the file hasn't errors attached to it. This time, it tells me that there is no errors.
    if ($_FILES['idavant']['error'] == 0) {
        echo "The file is error-free";
    } else {
        echo "Nope";
    }
    // Dumping the content of the different $_FILES variable. Returns an empty array and two NULLS.
    echo "<br/>" . var_dump($_FILES);
    echo "<br/>" . var_dump($_FILES['idavant']);
    echo "<br/>" . var_dump($_FILES['idarriere']);
}

Please note that the code contains some potential issues and may not work as expected. You should check the server configuration and make sure the file paths and permissions are set correctly for the file uploads to work.

英文:

After correcting a typo and succesfully sent to my database the mostly text fields to my sql database, i was trying now to upload the two files the user sends in the form (2 faces of an ID card) to my server. However, despite having the encpart=&quot;multipart/form-data&quot; and the post method in the form's tag, and having checked the names of both the input fields and their uses in the php files, php doesn't find the files sent.

I have files upload activated in my php.ini, with upload limits well above the size of the test images i sent (respectively 261 and 193 KB), with max file upload at 8 MB and post method limits at 250 MB. I am using php 8.1.0.

The html form (all of the other fields except the file inputs works) :

&lt;section id=&quot;form&quot;&gt;
            &lt;form action=&quot;submit_signature.php&quot; method=&quot;post&quot; encpart=&quot;multipart/form-data&quot;&gt;
                &lt;label for=&quot;nom&quot;&gt;Nom :&lt;/label&gt;&lt;br&gt;
                &lt;input type=text name=&quot;Nom&quot; id=&quot;Nom&quot; class=&quot;big&quot; required&gt;&lt;br&gt;
                &lt;label for=&quot;prenom&quot;&gt;Pr&#233;nom :&lt;/label&gt;&lt;br&gt;
                &lt;input type=text name=&quot;Pr&#233;nom&quot; id=&quot;prenom&quot; class=&quot;big&quot; required&gt;&lt;br&gt;
                &lt;label for=&quot;pseudo&quot;&gt;Pseudonyme :&lt;/label&gt;&lt;br&gt;
                &lt;input type=text name=&quot;Pseudonyme&quot; id=&quot;pseudo&quot; class=&quot;big&quot; placeholder=&quot;Nom sous lequel vous appara&#238;trez dans les derni&#232;res signatures.&quot;&gt;&lt;br&gt;
                &lt;label for=&quot;daten&quot;&gt;Date de naissance :&lt;/label&gt;&lt;br&gt;
                &lt;input type=date name=&quot;Datenaissance&quot; id=&quot;daten&quot; min=&quot;1907-03-04&quot; max=&quot;2006-01-01&quot; class=&quot;smol&quot; required&gt;&lt;br&gt;
                &lt;label for=&quot;email&quot;&gt;E-mail :&lt;/label&gt;&lt;br&gt;
                &lt;input type=email name=&quot;Email&quot; id=&quot;email&quot; class=&quot;smol&quot; placeholder=&quot;Adresse mail en cas de p&#233;pin pour vous contacter !&quot; required&gt;&lt;br&gt;
                &lt;label for=&quot;tel&quot;&gt;N&#176; de t&#233;l&#233;phone :&lt;/label&gt;&lt;br&gt;
                &lt;input type=tel name=&quot;tel&quot; id=&quot;tel&quot; placeholder=&quot;+(X)XX X XX XX XX XX&quot; class=&quot;smol&quot; pattern=&quot;+[0-972]{3}-[0-9]{1}-[0-9]{2}-[0-9]{2}-[0-9]{2}-[0-9]{2}&quot; required&gt;&lt;br&gt;
                &lt;label for=&quot;idavant&quot;&gt;Face avant de votre pi&#232;ce d&#39;identit&#233; :&lt;/label&gt;
                &lt;input type=&quot;file&quot; id=&quot;idavant&quot; name=&quot;idavant&quot; value=&quot;download&quot; required&gt;&lt;br&gt;
                &lt;label for=&quot;idarriere&quot;&gt;Face arri&#232;re de votre pi&#232;ce d&#39;identit&#233; :&lt;/label&gt;
                &lt;input type=&quot;file&quot; id=&quot;idarriere&quot; name=&quot;idarriere&quot; value=&quot;download&quot; required&gt;&lt;br&gt;
                &lt;label for=&quot;motiv&quot;&gt;Motivation :&lt;/label&gt;&lt;br&gt;
                &lt;textarea name=&quot;Motivation&quot; id=&quot;motiv&quot; class=&quot;big&quot; placeholder=&quot;Dites nous pourquoi vous signez cette p&#233;tition, si cela vous dit !&quot;&gt;&lt;/textarea&gt;&lt;br&gt;&lt;br&gt;
                &lt;input type=submit name=&quot;Envoyer&quot; id=&quot;envoyer&quot; value=&quot;Envoyer&quot;&gt;
                &lt;input type=reset name=&quot;R&#233;initialiser&quot; id=&quot;reset&quot; value=&quot;R&#233;initialiser&quot;&gt;
            &lt;/form&gt;
            &lt;br&gt;
            &lt;p id=&quot;formwarning&quot;&gt;NB : Votre signature ne sera pas prise en compte tant que votre identit&#233; ne sera pas v&#233;rifi&#233;e, et votre inscription certifi&#233;e par nos administrateurs™.&lt;/p&gt;
        &lt;/section&gt;

The php code i use to check and then send the file to the server. I've only done tests on the idavant file.

// Tests if the file exists and doesn&#39;t have errors
    if (isset($_FILES[&#39;idavant&#39;][&#39;tmp_name&#39;]) &amp;&amp; $_FILES[&#39;idavant&#39;][&#39;error&#39;] == 0) {
        // Tests if the file isn&#39;t too big
        if ($_FILES[&#39;idavant&#39;][&#39;size&#39;] &lt;= 15000000)
        {
                // Tests if it is an authorized file extension
                $fileInfo = pathinfo($_FILES[&#39;idavant&#39;][&#39;name&#39;]);
                $extension = $fileInfo[&#39;extension&#39;];
                $allowedExtensions = [&#39;jpg&#39;, &#39;jpeg&#39;, &#39;gif&#39;, &#39;png&#39;];
                if (in_array($extension, $allowedExtensions))
                {
                        // If all conditions are satisfied, file is uploaded definitively to the server
                        move_uploaded_file($_FILES[&#39;idavant&#39;][&#39;tmp_name&#39;], &#39;uploads/&#39; . basename($_FILES[&#39;idavant&#39;][&#39;name&#39;]));
                        echo &quot;L&#39;envoi a bien &#233;t&#233; effectu&#233; !&quot;;
                } else {
                    error_log(&quot;Erreur de t&#233;l&#233;chargement du fichier\n&quot;, 3, &quot;log1.txt&quot;);
                }
        } else {
            error_log(&quot;Erreur de t&#233;l&#233;chargement du fichier : Le fichier est trop gros;\n&quot;, 3, &quot;log1.txt&quot;);
        }
    } else {
        error_log(&quot;Erreur de t&#233;l&#233;chargement du fichier : Erreur dans la proc&#233;dure de transfert entre les pages;\n&quot;, 3, &quot;log1.txt&quot;);
        //Tests if the file exists. Always throws the file doesn&#39;t exist error.
        if (isset($_FILES[&#39;idavant&#39;][&#39;tmp_name&#39;])) {
            echo &quot;Le fichier existe.&quot;;
        } else {
            echo &quot;Le fichier n&#39;existe pas&quot;;
        }
        //Tests if the file hasn&#39;t errors attached to it. This time, it tells me that there is no errors.
        if ($_FILES[&#39;idavant&#39;][&#39;error&#39;] == 0) {
            echo &quot;Le fichier est bien sans erreur&quot;;
        } else {
            echo &quot;bah non mdr&quot;;
        }
        //Dumping the content of the different $_FILES variable. Returns an empty array and two NULLS.
        echo &quot;&lt;br/&gt;&quot;.var_dump($_FILES);
        echo &quot;&lt;br/&gt;&quot;.var_dump($_FILES[&#39;idavant&#39;]);
        echo &quot;&lt;br/&gt;&quot;.var_dump($_FILES[&#39;idarriere&#39;]);
    }

答案1

得分: -1

I've corrected the typos in my code pointed out by the comments + another one that a friend of mine showed (used the wrong upload folder). After that and a few tests, my code works now, so I'll leave the corrected one there:

    if (isset($_FILES['idarriere']['tmp_name']) && $_FILES['idarriere']['error'] == 0) {
        // Tests if the file isn't too big
        if ($_FILES['idarriere']['size'] <= 15000000)
        {
                // Tests if it is an authorized file extension
                $fileInfo = pathinfo($_FILES['idarriere']['name']);
                $extension = $fileInfo['extension'];
                $allowedExtensions = ['jpg', 'jpeg', 'gif', 'png'];
                if (in_array($extension, $allowedExtensions))
                {
                        // If all conditions are satisfied, file is uploaded definitively to the server
                        move_uploaded_file($_FILES['idarriere']['tmp_name'], 'fichiers/' . basename($_FILES['idarriere']['name']));
                        $target_file2 = 'fichiers/'.basename($_FILES['idarriere']['name']);
                        echo " L'envoi de la face verso a bien été effectué !";
                } else {
                    error_log("Erreur de téléchargement du fichier\n", 3, "log1.txt");
                }
        } else {
            error_log("Erreur de téléchargement du fichier : Le fichier est trop gros;\n", 3, "log1.txt");
        }
    } else {
        error_log("Erreur de téléchargement du fichier : Erreur dans la procédure de transfert entre les pages;\n", 3, "log1.txt");
        // Tests if the file exists.
        if (isset($_FILES['idarriere']['tmp_name'])) {
            echo " Le fichier existe.";
        } else {
            echo " Le fichier n'existe pas";
        }
        // Tests if the file hasn't errors attached to it.
        if ($_FILES['idarriere']['error'] == 0) {
            echo " Le fichier est bien sans erreur";
        } else {
            echo " Il semble y avoir une erreur dans le fichier.";
        }
    }```


<details>
<summary>英文:</summary>

I&#39;ve corrected the typos in my code pointed out by the comments + another one that a friend of me showed (used the wrong upload folder). After that and a few test my code works now, so i&#39;ll leave the corrected one there :

```// Tests if the file exists and doesn&#39;t have errors
    if (isset($_FILES[&#39;idarriere&#39;][&#39;tmp_name&#39;]) &amp;&amp; $_FILES[&#39;idarriere&#39;][&#39;error&#39;] == 0) {
        // Tests if the file isn&#39;t too big
        if ($_FILES[&#39;idarriere&#39;][&#39;size&#39;] &lt;= 15000000)
        {
                // Tests if it is an authorized file extension
                $fileInfo = pathinfo($_FILES[&#39;idarriere&#39;][&#39;name&#39;]);
                $extension = $fileInfo[&#39;extension&#39;];
                $allowedExtensions = [&#39;jpg&#39;, &#39;jpeg&#39;, &#39;gif&#39;, &#39;png&#39;];
                if (in_array($extension, $allowedExtensions))
                {
                        // If all conditions are satisfied, file is uploaded definitively to the server
                        move_uploaded_file($_FILES[&#39;idarriere&#39;][&#39;tmp_name&#39;], &#39;fichiers/&#39; . basename($_FILES[&#39;idarriere&#39;][&#39;name&#39;]));
                        $target_file2 = &#39;fichiers/&#39;.basename($_FILES[&#39;idarriere&#39;][&#39;name&#39;]);
                        echo &quot; L&#39;envoi de la face verso a bien &#233;t&#233; effectu&#233; !&quot;;
                } else {
                    error_log(&quot;Erreur de t&#233;l&#233;chargement du fichier\n&quot;, 3, &quot;log1.txt&quot;);
                }
        } else {
            error_log(&quot;Erreur de t&#233;l&#233;chargement du fichier : Le fichier est trop gros;\n&quot;, 3, &quot;log1.txt&quot;);
        }
    } else {
        error_log(&quot;Erreur de t&#233;l&#233;chargement du fichier : Erreur dans la proc&#233;dure de transfert entre les pages;\n&quot;, 3, &quot;log1.txt&quot;);
        //Tests if the file exists.
        if (isset($_FILES[&#39;idarriere&#39;][&#39;tmp_name&#39;])) {
            echo &quot; Le fichier existe.&quot;;
        } else {
            echo &quot; Le fichier n&#39;existe pas&quot;;
        }
        //Tests if the file hasn&#39;t errors attached to it.
        if ($_FILES[&#39;idarriere&#39;][&#39;error&#39;] == 0) {
            echo &quot; Le fichier est bien sans erreur&quot;;
        } else {
            echo &quot; Il semble y avoir une erreur dans le fichier.&quot;;
        }
    }```

</details>



huangapple
  • 本文由 发表于 2023年5月15日 03:24:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/76249306.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定