SlideShare a Scribd company logo
1 of 26
Download to read offline
!


    quot;#                                                                                                                                                                                  $
                                                                               %        &       &                       '                        (              %               &
                                                           )                                %   &                                               '*                                  )   +
                                      #
     #,       &                                            '                                                                                            %               -               +        %
                                                   .                                            /                                                                                           /0
              / 0#
                0
    1# 2                                  .                        3                                                                                        &           &           '

                                          #4           )%                                                   $                           3
                                  .        5                                       #4
                                                               '           /                            6                   #               #   0                               3
                                  '                                                                         3                                       #


                              %                                                                                                 .   !
         7                                '                                             '
         74                   -                                    '       .                                        '                               '               .
         78           3                                '
         7            $
         79       .                       '                                                                                                                                         &
                          $                                        )'                                           #
                                                                                                    '                                   3                                       &       &
    %+                                            %&                                    &                   &       &           '                #4
                                                                           &        &                                                                                       '           .
                                          #




:             2           '*                  2            ;           <                                            <
quot;<                     4                  4)          ###############################################     =
                  )       ###################################################################################
             > > > #######################################################################################        ?
    @                                         ################################################################    A
                        ####################################################################################     quot;
    B     '   ###############################################################################################    quot;1
                          ###################################################################################    quot;=
            C3        D
                      '      D###############################################################################    quot;?
    @                                     -         ##########################################################   quot;E
    @           -         ####################################################################################     quot;
          <@                          &              + #####################################################     1quot;
        1 <F        G@ H###################################################################################      1=
        I <2                      4 4 ######################################################################     I?




                                    Última atualização em 03/07/2006




:        2     '*        2      ;      <                           <                                              1
'-   )   &
    +            #




:       2   '*   2   ;   <        <       I
'                                                            '                       &            &                                         $
                -                                                                '           .                                %                         '3               #
8                                    J                                               &                                                          #

        $arquivo = quot;teste.txtquot;;
        $abertura = fopen($arquivo, quot;wquot;);


4                                                        !


            !F                                       #
                !H                                               #F              &               )+
                                                                                                 -                %       -
            &               #
            !F                                               #F              &               )+
                                                                                             -                %           -                                  K
                        #
                    !L                           H                   #F          &               )+
                                                                                                 -                    %       -                                      K
                      #
            !F                                               #                   &                +               -                         '                &           #
                !L                                               #                   &                +                   -                     '                &           #




        '                                                                                         '                                 &       /
                                             0                               /               K                &                   08
                                                                                                                                  #                     K
                                         #

        $conteudo = quot;Isto é um testequot;;
        $gravacao = fwrite($abertura, $conteudo);




        '                                                                                         '                                &        /
                                             0                           3               &                        M
                    #8                                       K                   &           %&                                         $
    -       #

:                   2           '*       2               ;           <                                    <                                                                  =
3                    &          %           C         $       '            '    $/0
               &                             '                                 %               $&       '               'N    /%
                                                                                                                              0
        )
        -           %                        K                                                  #

            $abertura = fopen($arquivo, quot;rquot;);
            $leitura = fread($abertura,filesize($arquivo));




                                                                                           !
    quot;

        '                                                                     '                     &       /
                             0                                       &        &                         /           +
    +%                               '                                                                          &        0#

            fseek($abertura, 0);




#   $

        '      $                                 &           %       C       ' 3-C %                                          .
                   #O                        -                   $       3                                          ;
        '                &       #

            fclose ($abertura);




:              2    '*       2           ;           <                             <
!                %                                     &       '

(

         <?php
                  # teste_arquivo.php
                  $arquivo = quot;teste.txtquot;;
                  $conteudo=quot;Isto é um testequot;;
                  $abertura=fopen(quot;$arquivoquot;,quot;w+quot;);
                  $gravacao = fwrite($abertura, $conteudo);
                  echo quot;Número de caracteres gravados: $gravacaoquot;;
                  # Reposiciona o ponteiro no início do arquivo
                  fseek($abertura, 0);
                  $leitura = fread($abertura, filesize($arquivo));
                  fclose ($abertura);
                  echo quot;<br> Conteúdo do arquivo: $leituraquot;;
         ?>




(        )

9                    3           +                 %
                    )    #                        &    '                    &
    +%        $                      P&       Q                    /   -   =quot;0#




:             2     '*       2       ;    <                    <                  ?
<?php
# arquivo.class.php

class arquivo
{
      # Atributos
      protected $abertura;
      protected $gravacao;
      protected $leitura;

     # Métodos
     # Abertura do arquivo, recebe como parametros o nome do arquivo
     # e o tipo de abertura
     function abreArq ($nome_arq, $tipo_abertura)
     {
           $this->abertura = fopen(quot;$nome_arqquot;, quot;$tipo_aberturaquot;);
     }

     # Gravação do arquivo, recebe como parâmetros o identificador do arquivo
     # e o conteúdo a ser gravado
     function gravaArq ($conteudo)
     {
           $this->gravacao = fwrite($this->abertura, $conteudo);
     }

     # Leitura do arquivo, a função fread recebe como parâmetros
     # o identificador do arquivo e o tamanho do mesmo
     function leArq ()
     {
           $this->leitura = fread($this->abertura, $this->gravacao);
     }

     # Fechamento do arquivo, a função fclose utiliza como parâmetro
     # o identificador do arquivo
     function fechaArq ()
     {
           fclose ($this->abertura);
     }

