fopen does not open uploaded files

0

Colleagues.

I need to populate a table with a CSV file and am using a simple PHP / Mysql code for this. But even with the 755 permission on the file, I can not. It says that the file could not be found. Here is the code:

// PHP
 if($_POST){            
           $arquivo = $_FILES['Arquivo']['name'];
           $arquivoTemp = $_FILES['Arquivo']['tmp_name'];
           $diretorio = "arquivo/".$arquivo;
           move_uploaded_file($arquivoTemp, $diretorio);
           $abrirArquivo = fopen(basename($_FILES['Arquivo']['tmp_name']), "r");

           if (!$abrirArquivo){
               echo "arquivo não encontrado!"; // Ele para aqui!
            }else{
                while ($valores = fgetcsv ($abrirArquivo, 2048, ";")) {
                       //aqui faço a inserção
                }
            }
        }

// HTML
<form method="post" enctype="multipart/form-data">
        <table width="500px" border="1" style="border-collapse: collapse">
            <tr>
                <td><input type="file" name="Arquivo"></td>
                <td><input type="submit" name="Enviar" value="Enviar"></td>
            </tr>
        </table>
        </form>
    
asked by anonymous 19.07.2016 / 20:27

1 answer

1

The problem is that you are using fopen with the temporary file path, which has already been moved by move_uploaded_file .

Try to make this change in the code:

<?php
 if($_POST){            
           $arquivo = $_FILES['Arquivo']['name'];
           $arquivoTemp = $_FILES['Arquivo']['tmp_name'];
           $diretorio = "arquivo/".$arquivo;
           move_uploaded_file($arquivoTemp, $diretorio);
           $abrirArquivo = fopen($diretorio, "r");

           if (!$abrirArquivo){
               echo "arquivo não encontrado!"; // Ele para aqui!
            }else{
                while ($valores = fgetcsv ($abrirArquivo, 2048, ";")) {
                       //aqui faço a inserção
                }
            }
        }
?>
    
19.07.2016 / 22:22