How to reload a page without losing inputfile

Asked

Viewed 49 times

4

Hello, I have a PHP me code where I use simplexml_load_file to read an xml on this page I need to send a form with submit, My problem is that by sending this form xml data is lost. And I would like to know how I can keep my xml and send the submit?

NOTE: I have tried using $_SESSION to store the file address but it didn’t work.

Test.php

<!doctype html>
<html lang="pt-br">
  <head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <title> Projeto GP </title>
  </head>
  <body>
  <pre>
    <?php 
        print_r($_POST); 
    ?>
    </pre>
    <?php
    
        #------------------------------------------------------------------------------------------
        $myfile2 = $_FILES['XML']['tmp_name'];
        $xml = simplexml_load_file($myfile2);
        $frutas_array = $xml 
        #Recebe o XML e le ele 
        #------------------------------------------------------------------------------------------
    ?>
    <div>
        <form id ="form4.2" action="<?= $_SERVER['PHP_SELF'] ?>" method="post">
        <?php 
            foreach($frutas_array as $key => $fruta):
            #recuperando dados do xml atraves de um foreach
            #------------------------------------------------------------------------------------------
        ?>
            <label>A fruta é <?= $fruta ?></label>
            <input type="hidden" value="<?= $fruta ?>" name="frutas[<?= $key ?>][nome]">
            <input type="" name="frutas[<?= $key ?>][cor]">
            <!--criação de inputs a cada item do foreach -->
        <?php 
        endforeach 
        #finalizando o foreach apos a criação do formulario
        #------------------------------------------------------------------------------------------
        ?>
        <input type="submit" name="">
        <!--submit com direção para esta propria pagina -->
        </form>
    </div>    
</body>
</html>
  • 1

    can’t instead the file path, read the file in php and save what matters in Session? can even parse the xml for a class

2 answers

2

This cannot be done for security reasons. The action of selecting the file has to be done manually by the user.

To be clear imagine the following situation: I own the site create a form with an input file and in the value of that input I put a path to a very generic file, for example: C:/Documents/passwords.txt, and add a javascript to run the form Submit automatically. I could get the file from the person’s computer without her knowing.

  • i understood what said but the user would have already manually selected the file before so I would only use the same file again

1


William, if I understand what you want to do, create 2 separate forms on the same page: one to send XML and the other to the fruit Submit. Each with its own upload button. Thus, in the first interaction the user selects the XML and sends it to the server. Then the server processes and creates the second form and in the second interaction the user puts the colors and sends again and the server processes the second Submit.

If you really need the uploaded file, use the PHP functions to create a copy of it. Save in the session the address of this copy.

Editing:

To easily save XML to the session, read the file sent with file_get_contents. It will return a string with the XML content. Save this return in $_SESSION.

Instead of using simplexml_load_file use simplexml_load_string using the return given by file_get_contents or $_SESSION, depending on what exists.

Remembering that you should do the necessary checks, and at some point clear the value saved in the session.

End edition


Analyzing the code, you used the $frutas_array in the foreach but nowhere did you initialize it.

The closest I could find was $xml = $frutas_array is inverted?

It is important to remember that the method simplexml_load_file returns Simplexmlelement. It is not Array but implements Traversable, so it can be used in foreach.

Depending on the XML structure you will need to access the method children to reach the level that contains the fruit list. Example:

$frutas_array = $xml->children()->children();

See more information on Children.

If you like to be visual, add the following before the foreach to see what is "coming":

echo '<pre>'; var_dump($frutas_array); echo '</pre>';

PS: In some PHP installations all alerts and errors are deleted so as not to leak important information. In some of these cases, add error_reporting(6143); at the beginning of the script activates the alerts. In other cases only changing the PHP configuration. Sometimes this can help to check what is happening.

  • Yes it really was a mistake of mine $xml = $frutas_array this inverted and already edited, but all part of reading the xml and show its contents is working perfect more when I send form this page download makes me lose xml

  • 1

    It disappears because it was not sent again, but you saved its data in the "fruits" array that is sent in the POST. But if you want to save the original file for reuse, I will edit the reply with possibility to save in session.

Browser other questions tagged

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