How to echo a php file?

Asked

Viewed 2,501 times

2

I’m looking to create a simple email marketing server. So for this I have to take the content 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. However 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 into a variable?

2 answers

3


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

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 the $_FILES['arquivo']['tmp_name'] take a look at the documentation of PHP about uploading files.

More about the role in the documentation of PHP.

  • The file comes from an input

  • Yeah, I just showed you how to use the function.

2

You can do the following:

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

Explanation:

When giving a var_dump() of the variable $_FILES['arquivo'] we may 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)
}

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

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

Browser other questions tagged

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