Search code examples
phpfile-uploadenctype

enctype and file upload


i made a form page to upload files

<form role="form" action="portfilio.php" method="post" enctype="multipart/from-data"> <textarea class="form-control textarea" name="desc"></textarea>
<input type="file" name= "img" class="custom-file-input" id="exampleInputFile">

when i want to test it if he is working or not by using this code

if (isset($_POST['desc'])) { echo '<pre>'; print_r($_FILES);die; }

should send somthing like result but not just empty array like : Array () but why ? where the wrong ??


Solution

  • (1) Please fix the typo and use enctype="multipart/form-data"

    (2) Please use $_POST and $_FILES to get the submitted data details, as follows:

    print_r($_POST);
    print_r($_FILES);
    

    So , amend your code to

    HTML

    <form  action="test1.php" method="post" enctype="multipart/form-data">  
    <textarea class="form-control textarea" name="desc"></textarea>
    
    <input type="file" name= "img" class="custom-file-input" id="exampleInputFile">
    <input type=submit>
    

    and

    test1.php

    <?php
    if (isset($_POST['desc'])) { 
    
    print_r($_POST);
    echo "<br>";
    print_r($_FILES);
    
    //die; 
    }
    
    ?>
    
    

    The result (if you type something in the textarea and pick a file, then submit) will be like (well tested):

    Array ( [desc] => Test1 Test2 )
    Array ( [img] => Array ( [name] => 2.jpg [type] => image/jpeg [tmp_name] => /tmp/phpO9vL4C [error] => 0 [size] => 22904 ) )