     # Exibe o conteúdo do arquivo, utiliza o resultado obtido
     # pelo método de leitura
     function exibeArq ()
     {
           echo quot;<br> Conteúdo do arquivo: quot;. $this->leitura;
     }
}

?>




:        2   '*       2   ;   <                 <                               A
Outras funções úteis para a manipulação de arquivos texto:



2               '             &   /       '0                              &       #


            !

H*              3                                     &                               #
&                         '           3           '       &   #



                              +           &           $               '       .   !



<?php

        $abertura = fopen(quot;teste.txtquot;,quot;rquot;);
        // Testa pelo fim de arquivo (eof)
        while (!feof($abertura)) {
                    // fgets lê uma linha de um ponteiro de arq
                    $buffer = fgets($abertura);
                    echo $buffer . quot;<Br>quot;;
        }
        fclose($abertura);
?>




:           2        '*       2   ;           <                   <                       E
1




(                    &                  *+ *,

                                            -

                     &           G@ H           %
F   G@ H#




:     2     '*   2       ;   <          <       quot;
'        .                         '                                                                          &           G@ H%                    =
        3                 1+                   .           G@ H%                         &                                                                          '              !

:4@ /:                            4)               @                0!                   +                &                                                %                   +
                         3%                                          G@ H                                         #2                          $                            ;           #

F G/F                         B'    G@ H0 +
                                         !                                                               G@ H                     #       $                                ;
&    :4@ %                         %' &                                                                                   ;                                +#


F           G@ H! '                                                                          &                G@ H%                               C                    )        %
)
-           3                                      '                                 =#                       %               3C                                               G@ H
              '                        )                                                                         %                                    $
             :4@ %                                                   + #O                                                             $                F               G@ H%
                                   +           .                      '-                                                  3   #



.           /-

G@ H / G                      @R   H                                             0                                                                    &
                     '        .    '                                                                       -                  B               #S
        /                       2@ H0 ; &
                                     %                                       %           &                        2@ H                3                                    C
    '           %        G@ H       &                                                                                                     ;
                                                                                     #               &                +                               &                        &
    +                              .       %                                             .
    '           .                                          &             #
:           '            %                                                                                                                                     '
                     #




-                              00
                                                                                                     %
  &             G@ H          '                    8FF                                           $            +                   F           G@ H
  &         #
8FF                 '                                                                        '            .                   B                &
    -                                                                        -               '                                                %                    &
                                                                     #                           &                                              G@ H
            3                              P'                  Q#
                                                                                 8FF%                     -                                                            '
        )                                                                                                                                                              %

:                2           '*        2               ;        <                                                 <                                                                    quot;quot;
'                 3                                                                     C        #
4   &            8FF       $                            )                                        '        %                             !        %
                   #
                                                            G@ H%                                                     #:
                                                        #
O                                      -                            &                   $             '           .
                  !,                                %       &                   %                                  %       #:
                                               -                        #                                                                                %
         -                                                                          %                             %                              #
         %                                         -                        &                   8FF                                         !




(    )

9                                  ;            '           %       &               G@ H /                    8FF0%         $
@ MF, H# B                     %                                                                                       %
'      -#                              %                                                                                            &            G@ H
   '       8FF#                    '           $%                                                     '                +                          #
O       +     %            $                                                                                  4                     4)            %
                           #



*       12                                                  3           45                                        3             6           47

                       !




:            2    '*       2               ;        <                                       <                                                        quot;
*       82
                    +                       %&   '           $
        !
                !       $                            #
                    !       $                            &   8FF#

            %                   '           !

    9           9$




:               2       '*      2   ;   <        <                  quot;1
<?php
# db.class.php

class db
{
      public $conexao;

         function __construct($dominio, $usuario, $senha, $db)
         {
               $this->conexao = mysql_connect($dominio, $usuario, $senha);
               mysql_select_db($db, $this->conexao);
         }

         function DBError()
         {
               echo mysql_error($this->conexao);
         }

         function insert($tabela, $campos)
         {
               $declar = quot;INSERT into $tabela values $camposquot;;
               return mysql_query ($declar);
         }

         function select($tabela, $campos, $condicao)
         {
               $declar = quot;SELECT $campos from $tabela $condicaoquot;;
               return mysql_query ($declar);
         }

         function delete($tabela, $condicao)
         {
               $declar = quot;DELETE from $tabela $condicaoquot;;
               return mysql_query ($declar);
         }

         function update($tabela, $campos, $condicao)
         {
               $declar = quot;UPDATE $tabela SET $campos WHERE $condicaoquot;;
               return mysql_query ($declar);
         }
}

?>

                                            &     '-        +
                 &                 '            #2                   $       &
'$                    %       %+                                 #



     9          9$




:           2    '*       2   ;    <               <                             quot;I
<?php

class feed
{
      public $obj_feed;

         function __construct($nome_feed)
         {
               $this->obj_feed = simplexml_load_file($nome_feed);
         }

         function exibeFeed()
         {
               foreach ( $this->obj_feed->channel->item as $noticia )
               {
                     echo '<br>';
                     echo $noticia->title . '<br>';
                     echo quot;<a href=quot;$noticia->linkquot;>$noticia->link</a>quot;.'<br>';
                     echo $noticia->description . '<br>';
               }
         }
}

?>

                                                                  simplexml_load_file% &
O                                       '   $             '                                *
          K                 &    G@ H               )#
                                                foreach% &
O                               $                                 '
                       !
                                                                      M # 4 foreach
'-                3               $     '
                    $              D    D
                                        %
            $noticia0
     -     /                                      $                                    #




*        :#             ;                                     5   6        9$     7




