-1

I have a problem uploading some files through a form in PHP. The files are not uploaded. I have checked that the file name is correct, that it receives the temporary folder, and the final path, but nothing at all. The permissions of that folder are 777. I have also checked that the php.ini file is set to "file_uploads = On"

I've looked at different code examples and I can't find the error why my files won't upload to the server.

The fact is that the form processes it well, the name is generated perfectly for me because it saves it in the database... But I don't know why it doesn't upload the files to the server

I leave you the code of the html form:

<form class="row justify-content-center" enctype="multipart/form-data" action="procesar.php" method="post">
                    <div class="col-md-8">
                        <div class="form-1 row g-4">
                            <div class="col-12 col-lg-6">
                                <input type="text" class="form-control" name="nombre" placeholder="Nombre" required>
                            </div>
                            <div class="col-12 col-lg-6">
                                <input type="text" class="form-control" name="apellidos" placeholder="Apellidos" required>
                            </div>
                            <div class="col-12 col-lg-6">
                                <input type="number" class="form-control" name="telefono" placeholder="Teléfono" required>
                            </div>
                            <div class="col-12 col-lg-6">
                                <input type="email" class="form-control" name="email" placeholder="Email" required>
                            </div>
                            <div class="col-12 col-lg-6">
                                <input type="text" class="form-control" name="ciudad" placeholder="Ciudad" required>
                            </div>
                            <div class="col-12 col-lg-6">
                                <input type="text" class="form-control" name="tienda" placeholder="Nombre de la tienda" required>
                            </div>
                        </div>
                    </div>
                    <div class="col-md-8">
                        <div class="form-2 row">
                            <div class="col-12 col-lg-6">
                                <label for="Tiquet">Foto del tiquet</label>
                                <input type="file" class="form-control" name="tiquet" placeholder="tiquet" required>
                            </div>
                            <div class="col-12 col-lg-6">
                                <label for="Tiquet">Foto del material</label>
                                <input type="file" class="form-control" name="material" placeholder="material" required>
                            </div>
                        </div>
                        <div class="col-8 mt-3 btn-enviar text-center">
                            <button type="submit" class="btn btn-danger" name="enviar">Enviar formulario</button>
                        </div>
                    </div>
                    
                </form>

And this is the function that uploads the file to the server

public function subirImagenTiquet()
{   
    $directorio = "../uploads/";
    $info = new SplFileInfo($_FILES["tiquet"]["name"]);
    $nombre = strtolower($this->getNombre()) . "_tiquet_" . $this->getAleatorio() . "." . $info->getExtension();
    $dirArchivoTemporal = $_FILES["tiquet"]["tmp_name"];
    $dirArchivoFinal = $directorio . $nombre;
    move_uploaded_file($dirArchivoTemporal, $dirArchivoFinal);
    $this->setTiquet($dirArchivoFinal);
}

The var_dump($_FILES)

array(2) { 
  ["tiquet"]=> array(5) { ["name"]=> string(16) "circle-black.jpg" ["type"]=> string(10) "image/jpeg" ["tmp_name"]=> string(14) "/tmp/phph41ROK" ["error"]=> int(0) ["size"]=> int(467) } 
  ["material"]=> array(5) { ["name"]=> string(15) "circle-gray.jpg" ["type"]=> string(10) "image/jpeg" ["tmp_name"]=> string(14) "/tmp/phpRbIpqA" ["error"]=> int(0) ["size"]=> int(404) } 
}

The var_dump result move_uploaded_file

bool(false) bool(false)

now

bool(false) Warning: move_uploaded_file(../uploads/jaimito_material_6431785209.jpg): failed to open stream: No such file or directory in /home/airlines/public_html/test/chino/includes/Form.php on line 69

Warning: move_uploaded_file(): Unable to move '/tmp/phpajTVlX' to '../uploads/jaimito_material_6431785209.jpg' in /home/airlines/public_html/test/chino/includes/Form.php on line 69

ADyson
  • 51,527
  • 13
  • 48
  • 61
Rodrypaladin
  • 188
  • 5
  • 1
    `var_dump($_FILES);` first of all so we can see the contents during the upload. Second, enable PHP error reporting in case any warnings come during the move_uploaded_file operation. – ADyson May 11 '22 at 13:51
  • 2
    Also you are not checking any of the [errors](https://www.php.net/manual/en/features.file-upload.errors.php) that could occur during the file upload – DarkBee May 11 '22 at 13:52
  • And `move_uploaded_file()` itself also returns a boolean, which is `false` when there's an error. – KIKO Software May 11 '22 at 13:53
  • @ADyson How could I display the error if it returns false? – Rodrypaladin May 11 '22 at 13:57
  • 1
    `$result = move_uploaded_file($dirArchivoTemporal, $dirArchivoFinal); var_dump($result);` ?? But it will also separately issue a warning in some circumstances too (the documentation explains - https://www.php.net/manual/en/function.move-uploaded-file.php) – ADyson May 11 '22 at 13:58
  • @ADyson I just edited with the result var_dump – Rodrypaladin May 11 '22 at 14:01
  • 1
    You seem to have accidentally pasted the same data for both dumps... – ADyson May 11 '22 at 14:01
  • @ADyson now ??? – Rodrypaladin May 11 '22 at 14:03
  • Well, `false` means there's an error, but you still don't know which error. So you should [output PHP errors](https://stackoverflow.com/questions/1053424/how-do-i-get-php-errors-to-display), to see that. – KIKO Software May 11 '22 at 14:05
  • 1
    Ok that's better. So it means that move_uploaded_file failed. Are you 100% certain that you enabled PHP error reporting and/or logging? See https://stackify.com/php-error-logs-guide/ for guidance. As per the documentation I linked you to earlier, move_uploaded file will return false _without_ a warning if the upload file isn't valid. But that seems unlikely, based on your $_FILES data. It will issue false _with_ a warning if the file cannot be moved (e.g. because the folder doesn't exist or there are permissions problems). We need to know exactly what is the case. – ADyson May 11 '22 at 14:06
  • 1
    Well "No such file or directory" seems pretty clear, don't you think? make sure you've specified the target path correctly. – ADyson May 11 '22 at 14:12
  • In this case you might want to use an absolute path instead of a relative path. Then the location isn't dependent on the location of your main script anymore. – KIKO Software May 11 '22 at 14:14
  • 1
    Or you might want to define it relative to the site root or something instead of being relative to the current script. – ADyson May 11 '22 at 14:15
  • 1
    @ADyson Solved. The path was wrong, because I started from the class where the logic was, and I had to start from the path where I start the instance of that class, so the path did not exist. Then it seemed that it did not upload the files, but it turns out that the filezilla showed me what it had in cache, I had to close and open the filezilla again – Rodrypaladin May 11 '22 at 14:23

0 Answers0