am_dot_com

ACA 20210105

Jan 5th, 2021 (edited)
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.18 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4.     <meta charset="UTF-8">
  5.     <title>Diferente exemplos de submit (comunicação de dados de um cliente para um servidor)</title>
  6. </head>
  7. <body>
  8.     <h1>Comunicação por GET</h1>
  9.     <form
  10.         method="get"
  11.         enctype="application/x-www-form-urlencoded"
  12.         action="server.php"
  13.     >
  14.         <label for="idName">Nome:</label>
  15.         <input type="text" id="idName" name="name" placeholder="seu nome aqui" value="Artur">
  16.         <br>
  17.         <label for="idNumero">Número:</label>
  18.         <input type="text" id="idNumero" name="number" placeholder="seu número aqui" value="12345678">
  19.         <br>
  20.         <input type="submit" value="enviar ao servidor">
  21.     </form>
  22.  
  23.     <hr>
  24.  
  25.     <h1>Comunicação por POST</h1>
  26.     <form
  27.             method="post"
  28.             enctype="application/x-www-form-urlencoded"
  29.             action="server.php"
  30.     >
  31.         <label for="idName2">Nome:</label>
  32.         <input type="text" id="idName2" name="name" placeholder="seu nome aqui" value="Artur">
  33.         <br>
  34.         <label for="idNumero2">Número:</label>
  35.         <input type="text" id="idNumero2" name="number" placeholder="seu número aqui" value="12345678">
  36.         <br>
  37.         <input type="submit" value="enviar ao servidor">
  38.     </form>
  39.  
  40.     <hr>
  41.     <h1>Comunicação por POST com binários (form-url-encoded NÃO funciona)</h1>
  42.     <form
  43.             method="post"
  44.             enctype="multipart/form-data"
  45.             action="server.php"
  46.     >
  47.         <label for="idName3">Nome:</label>
  48.         <input type="text" id="idName3" name="name" placeholder="seu nome aqui" value="Artur">
  49.         <br>
  50.         <label for="idNumero3">Número:</label>
  51.         <input type="text" id="idNumero3" name="number" placeholder="seu número aqui" value="12345678">
  52.         <br>
  53.         <label for="idFoto">Sua foto:</label>
  54.         <input type="file" id="idFoto" name="foto"><br>
  55.  
  56.         <input type="submit" value="enviar ao servidor">
  57.     </form>
  58. </body>
  59. </html>
  60.  
  61. //
  62.  
  63. <?php
  64.  
  65. var_dump ($_FILES);
  66.  
  67. //var_dump ($_SERVER);
  68. function momentoCGI(){
  69.     echo "<h1>Ambiente CGI</h1>".PHP_EOL;
  70.  
  71.     $ul = "<ul>";
  72.     foreach ($_SERVER as $chave=>$valor){
  73.         $li = "<li><mark>$chave</mark>".PHP_EOL;
  74.         //.= concatenação cumulativa
  75.         $li.=": $valor</li>";
  76.  
  77.         $ul.=$li;
  78.     }//foreach
  79.     $ul."</ul>";
  80.  
  81.     echo $ul;
  82.  
  83.     /*
  84.      * estamos a acompanhar esta escrita com Git
  85.      * queremos introduzir git branch
  86.      * criar "ramos" / "contextos de dev"
  87.      * git branch <nomeDoRamo>
  88.      * por exemplo, fizémos
  89.      * git branch get
  90.      * mas depois não gostamos do nome "get"
  91.      * e quisemos mudá-lo para "branchGet"
  92.      * git branch -m get branchGet
  93.      *
  94.      * para passar a trabalhar no context do novo
  95.      * ramos
  96.      *
  97.      * git checkout <nomeDoRamo>
  98.      * no nosso caso
  99.      * git checkout branchGet
  100.      *
  101.      * e estou no branch
  102.      */
  103. }
  104.  
  105. //momento1();
  106.  
  107. /*
  108.  * exemplo de receber por $_GET
  109.  */
  110. function momentoGet(){
  111.     //receção por GET
  112.     echo "<h2>Recebido por GET:</h2>";
  113.     foreach (
  114.         $_GET as
  115.         $nameUsadoNoHtml => $valorFornecido
  116.     ){
  117.         echo "$nameUsadoNoHtml : $valorFornecido<br>";
  118.     }//foreach
  119. }//momentoGet
  120.  
  121. function momentoPost(){
  122.     //receção por GET
  123.     echo "<h2>Recebido por POST:</h2>";
  124.     foreach (
  125.         $_POST as
  126.         $nameUsadoNoHtml => $valorFornecido
  127.     ){
  128.         echo "$nameUsadoNoHtml : $valorFornecido<br>";
  129.     }//foreach
  130. }//momentoPost
  131.  
  132. function momentoPostCom1Binario(){
  133.     //receção por POST
  134.     echo "<h2>Recebido por POST com suporte a 1 binário:</h2>";
  135.     foreach (
  136.         $_POST as
  137.         $nameUsadoNoHtml => $valorFornecido
  138.     ){
  139.         echo "$nameUsadoNoHtml : $valorFornecido<br>";
  140.     }//foreach
  141.  
  142.     foreach($_FILES as $nameUsadoNoHtml => $valorFornecido){
  143.         receiveSingleFile($nameUsadoNoHtml);
  144.     }
  145.  
  146. }//momentoPostCom1Binario
  147.  
  148. function bThereAreMultipleBinaries(
  149.     $pHtmlFileElementName
  150. ){
  151.     $b = is_array($_FILES[$pHtmlFileElementName]['name']) &&
  152.         count($_FILES[$pHtmlFileElementName]['name'])>0;
  153.  
  154.     return $b;
  155. }//bThereAreMultipleBinaries
  156.  
  157. /*
  158.  * 'name' => string 'amemail_v7.pptx' (length=15)
  159.       'type' => string 'application/vnd.openxmlformats-officedocument.presentationml.presentation' (length=73)
  160.       'tmp_name' => string 'H:\PHP.TEMP\php1872.tmp' (length=23)
  161.       'error' => int 0
  162.       'size' => int 433639
  163.  */
  164. function receiveSingleFile(
  165.     $pHtmlFileElementName
  166. ){
  167.     $strName = $_FILES[$pHtmlFileElementName]['name'];
  168.     $strTmp = $_FILES[$pHtmlFileElementName]['tmp_name'];
  169.     $strType = $_FILES[$pHtmlFileElementName]['type'];
  170.     $e = $_FILES[$pHtmlFileElementName]['error'];
  171.     $size = $_FILES[$pHtmlFileElementName]['size'];
  172.  
  173.     if ($e===0){
  174.         $bFalseOnFailure =
  175.             move_uploaded_file(
  176.                 $strTmp,
  177.                 $strName
  178.             );
  179.  
  180.         if ($bFalseOnFailure!==false){
  181.             echo "Recebi o ficheiro ".$strName."<BR>";
  182.         }
  183.         return $bFalseOnFailure;
  184.     }
  185.     return false;
  186. }
  187.  
  188.  
  189. momentoCGI();
  190. momentoGet();
  191. momentoPost();
  192. momentoPostCom1Binario();
  193.  
  194.  
Add Comment
Please, Sign In to add comment