:             2   '*        2   ;   <                 <                                    quot;=
<html>
<head>
<title>Formulário</title>
<meta http-equiv=quot;Content-Typequot; content=quot;text/html; charset=iso-8859-1quot;>
</head>
<body>
<p><font size=quot;2quot; face=quot;Verdana, Arial, Helvetica, sans-serifquot;><strong>Inserir
Notícias </strong></font></p>
<form name=quot;form1quot; method=quot;postquot; action=quot;inclui_noticia.phpquot;>
 <table width=quot;50%quot; border=quot;0quot; cellspacing=quot;10quot; cellpadding=quot;0quot;>
 <tr>
 <td width=quot;20%quot;><strong><font size=quot;2quot; face=quot;Verdana, Arial, Helvetica, sans-
serifquot;>Título:</font></strong></td>
 <td width=quot;80%quot;><input name=quot;tituloquot; type=quot;textquot; id=quot;tituloquot; size=quot;50quot;
maxlength=quot;100quot;></td>
 </tr>
 <tr>
 <td width=quot;20%quot;><strong><font size=quot;2quot; face=quot;Verdana, Arial, Helvetica, sans-
serifquot;>Link:</font></strong></td>
 <td width=quot;80%quot;><input name=quot;linkquot; type=quot;textquot; id=quot;linkquot; size=quot;50quot;
maxlength=quot;100quot;></td>
 </tr>
 <tr>
 <td width=quot;20%quot;><strong><font size=quot;2quot; face=quot;Verdana, Arial, Helvetica, sans-
serifquot;>Descrição:</font></strong></td>
 <td width=quot;80%quot;><textarea name=quot;descricaoquot; cols=quot;50quot; rows=quot;4quot;
id=quot;descricaoquot;></textarea></td>
 </tr>
 </table>
 <p>
 <input type=quot;submitquot; name=quot;Submitquot; value=quot;Enviarquot;>
 </p>
 </form>
</body>
</html>




:        2   '*    2    ;    <                  <                                quot;
*       <.                       6        9$        ;
               ;                           =        >                    2 ?9
<?php
        # inclui_noticia.php
        include_once ('classes/db.class.php');

        // Recebe dados do formulário
        $titulo = $_POST[quot;tituloquot;];
        $link = $_POST[quot;linkquot;];
        $descricao = $_POST[quot;descricaoquot;];

        // obtem data de publicação
        $data_publ = date(quot;Yquot;).'-'.date(quot;mquot;).'-'.date(quot;dquot;);

        // Instancia um objeto da classe db
        $inc_not = new db('localhost', 'root', 'unicamp', 'topico_xml');
        $inc_not->DBError();
        $campos = quot;(' ', '$titulo', '$link', '$descricao', '$data_publ')quot;;
        $result = $inc_not->insert('noticia', $campos);

        If ($result) {
              echo quot;Inclusao OK <br><br> <a href=quot;form_noticia.htmlquot;>Voltar</a>quot;;
        }
        else {
              echo quot;Erro na Inclusaoquot;;
        }
?>




*       ,0                   !                  -       5#       7
                    5!       6       9 $ 79 @                        >          32 ? 4
    3          49




:          2    '*       2       ;    <                      <                      quot;?
<?php

# gera_feed.php

function __autoload($classe)
{
       include_once (quot;classes/$classe.class.phpquot;);
}

// Obtem data atual
$data_atual = date(quot;Yquot;).'-'.date(quot;mquot;).'-'.date(quot;dquot;);

// Instancia um objeto da classe db
$sel_not = new db('localhost', 'root', 'unicamp', 'topico_xml');
$sel_not->DBError();
$condicao = quot;where data_publ = '$data_atual'quot;;
$result = $sel_not->select('noticia', '*', $condicao);

