How to echo a file in php?

2

I'm wanting to create a simple Email marketing server. So for this I have to get the contents of an HTML and save a variable in PHP. This is my form.

<form method="post" action="email.php" enctype="multipart/form-data">
    <h1> Enviar E-mails </h1>
    <table>
        <tr><td> E-mail: </td><td> <input type="text" name="assunto"></td></tr>
        <tr><td> Assunto: </td><td> <input type="text" name="a"></td></tr>
        <tr><td>Arquivo HTML </td><td> <input type="file" name="arquivo" /></td></tr>
        <tr><td><input type="submit" value="ok"> </td><td> </td></tr>
    </table>
</form>

I also made a php file to get this information. But it returns an error: Array to string conversion in C: \ xampp \ htdocs \ emailmkt \ email.php on line 3

<?php
$arquivo = $_FILES['arquivo'];
echo $arquivo;
?>

How do I convert this value to string? How do I put this html file in a variable?

    
asked by anonymous 20.05.2015 / 18:47

2 answers

3

You can use the file_get_contents() function, which will return the contents of the file as a string.

Example:

<?php
$data = file_get_contents("AquiOLocalDoArquivo");

//no seu caso ficaria assim:
//Como o @gmsantos escreveu na resposta dele

$data = file_get_contents($_FILES['arquivo']['tmp_name']);
echo $data.

?>

For you to understand about $_FILES['arquivo']['tmp_name'] of a look at the documentation for PHP about uploading files.

More about the function in the documentation PHP .

    
20.05.2015 / 18:53
2

You can do the following:

$conteudo = file_get_contents($_FILES['arquivo']['tmp_name']);

Explanation:

When giving var_dump() of variable $_FILES['arquivo'] we can notice an array with some attributes of the file:

array(5) {
  ["name"]=>
  string(10) "arquivo.txt"
  ["type"]=>
  string(10) "text/plain"
  ["tmp_name"]=>
  string(48) "C:\Windows\temp\php9775.tmp"
  ["error"]=>
  int(0)
  ["size"]=>
  int(102)
}

At position tmp_name we have the location of the file on the disk. By default, PHP writes the uploaded files to the temporary system folder.

With this path, we can use file_get_contents() to then read the content of the file and store it in a variable or print something on the screen.

    
20.05.2015 / 18:52