fopen does not open uploaded files

Asked

Viewed 135 times

0

Colleagues.

I need to popular a table with a CSV file and I am using a simple PHP/Mysql code for this. But even with 755 permission in the file, I can’t. It says that the file could not be found. Follow 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>
  • Do you have any debug type? Xdebug? puts the fixed file path in a variable to test.

1 answer

1


The problem is that you are using the 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
                }
            }
        }
?>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.