// Verifica se encontrou algum registro
$row = mysql_num_rows($result);
if ($row > 0)
{
       // Determina o nome do arquivo XML que será criado
       $arquivo = quot;feed.xmlquot;;

        // Instancia um objeto da classe arquivo e abre o arquivo
        $arquivo_xml = new arquivo();
        $arquivo_xml->abreArq($arquivo,'w');

        $conteudo        = quot;<?xml version='1.0' encoding='ISO-8859-1'?>quot;;
        $conteudo        .= quot;<rss version='2.0'>quot;;
        $conteudo        .= '<channel>';
        $conteudo        .= '<title>Seu Site</title>';
        $conteudo        .= '<link>http://www.seusite.com.br</link>';
        $conteudo        .= '<description>Descrição de seu site</description>';
        $conteudo        .= '<language>pt-br</language>';
        $conteudo        .= '<copyright>Copyright de seu site</copyright>';
        $conteudo        .= '<webmaster>webmaster@seusite.com.br</webmaster>';

        while ($result2 = mysql_fetch_assoc($result)) {

                    // Monta as tags referentes as noticias
                    $conteudo .= quot;<item>quot;;
                    $conteudo .= quot;<title>$result2[titulo]</title>quot;;
                    $conteudo .= quot;<link>$result2[link]</link>quot;;
                    $conteudo .= quot;<description>$result2[descricao]</description>quot;;
                    $conteudo .= quot;</item>quot;;

        }

        //Fecha as tags channel e rss
        $conteudo .= '</channel>';
        quot;        #%
                  $       %
                          &

        quot;'                  (       ) 'quot;       &
        quot;'                          )' &

            *       (
                +
                ,    '              +quot; '
                                     #     #
                                           +       (              -&
                                                                  +
.

/



:               2    '*         2      ;   <                  <                      quot;A
.                 !            6        9$              ;!                                3           9       4                         009
.                '                                                      ;                                 A   !            !
<?xml version=quot;1.0quot; encoding=quot;ISO-8859-1quot;?>
<rss version=quot;2.0quot;>
<channel>
<title>Seu Site</title>
<link>http://www.seusite.com.br</link>
<description>Descrição de seu site</description>
<language>pt-br</language>
<copyright>Copyright de seu site</copyright>
<webmaster>webmaster@seusite.com.br</webmaster>
<item>
<title>Noticia 1</title>
<link>URL da Notícia 1</link>
<description>Descrição da Notícia 1 </description>
</item>
<item>
<title>Noticia 2</title>
<link> URL da Notícia 2</link>
<description> Descrição da Notícia 2 </description>
</item>

<item>
<title>Noticia n</title>
<link> URL da Notícia n </link>
<description> Descrição da Notícia n </description>
</item>
</channel>
</rss>



*            BC                    )        D                               5         6               9$ 7
                                                            '       9        >                          9
8                           -           =#

.    !           ;                                                                6                   9$ %
    J                          &                                '                             %               $
    J                      '            T34   B                                  #
             '           !$        EE    9 $9   9                                9E       $E                      E       F G 19

4                                       +                   F           G@ H
4F                   G@ H                                           $                                             K  %              '
                                            %           $                                         G@ H%           '                %&
                                                '                           '                                     )#

                 %             +            &                   $                 '           .   #                            %
         '                                   K                              &                                                  %
    +                 '                             #                                                                 '
                                                                                                                                   !



:                    2      '*          2           ;       <                             <                                               quot;E
<?php

         $string = file(quot;feed.xmlquot;);
         $string = implode(quot; quot;, $string);
         $xml = simplexml_load_string($string);

         /* Procurando pelo título da primeira notícia */
         $result = $xml->xpath('/rss/channel/item/title');

         echo $result[0];

?>

O    ;                                                            M                       -       U         %
                                 /    0              &        #                   +                   -     %
     3                               M&          &                    +       #
4 !2                                                     &        8FF
8FF                    %   ;                                       '                  )       !

T               !      3 !V N N N #'
                           V                         #       V/W            N0H
23                  ! 3 !V N N N #
                          V          $       #       V3                   V/W NVH                     +0#




:           2        '*     2    ;       <                                <
4




    H                                                             ..*

            '                 $                   .   W               )
;                +       '-                               %       +           '       %
                                       . #F   -               '           -
                                      %&          -
    +       #




:       2       '*   2   ;        <                   <                               quot;
19 I

                                          %                                 ;                                                          W
                $         %                                     '                                %                        ;             2@ H
            .                 '               &       &         )       #4                                       -                    3&
                                                                                                         %                        3
            .       F, H                                                                             &               -                             %
                                                      #         $                                                                 4                5
4)                                                )                                 $                                         %
            $                                                       3                                                         ;           #O
                     $                                +                 -       &                -           $                                 )
T       #            +                                                                                                                     -       %
                                  ;                                     / 2@ H%         FF       X                   0%       ;
                      #



quot;#quot;< 4                                                $

            O                     3       !VV             3    V3 V                          %                   C        -           &            -
    $                                      )          T        %&               -                                                              #
            $                                                     C                 T            quot;#quot;# :
A             %      &                =*                          K                      $                                                 %&
    -       @ MF, H#




:               2     '*              2       ;           <                              <
#!         19 C 2
                        1                                      *       %   #           9



    quot;# D           $ !: '          .          -                    #
     #           !O                 &                  CC                                  #
    1# @     )          (                !O                                        C
           '         5                             #
    I#   @)        *                !O                             C                           %
               3            #
    =#   @)            2    !       O                              C                           &
                         3         #
     #   @)            J             +        !O                               C
         K       3   #
    ?#           /! '           .                          -
                           -             #



:   2      '*     2      ;     <                       <                                           1
19 8 C D A
  19                 !

     ,                                                 -                                                     -
'                        #                                                  3                               '
         -           #       T        quot;#               '                                                         !



                                                                    +# 3


                                                                    #           #3

                                                                        #        #3

                                                   O            #               #3



     @                   9$                                                                 O           %&                -       $
                                                                                        '           .                -                    #

                             9        9$                                                                -
                                        -      #2          &                -
         -                            C                     #

                                 9$                                                             -
         '               .        &                    $                                                             @ MF, H# ,       &
                                                                                                    )
                 #

                 9$                                                             $                                     )       &
             '                                                  %           '                                                         -
                         #




:            2           '*       2        ;   <                                    <                                                         I
)             A   8
L                               &       +    $              P   &       Q#

<?php
        # teste_arquivo.php
        # objetivo: gravar conteúdo num arquivo texto e em seguida exibir esse
        conteúdo

        // Inclui o script onde foi definida a classe a ser utilizada
        include_once ('classes/arquivo.class.php');

        # Inicializa variáveis que serão passadas como parâmetros
        $arquivo = quot;teste.txtquot;;
        $conteudo = quot;Isto é um testequot;;

        # Instancia a classe
        $arq = new arquivo();

        # Chama o método de abertura de arquivo (escrita)
        $arq->abreArq($arquivo,'w');

        # Chama o método de gravação de arquivo
        $arq->gravaArq($conteudo);

        # Chama o método de abertura de arquivo (leitura)
        $arq->abreArq($arquivo,'r');

        # Chama o método de leitura de arquivo
        $arq->leArq();

        # Chama o método de fechamento de arquivo
        $arq->fechaArq();

        # Chama o método que exibe o conteúdo do arquivo
        $arq->exibeArq();

