Download and upload form in the same button

0

I'm new to php and I can not create a button inside a form that uploads and downloads at the same time. How do I generate it all inside a single one, ie in the "send" button, and thus delete the html download button?

My code:

<form action="" method="post" enctype="multipart/form-data" onsubmit="return validate(this)">
        <input type="hidden"  name="<?php echo ini_get('session.upload_progress.prefix').ini_get('session.upload_progress.name'); ?>" value="123" />  
    <labeL>
    <span>Choose CSV file:</span>
    </span></span>
        <input type="file" accept="text/csv" name="userfile" id="csvfile" />
    </labeL>
    <p> </p>
        <p id="file-message"></p> 
    <input type="submit"  name="action" id="btnSend"  value="Send" />  

</form>

<form action="download.php" method="post" value="download">
<p></p>  

<input type="submit" name="btnDownload" id="btnDownload"   value="Download PDF File" <?php if ((strcmp($message,$userMessage)!=0)|| (empty($userFile))){ ?> disabled  <?php } ?> />

</form>
    
asked by anonymous 16.10.2018 / 02:26

1 answer

0

First you will have to upload, if successful just display a link pointing to where the file was stored.

I saw that your code has some validations in javascript, below is an example "CRU", so you can use it as a reference.

Create a file called upload.php, and put the code below:

<html>
<head></head>
<body>
<form action="upload.php" method="post" enctype="multipart/form-data">
    <input type="file"  name="arquivo"/>
    <input type="submit" name="btnEnviar"   value="Enviar" />  
</form>


<?php

if($_POST)
{

    $origem = $_FILES['arquivo']['name'];

    $diretorio = 'uploads';
    $destino   = $diretorio.'/'.$origem;

    if(!file_exists($diretorio))
    {
        mkdir($diretorio);
    }

    if (move_uploaded_file($_FILES['arquivo']['tmp_name'], $destino))
    {
        echo "<a href='{$destino}'> Baixar Arquivo / Visualizar Arquivo </a>";
    } else {
        echo "Possível ataque de upload de arquivo!\n";
    }
}
?>

</body>
</html>

Explaining

<form action="upload.php" method="post" enctype="multipart/form-data">
    <input type="file"  name="arquivo"/>
    <input type="submit"  value="Enviar" />  
</form>
  • In the form the enctype="multipart / form-data" has been added because it is a file upload;
  • A file input field called file was also created;
  • When you click the submit button, the form is forwarded to the same page, then checked to see if $ _POST is true:

    if($_POST)
    {
       ...
    }

    OBS ::. The $ _POST is true, when a form is submitted and its method is post

    Knowing that POST is true, the file name is assigned the $ source variable:

     $origem = $_FILES['arquivo']['name'];

    Next in the directory variable the location where the files are to be written is informed, and in the $ target variable the directory with the original file name is concatenated:

    $diretorio = 'uploads';
    $destino   = $diretorio.'/'.$origem;

    It then checks whether the directory where the files will be written actually exists; if it does not exist, create the directory using the mkdir command:

     if(!file_exists($diretorio))
        {
            mkdir($diretorio);
        }

    In the final code snippet, the native PHP function move_uploaded_file is used, informing in the first parameter the temporary path of the file and in the second its destination, if the upload is successful, a download link / visualization of the file is displayed (as we are not forcing the download, who will set whether the file will be displayed or downloaded will be the browser)

     if (move_uploaded_file($_FILES['arquivo']['tmp_name'], $destino))
        {
            echo "<a href='{$destino}'> Baixar Arquivo / Visualizar Arquivo </a>";
        } else {
            echo "Possível ataque de upload de arquivo!\n";
        }

    If you want to know all the properties of the file sent through the form, inside the if ($ _ POST) ... add the following code:

    <?php
    
    if($_POST)
    {
    
        echo "<pre>";
        var_dump($_FILES);
        echo "<pre>";
     
    ... restante do código

    The output will be something like:

    rray(1) {
      ["arquivo"]=>
      array(5) {
        ["name"]=>
        string(8) "xxxx.csv"
        ["type"]=>
        string(24) "application/vnd.ms-excel"
        ["tmp_name"]=>
        string(24) "C:\xampp\tmp\phpD4C8.tmp"
        ["error"]=>
        int(0)
        ["size"]=>
        int(30)
      }
    }
        
    16.10.2018 / 03:36