?>




:          2   '*     2    ;        <               <                            =
)       A    :

         C            +   Y       #3
<?php

        # exibe_noticia.php

        include_once ('classes/feed.class.php');

        $noticia = new feed('feed.xml');
        $noticia->ExibeFeed();

?>




:            2   '*       2   ;   <                <

More Related Content

What's hot

Ly thuyet thi truong von
Ly thuyet thi truong vonLy thuyet thi truong von
Ly thuyet thi truong voncucaispc
 
Protocolo Adicional A La ConvencióN Americana Sobre Derechos Humanos En Mater...
Protocolo Adicional A La ConvencióN Americana Sobre Derechos Humanos En Mater...Protocolo Adicional A La ConvencióN Americana Sobre Derechos Humanos En Mater...
Protocolo Adicional A La ConvencióN Americana Sobre Derechos Humanos En Mater...Jesús David Fernández Vivenes
 
Garis garis besar program pengajaran
Garis garis besar program pengajaranGaris garis besar program pengajaran
Garis garis besar program pengajaranRatzman III
 
Introducción a la administración f08
Introducción a la administración f08Introducción a la administración f08
Introducción a la administración f08Skepper63
 
Basics of Profiled SMS Marketing with SPADE
Basics of Profiled SMS Marketing with SPADEBasics of Profiled SMS Marketing with SPADE
Basics of Profiled SMS Marketing with SPADEsvartikar
 
Syllabus electronics & telecommunication
Syllabus electronics & telecommunicationSyllabus electronics & telecommunication
Syllabus electronics & telecommunicationGK LINK ZONE
 
Presentacion GPC trastornos del sueño
Presentacion GPC trastornos del sueñoPresentacion GPC trastornos del sueño
Presentacion GPC trastornos del sueñoCristobal Buñuel
 
Cd jun d_fut7[1]
Cd jun d_fut7[1]Cd jun d_fut7[1]
Cd jun d_fut7[1]mm23bb
 
Se aprobó en primer debate proyecto de ley que permite la legalización del de...
Se aprobó en primer debate proyecto de ley que permite la legalización del de...Se aprobó en primer debate proyecto de ley que permite la legalización del de...
Se aprobó en primer debate proyecto de ley que permite la legalización del de...Comisión Colombiana de Juristas
 

What's hot (17)

Ly thuyet thi truong von
Ly thuyet thi truong vonLy thuyet thi truong von
Ly thuyet thi truong von
 
Protocolo Adicional A La ConvencióN Americana Sobre Derechos Humanos En Mater...
Protocolo Adicional A La ConvencióN Americana Sobre Derechos Humanos En Mater...Protocolo Adicional A La ConvencióN Americana Sobre Derechos Humanos En Mater...
Protocolo Adicional A La ConvencióN Americana Sobre Derechos Humanos En Mater...
 
Bases mou te tardor (3)
Bases mou te tardor (3)Bases mou te tardor (3)
Bases mou te tardor (3)
 
361o 1 semana_7
361o 1 semana_7361o 1 semana_7
361o 1 semana_7
 
Garis garis besar program pengajaran
Garis garis besar program pengajaranGaris garis besar program pengajaran
Garis garis besar program pengajaran
 
Lembaga Keuangan Mikro
Lembaga Keuangan MikroLembaga Keuangan Mikro
Lembaga Keuangan Mikro
 
Bem vindo ao mes de ramadan
Bem vindo ao mes de ramadanBem vindo ao mes de ramadan
Bem vindo ao mes de ramadan
 
Introducción a la administración f08
Introducción a la administración f08Introducción a la administración f08
Introducción a la administración f08
 
Basics of Profiled SMS Marketing with SPADE
Basics of Profiled SMS Marketing with SPADEBasics of Profiled SMS Marketing with SPADE
Basics of Profiled SMS Marketing with SPADE
 
Syllabus electronics & telecommunication
Syllabus electronics & telecommunicationSyllabus electronics & telecommunication
Syllabus electronics & telecommunication
 
Juegos+para+ni
Juegos+para+niJuegos+para+ni
Juegos+para+ni
 
Presentacion GPC trastornos del sueño
Presentacion GPC trastornos del sueñoPresentacion GPC trastornos del sueño
Presentacion GPC trastornos del sueño
 
Taula de aposits
Taula de apositsTaula de aposits
Taula de aposits
 
Cd jun d_fut7[1]
Cd jun d_fut7[1]Cd jun d_fut7[1]
Cd jun d_fut7[1]
 
Apre 1 t09
Apre 1 t09Apre 1 t09
Apre 1 t09
 
Ordo missae bracarense
Ordo missae bracarenseOrdo missae bracarense
Ordo missae bracarense
 
Se aprobó en primer debate proyecto de ley que permite la legalización del de...
Se aprobó en primer debate proyecto de ley que permite la legalización del de...Se aprobó en primer debate proyecto de ley que permite la legalización del de...
Se aprobó en primer debate proyecto de ley que permite la legalización del de...
 

Apostila Php Avancado

  • 1.
  • 2. ! quot;# $ % & & ' ( % & ) % & '* ) + # #, & ' % - + % . / /0 / 0# 0 1# 2 . 3 & & ' #4 )% $ 3 . 5 #4 ' / 6 # # 0 3 ' 3 # % . ! 7 ' ' 74 - ' . ' ' . 78 3 ' 7 $ 79 . ' & $ )' # ' 3 & & %+ %& & & & ' #4 & & ' . # : 2 '* 2 ; < <
  • 3. quot;< 4 4) ############################################### = ) ################################################################################### > > > ####################################################################################### ? @ ################################################################ A #################################################################################### quot; B ' ############################################################################################### quot;1 ################################################################################### quot;= C3 D ' D############################################################################### quot;? @ - ########################################################## quot;E @ - #################################################################################### quot; <@ & + ##################################################### 1quot; 1 <F G@ H################################################################################### 1= I <2 4 4 ###################################################################### I? Última atualização em 03/07/2006 : 2 '* 2 ; < < 1
  • 4. '- ) & + # : 2 '* 2 ; < < I
  • 5. ' ' & & $ - ' . % '3 # 8 J & # $arquivo = quot;teste.txtquot;; $abertura = fopen($arquivo, quot;wquot;); 4 ! !F # !H #F & )+ - % - & # !F #F & )+ - % - K # !L H #F & )+ - % - K # !F # & + - ' & # !L # & + - ' & # ' ' & / 0 / K & 08 # K # $conteudo = quot;Isto é um testequot;; $gravacao = fwrite($abertura, $conteudo); ' ' & / 0 3 & M #8 K & %& $ - # : 2 '* 2 ; < < =
  • 6. 3 & % C $ ' ' $/0 & ' % $& ' 'N /% 0 ) - % K # $abertura = fopen($arquivo, quot;rquot;); $leitura = fread($abertura,filesize($arquivo)); ! quot; ' ' & / 0 & & / + +% ' & 0# fseek($abertura, 0); # $ ' $ & % C ' 3-C % . #O - $ 3 ; ' & # fclose ($abertura); : 2 '* 2 ; < <
  • 7. ! % & ' ( <?php # teste_arquivo.php $arquivo = quot;teste.txtquot;; $conteudo=quot;Isto é um testequot;; $abertura=fopen(quot;$arquivoquot;,quot;w+quot;); $gravacao = fwrite($abertura, $conteudo); echo quot;Número de caracteres gravados: $gravacaoquot;; # Reposiciona o ponteiro no início do arquivo fseek($abertura, 0); $leitura = fread($abertura, filesize($arquivo)); fclose ($abertura); echo quot;<br> Conteúdo do arquivo: $leituraquot;; ?> ( ) 9 3 + % ) # & ' & +% $ P& Q / - =quot;0# : 2 '* 2 ; < < ?
  • 8. <?php # arquivo.class.php class arquivo { # Atributos protected $abertura; protected $gravacao; protected $leitura; # Métodos # Abertura do arquivo, recebe como parametros o nome do arquivo # e o tipo de abertura function abreArq ($nome_arq, $tipo_abertura) { $this->abertura = fopen(quot;$nome_arqquot;, quot;$tipo_aberturaquot;); } # Gravação do arquivo, recebe como parâmetros o identificador do arquivo # e o conteúdo a ser gravado function gravaArq ($conteudo) { $this->gravacao = fwrite($this->abertura, $conteudo); } # Leitura do arquivo, a função fread recebe como parâmetros # o identificador do arquivo e o tamanho do mesmo function leArq () { $this->leitura = fread($this->abertura, $this->gravacao); } # Fechamento do arquivo, a função fclose utiliza como parâmetro # o identificador do arquivo function fechaArq () { fclose ($this->abertura); } # Exibe o conteúdo do arquivo, utiliza o resultado obtido # pelo método de leitura function exibeArq () { echo quot;<br> Conteúdo do arquivo: quot;. $this->leitura; } } ?> : 2 '* 2 ; < < A
  • 9. Outras funções úteis para a manipulação de arquivos texto: 2 ' & / '0 & # ! H* 3 & # & ' 3 ' & # + & $ ' . ! <?php $abertura = fopen(quot;teste.txtquot;,quot;rquot;); // Testa pelo fim de arquivo (eof) while (!feof($abertura)) { // fgets lê uma linha de um ponteiro de arq $buffer = fgets($abertura); echo $buffer . quot;<Br>quot;; } fclose($abertura); ?> : 2 '* 2 ; < < E
  • 10. 1 ( & *+ *, - & G@ H % F G@ H# : 2 '* 2 ; < < quot;
  • 11. ' . ' & G@ H% = 3 1+ . G@ H% & ' ! :4@ /: 4) @ 0! + & % + 3% G@ H #2 $ ; # F G/F B' G@ H0 + ! G@ H # $ ; & :4@ % %' & ; +# F G@ H! ' & G@ H% C ) % ) - 3 ' =# % 3C G@ H ' ) % $ :4@ % + #O $ F G@ H% + . '- 3 # . /- G@ H / G @R H 0 & ' . ' - B #S / 2@ H0 ; & % % & 2@ H 3 C ' % G@ H & ; # & + & & + . % . ' . & # : ' % ' # - 00 % & G@ H ' 8FF $ + F G@ H & # 8FF ' ' . B & - - ' % & # & G@ H 3 P' Q# 8FF% - ' ) % : 2 '* 2 ; < < quot;quot;
  • 12. ' 3 C # 4 & 8FF $ ) ' % ! % # G@ H% #: # O - & $ ' . !, % & % % #: - # % - % % # % - & 8FF ! ( ) 9 ; ' % & G@ H / 8FF0% $ @ MF, H# B % % ' -# % & G@ H ' 8FF# ' $% ' + # O + % $ 4 4) % # * 12 3 45 3 6 47 ! : 2 '* 2 ; < < quot;
  • 13. * 82 + %& ' $ ! ! $ # ! $ & 8FF# % ' ! 9 9$ : 2 '* 2 ; < < quot;1
  • 14. <?php # db.class.php class db { public $conexao; function __construct($dominio, $usuario, $senha, $db) { $this->conexao = mysql_connect($dominio, $usuario, $senha); mysql_select_db($db, $this->conexao); } function DBError() { echo mysql_error($this->conexao); } function insert($tabela, $campos) { $declar = quot;INSERT into $tabela values $camposquot;; return mysql_query ($declar); } function select($tabela, $campos, $condicao) { $declar = quot;SELECT $campos from $tabela $condicaoquot;; return mysql_query ($declar); } function delete($tabela, $condicao) { $declar = quot;DELETE from $tabela $condicaoquot;; return mysql_query ($declar); } function update($tabela, $campos, $condicao) { $declar = quot;UPDATE $tabela SET $campos WHERE $condicaoquot;; return mysql_query ($declar); } } ?> & '- + & ' #2 $ & '$ % %+ # 9 9$ : 2 '* 2 ; < < quot;I
  • 15. <?php class feed { public $obj_feed; function __construct($nome_feed) { $this->obj_feed = simplexml_load_file($nome_feed); } function exibeFeed() { foreach ( $this->obj_feed->channel->item as $noticia ) { echo '<br>'; echo $noticia->title . '<br>'; echo quot;<a href=quot;$noticia->linkquot;>$noticia->link</a>quot;.'<br>'; echo $noticia->description . '<br>'; } } } ?> simplexml_load_file% & O ' $ ' * K & G@ H )# foreach% & O $ ' ! M # 4 foreach '- 3 $ ' $ D D % $noticia0 - / $ # * :# ; 5 6 9$ 7 : 2 '* 2 ; < < quot;=
  • 16. <html> <head> <title>Formulário</title> <meta http-equiv=quot;Content-Typequot; content=quot;text/html; charset=iso-8859-1quot;> </head> <body> <p><font size=quot;2quot; face=quot;Verdana, Arial, Helvetica, sans-serifquot;><strong>Inserir Notícias </strong></font></p> <form name=quot;form1quot; method=quot;postquot; action=quot;inclui_noticia.phpquot;> <table width=quot;50%quot; border=quot;0quot; cellspacing=quot;10quot; cellpadding=quot;0quot;> <tr> <td width=quot;20%quot;><strong><font size=quot;2quot; face=quot;Verdana, Arial, Helvetica, sans- serifquot;>Título:</font></strong></td> <td width=quot;80%quot;><input name=quot;tituloquot; type=quot;textquot; id=quot;tituloquot; size=quot;50quot; maxlength=quot;100quot;></td> </tr> <tr> <td width=quot;20%quot;><strong><font size=quot;2quot; face=quot;Verdana, Arial, Helvetica, sans- serifquot;>Link:</font></strong></td> <td width=quot;80%quot;><input name=quot;linkquot; type=quot;textquot; id=quot;linkquot; size=quot;50quot; maxlength=quot;100quot;></td> </tr> <tr> <td width=quot;20%quot;><strong><font size=quot;2quot; face=quot;Verdana, Arial, Helvetica, sans- serifquot;>Descrição:</font></strong></td> <td width=quot;80%quot;><textarea name=quot;descricaoquot; cols=quot;50quot; rows=quot;4quot; id=quot;descricaoquot;></textarea></td> </tr> </table> <p> <input type=quot;submitquot; name=quot;Submitquot; value=quot;Enviarquot;> </p> </form> </body> </html> : 2 '* 2 ; < < quot;
  • 17. * <. 6 9$ ; ; = > 2 ?9 <?php # inclui_noticia.php include_once ('classes/db.class.php'); // Recebe dados do formulário $titulo = $_POST[quot;tituloquot;]; $link = $_POST[quot;linkquot;]; $descricao = $_POST[quot;descricaoquot;]; // obtem data de publicação $data_publ = date(quot;Yquot;).'-'.date(quot;mquot;).'-'.date(quot;dquot;); // Instancia um objeto da classe db $inc_not = new db('localhost', 'root', 'unicamp', 'topico_xml'); $inc_not->DBError(); $campos = quot;(' ', '$titulo', '$link', '$descricao', '$data_publ')quot;; $result = $inc_not->insert('noticia', $campos); If ($result) { echo quot;Inclusao OK <br><br> <a href=quot;form_noticia.htmlquot;>Voltar</a>quot;; } else { echo quot;Erro na Inclusaoquot;; } ?> * ,0 ! - 5# 7 5! 6 9 $ 79 @ > 32 ? 4 3 49 : 2 '* 2 ; < < quot;?
  • 18. <?php # gera_feed.php function __autoload($classe) { include_once (quot;classes/$classe.class.phpquot;); } // Obtem data atual $data_atual = date(quot;Yquot;).'-'.date(quot;mquot;).'-'.date(quot;dquot;); // Instancia um objeto da classe db $sel_not = new db('localhost', 'root', 'unicamp', 'topico_xml'); $sel_not->DBError(); $condicao = quot;where data_publ = '$data_atual'quot;; $result = $sel_not->select('noticia', '*', $condicao); // Verifica se encontrou algum registro $row = mysql_num_rows($result); if ($row > 0) { // Determina o nome do arquivo XML que será criado $arquivo = quot;feed.xmlquot;; // Instancia um objeto da classe arquivo e abre o arquivo $arquivo_xml = new arquivo(); $arquivo_xml->abreArq($arquivo,'w'); $conteudo = quot;<?xml version='1.0' encoding='ISO-8859-1'?>quot;; $conteudo .= quot;<rss version='2.0'>quot;; $conteudo .= '<channel>'; $conteudo .= '<title>Seu Site</title>'; $conteudo .= '<link>http://www.seusite.com.br</link>'; $conteudo .= '<description>Descrição de seu site</description>'; $conteudo .= '<language>pt-br</language>'; $conteudo .= '<copyright>Copyright de seu site</copyright>'; $conteudo .= '<webmaster>webmaster@seusite.com.br</webmaster>'; while ($result2 = mysql_fetch_assoc($result)) { // Monta as tags referentes as noticias $conteudo .= quot;<item>quot;; $conteudo .= quot;<title>$result2[titulo]</title>quot;; $conteudo .= quot;<link>$result2[link]</link>quot;; $conteudo .= quot;<description>$result2[descricao]</description>quot;; $conteudo .= quot;</item>quot;; } //Fecha as tags channel e rss $conteudo .= '</channel>'; quot; #% $ % & quot;' ( ) 'quot; & quot;' )' & * ( + , ' +quot; ' # # + ( -& + . / : 2 '* 2 ; < < quot;A
  • 19. . ! 6 9$ ;! 3 9 4 009 . ' ; A ! ! <?xml version=quot;1.0quot; encoding=quot;ISO-8859-1quot;?> <rss version=quot;2.0quot;> <channel> <title>Seu Site</title> <link>http://www.seusite.com.br</link> <description>Descrição de seu site</description> <language>pt-br</language> <copyright>Copyright de seu site</copyright> <webmaster>webmaster@seusite.com.br</webmaster> <item> <title>Noticia 1</title> <link>URL da Notícia 1</link> <description>Descrição da Notícia 1 </description> </item> <item> <title>Noticia 2</title> <link> URL da Notícia 2</link> <description> Descrição da Notícia 2 </description> </item> <item> <title>Noticia n</title> <link> URL da Notícia n </link> <description> Descrição da Notícia n </description> </item> </channel> </rss> * BC ) D 5 6 9$ 7 ' 9 > 9 8 - =# . ! ; 6 9$ % J & ' % $ J ' T34 B # ' !$ EE 9 $9 9 9E $E E F G 19 4 + F G@ H 4F G@ H $ K % ' % $ G@ H% ' %& ' ' )# % + & $ ' . # % ' K & % + ' # ' ! : 2 '* 2 ; < < quot;E
  • 20. <?php $string = file(quot;feed.xmlquot;); $string = implode(quot; quot;, $string); $xml = simplexml_load_string($string); /* Procurando pelo título da primeira notícia */ $result = $xml->xpath('/rss/channel/item/title'); echo $result[0]; ?> O ; M - U % / 0 & # + - % 3 M& & + # 4 !2 & 8FF 8FF % ; ' ) ! T ! 3 !V N N N #' V # V/W N0H 23 ! 3 !V N N N # V $ # V3 V/W NVH +0# : 2 '* 2 ; < <
  • 21. 4 H ..* ' $ . W ) ; + '- % + ' % . #F - ' - %& - + # : 2 '* 2 ; < < quot;
  • 22. 19 I % ; W $ % ' % ; 2@ H . ' & & ) #4 - 3& % 3 . F, H & - % # $ 4 5 4) ) $ % $ 3 ; #O $ + - & - $ ) T # + - % ; / 2@ H% FF X 0% ; # quot;#quot;< 4 $ O 3 !VV 3 V3 V % C - & - $ ) T %& - # $ C T quot;#quot;# : A % & =* K $ %& - @ MF, H# : 2 '* 2 ; < <
  • 23. #! 19 C 2 1 * % # 9 quot;# D $ !: ' . - # # !O & CC # 1# @ ) ( !O C ' 5 # I# @) * !O C % 3 # =# @) 2 ! O C & 3 # # @) J + !O C K 3 # ?# /! ' . - - # : 2 '* 2 ; < < 1
  • 24. 19 8 C D A 19 ! , - - ' # 3 ' - # T quot;# ' ! +# 3 # #3 # #3 O # #3 @ 9$ O %& - $ ' . - # 9 9$ - - #2 & - - C # 9$ - ' . & $ @ MF, H# , & ) # 9$ $ ) & ' % ' - # : 2 '* 2 ; < < I
  • 25. ) A 8 L & + $ P & Q# <?php # teste_arquivo.php # objetivo: gravar conteúdo num arquivo texto e em seguida exibir esse conteúdo // Inclui o script onde foi definida a classe a ser utilizada include_once ('classes/arquivo.class.php'); # Inicializa variáveis que serão passadas como parâmetros $arquivo = quot;teste.txtquot;; $conteudo = quot;Isto é um testequot;; # Instancia a classe $arq = new arquivo(); # Chama o método de abertura de arquivo (escrita) $arq->abreArq($arquivo,'w'); # Chama o método de gravação de arquivo $arq->gravaArq($conteudo); # Chama o método de abertura de arquivo (leitura) $arq->abreArq($arquivo,'r'); # Chama o método de leitura de arquivo $arq->leArq(); # Chama o método de fechamento de arquivo $arq->fechaArq(); # Chama o método que exibe o conteúdo do arquivo $arq->exibeArq(); ?> : 2 '* 2 ; < < =
  • 26. ) A : C + Y #3 <?php # exibe_noticia.php include_once ('classes/feed.class.php'); $noticia = new feed('feed.xml'); $noticia->ExibeFeed(); ?> : 2 '* 2 ; < <