<?xml version="1.0" encoding="utf-8"?><?xml-stylesheet title="XSL formatting" type="text/xsl" href="http://www.code-web.org/feed/rss2/xslt" ?><rss version="2.0"
  xmlns:dc="http://purl.org/dc/elements/1.1/"
  xmlns:wfw="http://wellformedweb.org/CommentAPI/"
  xmlns:content="http://purl.org/rss/1.0/modules/content/">
<channel>
  <title>Code-Web.org - php</title>
  <link>http://www.code-web.org/</link>
  <description>Développement Web en toute liberté!</description>
  <language>fr</language>
  <pubDate>Thu, 21 Aug 2008 18:05:39 +0200</pubDate>
  <copyright></copyright>
  <docs>http://blogs.law.harvard.edu/tech/rss</docs>
  <generator>Dotclear</generator>
  
    
  <item>
    <title>Getter et setter automatiques avec les méthodes magiques sous PHP 5</title>
    <link>http://www.code-web.org/post/2008/05/13/Les-methodes-magic-sous-PHP-5</link>
    <guid isPermaLink="false">urn:md5:e2966caeb7bac628255e8704e274f59e</guid>
    <pubDate>Tue, 13 May 2008 17:17:00 +0200</pubDate>
    <dc:creator>Thierry Geindre</dc:creator>
        <category>PHP</category>
        <category>objet</category><category>php</category>    
    <description>&lt;p&gt;L' objet sous PHP 5 introduit de nouvelles methodes, dites magiques. Elles permettent de s'affranchir de tâches parfois fastidieuses telles que l'écriture de getter et de setter. Nous allons voir comment employer deux de ces méthodes.&lt;/p&gt;    &lt;p&gt;L'un des avantages de l'objet est la possibilité d'en capsuler les données dans un objet et ainsi de pouvoir exercer un certain contrôle sur les accès qui sont effectués. Cependant, lorsque ces données deviennent nombreuses, l'écriture de toutes les méthodes d'accès peut devenir fastidieuse. Prenons un exemple assez simple&amp;nbsp;:&lt;/p&gt;
&lt;pre class=&quot;php&quot;&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;class&lt;/span&gt; foo&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;
&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#123;&lt;/span&gt;
   &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;private&lt;/span&gt; &lt;span style=&quot;color: #ff0000&quot;&gt;$a&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;;&lt;/span&gt;
   &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;private&lt;/span&gt; &lt;span style=&quot;color: #ff0000&quot;&gt;$b&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;;&lt;/span&gt;
   &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;private&lt;/span&gt; &lt;span style=&quot;color: #ff0000&quot;&gt;$c&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;;&lt;/span&gt;
   &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#91;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;...&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#93;&lt;/span&gt;
   &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;private&lt;/span&gt; &lt;span style=&quot;color: #ff0000&quot;&gt;$z&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;Il nous faudrait, en principe, pas moins de 26 getter et 26 setter (soit 52 méthodes) pour pouvoir accéder à tous les attributs de cette classe. Il existe bien des outils qui permettent l'écriture automatisée des ces méthodes, mais on assistera alors à une belle surcharge de code.&lt;/p&gt;


&lt;p&gt;C'est là qu'interviennent les méthodes &lt;strong&gt;__get()&lt;/strong&gt; et &lt;strong&gt;__set()&lt;/strong&gt;.&lt;/p&gt;


&lt;h3&gt;La méthode __set()&lt;/h3&gt;

&lt;p&gt;Cette méthode reçoit deux paramètres, le nom de l'attribut auquel on souhaite accéder et la valeur que l'on souhaite lui donner. Reprenons donc avec notre exemple de classe&amp;nbsp;:&lt;/p&gt;

&lt;pre class=&quot;php&quot;&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;class&lt;/span&gt; foo&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;
&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#123;&lt;/span&gt;
   &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;private&lt;/span&gt; &lt;span style=&quot;color: #ff0000&quot;&gt;$a&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;;&lt;/span&gt;
   &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;private&lt;/span&gt; &lt;span style=&quot;color: #ff0000&quot;&gt;$b&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;;&lt;/span&gt;
   &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;private&lt;/span&gt; &lt;span style=&quot;color: #ff0000&quot;&gt;$c&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;;&lt;/span&gt;
   &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#91;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;...&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#93;&lt;/span&gt;
   &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;private&lt;/span&gt; &lt;span style=&quot;color: #ff0000&quot;&gt;$z&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;;&lt;/span&gt;
&amp;nbsp;
   &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;function&lt;/span&gt; __set&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #ff0000&quot;&gt;$attr&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #ff0000&quot;&gt;$value&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;
   &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#123;&lt;/span&gt;
       &lt;span style=&quot;color: #b1b100;&quot;&gt;if&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000066;&quot;&gt;isset&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #ff0000&quot;&gt;$this&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #ff0000&quot;&gt;$attr&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #ff0000&quot;&gt;$this&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #ff0000&quot;&gt;$attr&lt;/span&gt; &lt;span style=&quot;color: #66cc66;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #ff0000&quot;&gt;$value&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;;&lt;/span&gt;
       &lt;span style=&quot;color: #b1b100;&quot;&gt;else&lt;/span&gt; throw &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;new&lt;/span&gt; Exception&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;'Unknow attribute '&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #ff0000&quot;&gt;$attr&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;;&lt;/span&gt;
   &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#125;&lt;/span&gt;
&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;Si l'attribut demandé n'existe pas, on lance une exception.&lt;/p&gt;


&lt;h3&gt;La méthode __get()&lt;/h3&gt;

&lt;p&gt;Cette méthode ne reçoit qu'un seul paramètre, c'est le nom de l'attribut qui a été appelé. Reprenons donc avec notre exemple de classe&amp;nbsp;:&lt;/p&gt;
&lt;pre class=&quot;php&quot;&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;class&lt;/span&gt; foo&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;
&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#123;&lt;/span&gt;
   &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;private&lt;/span&gt; &lt;span style=&quot;color: #ff0000&quot;&gt;$a&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;;&lt;/span&gt;
   &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;private&lt;/span&gt; &lt;span style=&quot;color: #ff0000&quot;&gt;$b&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;;&lt;/span&gt;
   &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;private&lt;/span&gt; &lt;span style=&quot;color: #ff0000&quot;&gt;$c&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;;&lt;/span&gt;
   &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#91;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;...&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#93;&lt;/span&gt;
   &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;private&lt;/span&gt; &lt;span style=&quot;color: #ff0000&quot;&gt;$z&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;;&lt;/span&gt;
&amp;nbsp;
   &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;function&lt;/span&gt; __get&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #ff0000&quot;&gt;$attr&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;
   &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#123;&lt;/span&gt;
      &lt;span style=&quot;color: #b1b100;&quot;&gt;if&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000066;&quot;&gt;isset&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #ff0000&quot;&gt;$this&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #ff0000&quot;&gt;$attr&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #b1b100;&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: #ff0000&quot;&gt;$this&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #ff0000&quot;&gt;$attr&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;;&lt;/span&gt;
      &lt;span style=&quot;color: #b1b100;&quot;&gt;else&lt;/span&gt; throw &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;new&lt;/span&gt; Exception&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;'Unknow attribute '&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #ff0000&quot;&gt;$attr&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;;&lt;/span&gt;
   &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#125;&lt;/span&gt;
&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;Si l'attribut demandé n'existe pas, on lance une exception.&lt;/p&gt;


&lt;h3&gt;Réinventons &lt;em&gt;stdclass&lt;/em&gt;&lt;/h3&gt;

&lt;p&gt;&lt;em&gt;Stdclass&lt;/em&gt; est une classe définit par php. Elle permet de stocker autant d'attributs que l'on souhaite. Il est donc possible d'écrire le code suivant sans aucun problème&amp;nbsp;:&lt;/p&gt;
&lt;pre class=&quot;php&quot;&gt;&lt;span style=&quot;color: #ff0000&quot;&gt;$a&lt;/span&gt; &lt;span style=&quot;color: #66cc66;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;new&lt;/span&gt; stdclass&lt;span style=&quot;color: #66cc66;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #ff0000&quot;&gt;$a&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #006600;&quot;&gt;foo&lt;/span&gt; &lt;span style=&quot;color: #66cc66;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #cc66cc;&quot;&gt;10&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #000066;&quot;&gt;echo&lt;/span&gt; &lt;span style=&quot;color: #ff0000&quot;&gt;$a&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #006600;&quot;&gt;foo&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;;&lt;/span&gt; &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;// Affiche bien 10&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;Voici donc, à partir de ce que nous venons de voir, de quelle manière nous pourrions implémenter une telle classe. Pour éviter les conflits, nous la nommerons &lt;em&gt;MyStdclass&lt;/em&gt;&amp;nbsp;:&lt;/p&gt;
&lt;pre class=&quot;php&quot;&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;class&lt;/span&gt; MyStdclass
&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#123;&lt;/span&gt;
  &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;private&lt;/span&gt; &lt;span style=&quot;color: #ff0000&quot;&gt;$data&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;;&lt;/span&gt;
&amp;nbsp;
  &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;function&lt;/span&gt; __construct&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;
  &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#123;&lt;/span&gt;
    &lt;span style=&quot;color: #ff0000&quot;&gt;$this&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #006600;&quot;&gt;data&lt;/span&gt; &lt;span style=&quot;color: #66cc66;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #000066;&quot;&gt;array&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;;&lt;/span&gt;
  &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#125;&lt;/span&gt;
&amp;nbsp;
  &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;function&lt;/span&gt; __set&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #ff0000&quot;&gt;$key&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #ff0000&quot;&gt;$value&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;
  &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#123;&lt;/span&gt;
    &lt;span style=&quot;color: #ff0000&quot;&gt;$this&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #006600;&quot;&gt;data&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#91;&lt;/span&gt;&lt;span style=&quot;color: #ff0000&quot;&gt;$key&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#93;&lt;/span&gt; &lt;span style=&quot;color: #66cc66;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #ff0000&quot;&gt;$value&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;;&lt;/span&gt;
  &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#125;&lt;/span&gt;
&amp;nbsp;
  &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;function&lt;/span&gt; __get&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #ff0000&quot;&gt;$key&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;
  &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#123;&lt;/span&gt;
    &lt;span style=&quot;color: #b1b100;&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: #ff0000&quot;&gt;$this&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #006600;&quot;&gt;data&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#91;&lt;/span&gt;&lt;span style=&quot;color: #ff0000&quot;&gt;$key&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;;&lt;/span&gt;
  &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#125;&lt;/span&gt;
&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;Je ne lance pas d'erreur ici, logiquement PHP s'en chargera selon sa configuration, comportement adopté par &lt;em&gt;stdclass&lt;/em&gt;. Mais rien n'empêche de lancer des erreurs en cas d'attribut indéfini.&lt;/p&gt;


&lt;h3&gt;Conclusion&lt;/h3&gt;

&lt;p&gt;Personnellement, l'emploi d'un tableau me parait plus propre plutôt que de définir de nouveau attribut. Un tableau est une structure naturellement dynamique en PHP et on sait que, quoi qu'il contienne, il conservera toujours la même visibilité. Quel est la visibilité de nouveaux attributs définit durant l'exécution? Bonne question, &lt;em&gt;public&lt;/em&gt; j'imagine...&lt;/p&gt;


&lt;p&gt;J'en ai déjà vu certains dire (ou plutôt écrire) que l'utilisation de tableaux rendait les écritures moins simples dans les méthodes. &lt;strong&gt;Oui mais&lt;/strong&gt;, le fait de définir les méthodes __get() et __set() tel qu'indiqué ci-dessus, rend possible l'emploi de $this-&amp;gt;indiceDuTableau, les écritures restent donc rigoureusement les mêmes qu'avec des attributs classiques.&lt;/p&gt;</description>
    
    
    
          <comments>http://www.code-web.org/post/2008/05/13/Les-methodes-magic-sous-PHP-5#comment-form</comments>
      <wfw:comment>http://www.code-web.org/post/2008/05/13/Les-methodes-magic-sous-PHP-5#comment-form</wfw:comment>
      <wfw:commentRss>http://www.code-web.org/feed/rss2/comments/48</wfw:commentRss>
    						<h3>Abonnement aux commentaires</h3>
						<p>
							<a href="http://www.code-web.org/subscribetocomments?post_id=48">
								<!-- # If the subscriber is logged in -->
																<!-- # If the subscriber is not logged in -->
																	S'abonner pour recevoir les commentaires suivants par email															</a>
						</p>
					  </item>
    
  <item>
    <title>Plugin de coloration syntaxique</title>
    <link>http://www.code-web.org/post/2008/03/28/Plugin-de-coloration-syntaxique</link>
    <guid isPermaLink="false">urn:md5:7fbc10fd147c9cf1b4fd07697cd3fc91</guid>
    <pubDate>Mon, 31 Mar 2008 14:14:00 +0000</pubDate>
    <dc:creator>Thierry Geindre</dc:creator>
        <category>Dotclear</category>
        <category>dotclear</category><category>php</category><category>plugin</category>    
    <description>&lt;p&gt;La coloration syntaxique manquait sur Dotclear 2, je me suis donc lancé dans la réalisation d'un plugin remplissant cette fonction. A travers cette article, je vous propose de découvrir la création de plugin pour Dotclear 2.&lt;/p&gt;


&lt;p&gt;Pour réaliser la coloration, nous utiliserons tout simplement &lt;a href=&quot;http://qbnz.com/highlighter/&quot; hreflang=&quot;fr&quot;&gt;GeShi&lt;/a&gt;, une classe qui remplit admirablement bien cette fonction. Il s'agit donc d'intégrer le travaille de cette classe à Dotclear sous la forme d'un plugin.&lt;/p&gt;    &lt;p&gt;J'ai choisi de reprendre le nom de l'ancien plugin qui fournissait la coloration syntaxique ,SyntaxeHl pour &lt;em&gt;syntaxe highlight&lt;/em&gt; je présume.&lt;/p&gt;


&lt;h3&gt;Un répertoire avec GeShi pour le plugin&lt;/h3&gt;

&lt;p&gt;La première étape, très simple, consiste à créer un répertoire pour accueillir les fichiers de votre plugin. Pour cela, il suffit de créer un sous répertoire dans le répertoire &lt;em&gt;plugins&lt;/em&gt; de votre Dotclear. Nommons le par exemple &lt;em&gt;syntaxehl&lt;/em&gt;. Il vous faudra ensuite &lt;a href=&quot;http://sourceforge.net/project/showfiles.php?group_id=114997&quot; hreflang=&quot;fr&quot;&gt;télécharger la dernière version de GeShi&lt;/a&gt;. Décompressez  l'archive que vous venez de récupérer dans le répertoire que vous venez de créer. Ainsi vous devez obtenir un nouveau répertoire &lt;em&gt;/plugins/syntaxehl/geshi/&lt;/em&gt;. Dans ce répertoire, vous pouvez supprimer les répertoires &lt;em&gt;contrib&lt;/em&gt; et &lt;em&gt;doc&lt;/em&gt; qui ne nous serons pas utile.&lt;/p&gt;


&lt;h3&gt;Informations relatives au plugin&lt;/h3&gt;

&lt;p&gt;Nous allons maintenant définir les différentes informations relatives à notre plugin. Pour cela, vous devez créer un fichier &lt;em&gt;_define.php&lt;/em&gt; dans le répertoire &lt;em&gt;/plugins/syntaxehl/&lt;/em&gt;. Le contenu de ce fichier sera le suivant&amp;nbsp;:&lt;/p&gt;
&lt;pre class=&quot;php&quot;&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;lt;?php&lt;/span&gt;
&lt;span style=&quot;color: #ff0000&quot;&gt;$this&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #006600;&quot;&gt;registerModule&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;
  &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;/* Name */&lt;/span&gt;          &lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;SyntaxeHl&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;,&lt;/span&gt;
  &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;/* Description*/&lt;/span&gt;    &lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;Plugin de coloration syntaxique&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;,&lt;/span&gt;
  &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;/* Author */&lt;/span&gt;        &lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;Votre Nom&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;,&lt;/span&gt;
  &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;/* Version */&lt;/span&gt;       &lt;span style=&quot;color: #ff0000;&quot;&gt;'1'&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;,&lt;/span&gt;
  &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;/* Permissions */&lt;/span&gt;   &lt;span style=&quot;color: #ff0000;&quot;&gt;'usage,contentadmin'&lt;/span&gt;
&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;?&amp;gt;&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;Les commentaires accompagnant chacun des paramètres sont suffisamment clair pour comprendre de quoi il s'agit. Nous ne nous arrêterons que sur le champ concernant les permissions. Il s'agit ici de définir quel type d'utilisateur peut activer le plugin. Dans cet exemple, nous avons choisit d'autoriser tous les utilisateurs mais on aurait également pu se limiter aux administrateurs en indiquant seulement &lt;em&gt;admin&lt;/em&gt;.&lt;/p&gt;


&lt;p&gt;Enregistrez les modifications puis rendez vous sur la page listant les plugin installés sur votre blog. Vous constaterez avec joie que le plugin que vous venez de créer apparaît bien dans la liste. Pour l'instant, ce plugin est totalement inutile puisqu'il ne fait absolument rien. Nous allons donc lui ajouter ses fonctionnalités.&lt;/p&gt;


&lt;h3&gt;Intercepter le code à mettre en forme&lt;/h3&gt;

&lt;p&gt;Pour mettre en forme le code inséré dans un billet, j'ai choisi d'exploiter la classe wiki2xhtml qui est employée, comme son nom l'indique, pour transformer la syntaxe Wiki d'un billet en (X)HTML.Ce choix peut être discutable puisqu'il ne permet pas de mettre en forme du code inséré dans un billet écrit directement en HTML.Cela-dit, je part du principe qu'un billet écrit en HTML directement ne devrait pas faire l'objet de modifications par un plugin puisqu'en principe il est déjà écrit dans sa version finale.&lt;/p&gt;


&lt;p&gt;Pour mettre en forme les codes sources, nous allons exploiter une des fonctionnalités offertes par wiki2xhtml&amp;nbsp;: les macros. Pour cela, nous utiliserons la fonction registerFunction de wiki2xhtml qui attend deux paramètres&amp;nbsp;: le type de fonction que l'on souhaite enregistrer et la fonction en question. Pour le type, on indiquera simplement &lt;em&gt;macro:nom_de_la_macro&lt;/em&gt;. Le fait d'enregistrer une fonction en tant que macro va déclencher l'appel à notre fonction lorsque la classe rencontrera dans le contenu du billet le texte suivant&amp;nbsp;:&lt;/p&gt;

&lt;pre&gt;&amp;#47;&amp;#47;&amp;#47;nom_de_la_macro
du texte
&amp;#47;&amp;#47;&amp;#47;&lt;/pre&gt;



&lt;p&gt;Ici, notre fonction recevra &lt;em&gt;du texte&lt;/em&gt; en paramètre, il nous suffira alors d'effectuer toutes les modifications souhaitées puis de retourner la version modifié du texte pour qu'elle remplace l'originale.&lt;/p&gt;


&lt;h3&gt;Enregistrer notre macro pour wiki2xhtml&lt;/h3&gt;

&lt;p&gt;Nous n'avons pour le moment qu'un bout de la solution finale. En effet, même si on sait comment procéder pour obtenir le code à mettre en forme, on ne sait pas vraiment comment procéder pour enregistrer notre fonction auprès de la classe wiki2xhtml aux bons moments.&lt;/p&gt;


&lt;h4&gt;Le système des behavior&lt;/h4&gt;

&lt;p&gt;Depuis la version 2, Dotclear intègre un tout nouveau système&amp;nbsp;: les &lt;em&gt;behavior&lt;/em&gt;. Terme qui peut être traduit littéralement par &lt;em&gt;comportement&lt;/em&gt;. C'est un système très simple à utiliser qui propose une gestion événementielle. En fait, un plugin peut enregistrer une fonction pour tel ou tel événement. Lorsque cet événement se produit, Dotclear appel la  ou les fonctions qui ont été enregistrées pour cet événement. En plus, ces fonctions recevront un certain nombre de paramètres différents en fonction du behavior concerné.&lt;/p&gt;


&lt;p&gt;Parmi la &lt;a href=&quot;http://doc.dotclear.net/2.0/resources/plugins/behaviors&quot; hreflang=&quot;fr&quot;&gt;liste des behavior diponibles&lt;/a&gt;, on trouve &lt;em&gt;coreWikiPostInit&lt;/em&gt; qui est appelé lors de l'initialisation de la classe wiki2xhtml. La fonction qui sera enregistrée pour cette événement recevra une référence vers l'instance de wiki2xhtml en paramètre. Il sera donc très facile d'y enregistrer notre macro.&lt;/p&gt;


&lt;h3&gt;Quelques fichiers&lt;/h3&gt;

&lt;p&gt;Nous avons toutes les solutions en main pour la création de notre plugin. Nous allons maintenant pouvoir entamer le codage. Il faudra bien sur placer notre code dans certains fichiers. Comme vous pouvez l'imaginer, le fait de créer un fichier nommé n'importe comment ne sera pas forcément pris en compte par Dotclear. Le nommage est important pour le bon fonctionnement de notre plugin.&lt;/p&gt;


&lt;p&gt;Ce qui va déterminer le nom du fichier que nous allons utiliser, c'est la partie de Dotclear dans laquelle on souhaite agir, la partie publique ou la partie administration. Pour agir sur la partie publique, on créera un fichier nommé &lt;em&gt;_public.php&lt;/em&gt; ou alors un fichier &lt;em&gt;_admin.php&lt;/em&gt; pour agir dans la partie administration.&lt;/p&gt;


&lt;p&gt;Pour notre pugin, c'est dans la partie admin que nous voulons agir. En effet, les billets rédigés en syntaxe Wiki sont transformés en (X)HTML dans la partie administration. Nous allons donc créer un fichier &lt;em&gt;_admin.php&lt;/em&gt; dans le répertoire &lt;em&gt;/plugins/syntaxehl/&lt;/em&gt;.&lt;/p&gt;


&lt;h3&gt;Codage du plugin&lt;/h3&gt;

&lt;p&gt;Il ne nous reste maintenant plus qu'à coder notre plugin. Placez le code suivant dans le fichier &lt;em&gt;_admin.php&lt;/em&gt; que nous venons de créer.&lt;/p&gt;

&lt;pre class=&quot;php&quot;&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;lt;?php&lt;/span&gt;
&lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;// Protège contre les accès directes au fichier&lt;/span&gt;
&lt;span style=&quot;color: #b1b100;&quot;&gt;if&lt;/span&gt; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;!&lt;/span&gt;&lt;span style=&quot;color: #000066;&quot;&gt;defined&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;'DC_CONTEXT_ADMIN'&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #000066;&quot;&gt;exit&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;;&lt;/span&gt;
&amp;nbsp;
&lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;// Require façon Dotclear&lt;/span&gt;
&lt;span style=&quot;color: #b1b100;&quot;&gt;require&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000066;&quot;&gt;dirname&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;__FILE__&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;'/geshi/geshi.php'&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;;&lt;/span&gt;
&amp;nbsp;
&lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;// On enregistre notre behavior&lt;/span&gt;
&lt;span style=&quot;color: #ff0000&quot;&gt;$core&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #006600;&quot;&gt;addBehavior&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;'coreInitWikiPost'&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #000066;&quot;&gt;array&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;'syntaxeHl'&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;'registerFunc'&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;;&lt;/span&gt;
&amp;nbsp;
&amp;nbsp;
&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;class&lt;/span&gt; syntaxeHl
&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#123;&lt;/span&gt;
        &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;// Fonction correspondant à notre behavior&lt;/span&gt;
        &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #000066;&quot;&gt;static&lt;/span&gt; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;function&lt;/span&gt; registerFunc&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span style=&quot;color: #ff0000&quot;&gt;$wiki2xhtml&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;
        &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#123;&lt;/span&gt;
                &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;// On lit de répertoire de GeShi pour connaître&lt;/span&gt;
                &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;// la liste des langages diponibles&lt;/span&gt;
                &lt;span style=&quot;color: #ff0000&quot;&gt;$dir&lt;/span&gt; &lt;span style=&quot;color: #66cc66;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #000066;&quot;&gt;dirname&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;__FILE__&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;'/geshi/geshi/'&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;;&lt;/span&gt;
                &lt;span style=&quot;color: #ff0000&quot;&gt;$od&lt;/span&gt; &lt;span style=&quot;color: #66cc66;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #000066;&quot;&gt;opendir&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #ff0000&quot;&gt;$dir&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;;&lt;/span&gt;
                &lt;span style=&quot;color: #b1b100;&quot;&gt;while&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #ff0000&quot;&gt;$f&lt;/span&gt; &lt;span style=&quot;color: #66cc66;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #000066;&quot;&gt;readdir&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #ff0000&quot;&gt;$od&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;
                &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#123;&lt;/span&gt;
                        &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;// Si c'est bien un fichier portant l'extension .php&lt;/span&gt;
                        &lt;span style=&quot;color: #b1b100;&quot;&gt;if&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000066;&quot;&gt;is_file&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #ff0000&quot;&gt;$dir&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #ff0000&quot;&gt;$f&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span style=&quot;color: #000066;&quot;&gt;substr&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #ff0000&quot;&gt;$f&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #cc66cc;&quot;&gt;-4&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #cc66cc;&quot;&gt;4&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;==&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;'.php'&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;
                        &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#123;&lt;/span&gt;
                                &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;// On enregistre une nouvelle macro pour ce langage&lt;/span&gt;
                                &lt;span style=&quot;color: #ff0000&quot;&gt;$lang&lt;/span&gt; &lt;span style=&quot;color: #66cc66;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #000066;&quot;&gt;str_replace&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;'.php'&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;''&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #ff0000&quot;&gt;$f&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;;&lt;/span&gt;
                                &lt;span style=&quot;color: #ff0000&quot;&gt;$wiki2xhtml&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #006600;&quot;&gt;registerFunction&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;'macro:['&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #ff0000&quot;&gt;$lang&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;']'&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #000066;&quot;&gt;array&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;'syntaxeHl'&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;'parse'&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;;&lt;/span&gt;
                        &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#125;&lt;/span&gt;
                &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#125;&lt;/span&gt;
        &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#125;&lt;/span&gt;
&amp;nbsp;
        &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;// Fonction appelée par Wiki2Xhtml&lt;/span&gt;
        &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #000066;&quot;&gt;static&lt;/span&gt; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;function&lt;/span&gt; parse&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #ff0000&quot;&gt;$text&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #ff0000&quot;&gt;$args&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;
        &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#123;&lt;/span&gt;
                &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;// On supprime les caractères blancs en&lt;/span&gt;
                &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;// en début et fin de chaîne (optionnel)&lt;/span&gt;
                &lt;span style=&quot;color: #ff0000&quot;&gt;$text&lt;/span&gt; &lt;span style=&quot;color: #66cc66;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #000066;&quot;&gt;trim&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #ff0000&quot;&gt;$text&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;;&lt;/span&gt;
&amp;nbsp;
                &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;// Création d'une instance de GeShi&lt;/span&gt;
                &lt;span style=&quot;color: #ff0000&quot;&gt;$geshi&lt;/span&gt; &lt;span style=&quot;color: #66cc66;&quot;&gt;=&amp;amp;&lt;/span&gt; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;new&lt;/span&gt; GeSHi&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #ff0000&quot;&gt;$text&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #ff0000&quot;&gt;$args&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;;&lt;/span&gt;
&amp;nbsp;
                &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;// On désactive les liens sur les mots clés (optionnel)&lt;/span&gt;
                &lt;span style=&quot;color: #ff0000&quot;&gt;$geshi&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #006600;&quot;&gt;enable_keyword_links&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;false&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;;&lt;/span&gt;
&amp;nbsp;
                &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;// On retourne le code mis en forme, coloré&lt;/span&gt;
                &lt;span style=&quot;color: #b1b100;&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: #ff0000&quot;&gt;$geshi&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #006600;&quot;&gt;parse_code&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;;&lt;/span&gt;
        &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#125;&lt;/span&gt;
&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#125;&lt;/span&gt;
&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;?&amp;gt;&lt;/span&gt;&lt;/pre&gt;


&lt;p&gt;Ayant déjà bien expliqué les solutions employées ci-dessus, je ne m'attarderais pas sur ce code. Vous pouvez consulter la documentation de Dotclear pour en savoir plus sur les méthodes utilisées.&lt;/p&gt;


&lt;p&gt;Notez simplement que l'on utilise pas directement le nom du langage pour nommer nos macro, mais qu'on le place entre crochets ([ et ]). Il y a deux raisons à cela, d'une part le plugin qui assurait la coloration syntaxique pour Dotclear 1 utilisait lui aussi les crochets (rétro compatibilité) et d'autre part cela permet de ne pas interférer avec d'autres macros comme &lt;em&gt;html&lt;/em&gt; par exemple qui permet d'insérer du code html dans un billet lorsqu'on est en syntaxe Wiki.&lt;/p&gt;


&lt;h3&gt;Un test&amp;nbsp;?&lt;/h3&gt;

&lt;p&gt;Si vous avez correctement suivit cet article, votre plugin est maintenant fonctionnel. Vous pouvez le tester en créant un nouveau billet et en insérant le code suivant (en syntaxe Wiki):&lt;/p&gt;
&lt;pre&gt;
&amp;#47;&amp;#47;&amp;#47;[php]
&amp;lt;?php
  echo 'Hello world!';
?&amp;gt;
&amp;#47;&amp;#47;&amp;#47;
&lt;/pre&gt;


&lt;p&gt;et vous obtiendrez normalement le résultat suivant&amp;nbsp;:&lt;/p&gt;
&lt;pre class=&quot;php&quot;&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;lt;?php&lt;/span&gt;
  &lt;span style=&quot;color: #000066;&quot;&gt;echo&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;'Hello world!'&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;?&amp;gt;&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;Vous remarquerez peut-être que ce plugin n'est pas rétro actif, c'est à dire que les anciens billets contenant du code sources ne seront pas colorés. Pour chacun d'entre eux, il vous faudra les éditer et enregistrer les modifications.&lt;/p&gt;


&lt;h3&gt;Packager votre plugin&lt;/h3&gt;

&lt;p&gt;Maintenant que votre plugin fonctionne, il pourrait être intéressant de le distribuer. Le meilleur moyen pour cela est de créer un package. Le moyen le plus simple est d'utiliser le plugin &lt;a href=&quot;http://plugins.dotaddict.org/dc2/details/Packager&quot; hreflang=&quot;fr&quot;&gt;Packager&lt;/a&gt; dont c'est la seule et unique fonction.&lt;/p&gt;


&lt;h3&gt;Version finale&lt;/h3&gt;

&lt;p&gt;Pour ceux qui souhaitent simplement installer ce plugin sur leur blog, vous pouvez consulter &lt;a href=&quot;http://plugins.code-web.org/plugin/syntaxe-hl/&quot; hreflang=&quot;fr&quot;&gt;le dépôt de plugin de Code-Web.org&lt;/a&gt; où vous trouverez les paquets directement installables sur Dotclear. Si vous avez des questions qui concernent ce plugin, rendez-vous sur le &lt;a href=&quot;http://www.code-web.org/post/2008/04/14/SyntaxeHl-11&quot; hreflang=&quot;fr&quot;&gt;billet traitant de la dernière version disponible&lt;/a&gt;.&lt;/p&gt;</description>
    
    
    
    						<h3>Abonnement aux commentaires</h3>
						<p>
							<a href="http://www.code-web.org/subscribetocomments?post_id=39">
								<!-- # If the subscriber is logged in -->
																<!-- # If the subscriber is not logged in -->
																	S'abonner pour recevoir les commentaires suivants par email															</a>
						</p>
					  </item>
    
  <item>
    <title>Upload de fichiers via formulaire avec PHP</title>
    <link>http://www.code-web.org/post/2008/01/14/40-upload-de-fichiers-via-formulaire-avec-php</link>
    <guid isPermaLink="false">urn:md5:970fdd726cba736a6e375f6b37f5dc06</guid>
    <pubDate>Mon, 14 Jan 2008 16:10:00 +0000</pubDate>
    <dc:creator>Thierry Geindre</dc:creator>
        <category>PHP</category>
        <category>apache</category><category>automatisation</category><category>html</category><category>php</category>    
    <description>&lt;p&gt;L'utilisation d'un langage côté serveur permet d'automatiser ou de simplifier bon nombre de tâches d'administration. L'upload (envoi) de fichiers fait partie de ces tâches qui peuvent être simplifiées. En effet, il est possible d'envoyer vos fichiers via un formulaire sur une page web puis d'effectuer le traitement de celui-ci grâce à PHP. Il ne sera donc plus nécessaire d'utiliser à chaque fois un client FTP.&lt;/p&gt;


&lt;p&gt;Ceci pourra être très pratique dans le cas de l'administration d'une galerie photo par exemple, ou encore simplement pour illustrer vos publications.&lt;/p&gt;    &lt;h3&gt;Pré requis&lt;/h3&gt;

&lt;h4&gt;Droits d'écriture&lt;/h4&gt;

&lt;p&gt;Pour que l'upload de fichiers puisse fonctionner correctement, il est impératif que le répertoire dans lequel on souhaitera placer les fichiers soit accessible en écriture. Sur certains systèmes, les dossiers sont toujours accessibles en écriture, c'est le cas de Windows (&amp;lt; Vista). En revanche, sur d'autres, tel que les systèmes dérivés d'UNIX, il est possible d'interdire l'écriture dans certains répertoires.&lt;/p&gt;


&lt;p&gt;Ce réglage ne sera donc nécessaire que si le système de la machine qui héberge votre site utilise un mécanisme de gestions de droits sur les fichiers et répertoires.&lt;/p&gt;


&lt;p&gt;Pour autoriser l'écriture, vous avez plusieurs possibilités. Soit en utilisant votre client FTP, auquel cas vous pourrez modifier les droits en accédant aux attributs ou aux propriétés de votre répertoire. Vous pouvez également modifier les droits de votre répertoire grâce à la fonction &lt;a href=&quot;http://fr.php.net/chmod&quot; hreflang=&quot;fr&quot;&gt;chmod()&lt;/a&gt; de PHP (reportez-vous à la documentation). Enfin, si vous avez accès directement au système de la machine (par SSH par exemple), vous pourrez modifier les droits très facilement grâce aux commandes appropriées (reportez vous à la documentation de votre système).&lt;/p&gt;


&lt;h4&gt;Configuration de php (php.ini)&lt;/h4&gt;

&lt;p&gt;Pour que l'envoie de fichiers fonctionne, PHP doit être configuré de la bonne manière. Ainsi certaine directives doivent avoir certaines valeurs. Vous pouvez modifier ces valeurs dans le fichier php.ini ou grâce à la fonction &lt;a href=&quot;http://fr.php.net/ini_set&quot; hreflang=&quot;fr&quot;&gt;ini_set()&lt;/a&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;file_uploads&lt;/strong&gt; est la directive qui autorise ou interdit l'upload de fichier. Sa valeur doit donc être &lt;em&gt;on&lt;/em&gt; (inverse de off).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;upload_tmp_dir&lt;/strong&gt; correspond au répertoire dans lequel seront placé les fichiers temporairement uploadés. Ce répertoire doit lui aussi être accessible en écriture. Sur les systèmes UNIX et dérivés il s'agira souvent du répertoire /tmp/ accessible en écriture pour tous les utilisateurs du système.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;upload_max_filesize&lt;/strong&gt; détermine la taille maximal de l'envoi d'un fichier. Il n'y a pas de valeur parfaite mais &lt;em&gt;2M&lt;/em&gt; est une valeur courante et satisfaisante dans la plupart des cas. Elle correspond à 2 méga octets.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;post_max_size&lt;/strong&gt; correspond au volume maximum d'informations qui peuvent être envoyées via la méthode POST. Cette directive prime sur la précédente et doit logiquement avoir une valeur au moins égale. C'est pourquoi, là encore, &lt;em&gt;2M&lt;/em&gt; est une valeur satisfaisante.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Si vous rencontrez des difficultés dans l'upload de vos fichiers avec la méthodes que nous allons voir, c'est probablement à cause de votre configuration. SI vous ne pouvez pas y avoir accès (cas des hébergement mutualisés), utilisez la fonction &lt;a href=&quot;http://fr.php.net/phpinfo&quot; hreflang=&quot;fr&quot;&gt;phpinfo()&lt;/a&gt; qui vous donnera toutes les informations de configuration de php.&lt;/p&gt;


&lt;h3&gt;Le formulaire HTML&lt;/h3&gt;

&lt;p&gt;La première étape est de créer notre formulaire en HTML qui permettra l'envoie de fichiers au serveur. Pour cela, nous allons devoir préciser type de données qui sera envoyé via notre formulaire. Ce type est définit par défaut à &lt;em&gt;application/x-www-form-urlencoded&lt;/em&gt; et ne permet pas l'envoie de fichiers. Nous allons le modifier au moyen de l'attribut &lt;em&gt;enctype&lt;/em&gt; de la balise &lt;em&gt;form&lt;/em&gt; et luis donner pour valeur &lt;em&gt;multipart/form-data&lt;/em&gt; ce qui permettra l'envoie de n'importe quel type de données.&lt;/p&gt;


&lt;p&gt;Enfin, pour permettre la sélection du fichier par le client, nous utiliserons un élément de formulaire de type &lt;em&gt;file&lt;/em&gt;.&lt;/p&gt;

&lt;pre class=&quot;html&quot;&gt;&lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;lt;&lt;/span&gt;?xml &lt;span style=&quot;color: #000066;&quot;&gt;version&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;1.0&amp;quot;&lt;/span&gt; encoding&lt;span style=&quot;color: #66cc66;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;UTF-8&amp;quot;&lt;/span&gt;?&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
﻿&lt;span style=&quot;color: #00bbdd;&quot;&gt;&amp;lt;!DOCTYPE html PUBLIC &amp;quot;-//W3C//DTD XHTML 1.0 Strict//EN&amp;quot; &amp;quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd&amp;quot;&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;lt;html&lt;/span&gt; xmlns&lt;span style=&quot;color: #66cc66;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;http://www.w3.org/1999/xhtml&amp;quot;&lt;/span&gt; xml:&lt;span style=&quot;color: #000066;&quot;&gt;lang&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;fr&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: #000066;&quot;&gt;lang&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;fr&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
 &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;lt;head&amp;gt;&lt;/span&gt;&lt;/span&gt;
  &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;lt;title&amp;gt;&lt;/span&gt;&lt;/span&gt;Formulaire d'upload&lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;lt;/title&amp;gt;&lt;/span&gt;&lt;/span&gt;
 &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;lt;/head&amp;gt;&lt;/span&gt;&lt;/span&gt;
 &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;lt;body&amp;gt;&lt;/span&gt;&lt;/span&gt;
  &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;lt;form&lt;/span&gt; &lt;span style=&quot;color: #000066;&quot;&gt;method&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;post&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: #000066;&quot;&gt;enctype&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;multipart/form-data&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: #000066;&quot;&gt;action&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;upload.php&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
   &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;lt;p&amp;gt;&lt;/span&gt;&lt;/span&gt;
    &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;lt;label&amp;gt;&lt;/span&gt;&lt;/span&gt;S&lt;span style=&quot;color: #ddbb00;&quot;&gt;&amp;amp;eacute;&lt;/span&gt;lectionnez un fichier :
    &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;lt;input&lt;/span&gt; &lt;span style=&quot;color: #000066;&quot;&gt;type&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;file&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: #000066;&quot;&gt;name&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;fichier&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: #66cc66;&quot;&gt;/&lt;/span&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
    &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;lt;/label&amp;gt;&lt;/span&gt;&lt;/span&gt;
   &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;&lt;/span&gt;
   &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;lt;p&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;lt;input&lt;/span&gt; &lt;span style=&quot;color: #000066;&quot;&gt;type&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;submit&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: #000066;&quot;&gt;value&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;Envoyer&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: #66cc66;&quot;&gt;/&lt;/span&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;&lt;/span&gt;
  &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;lt;/form&amp;gt;&lt;/span&gt;&lt;/span&gt;
 &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;


&lt;p&gt;Cet exemple de formulaire permet d'envoyer un seul fichier sur le serveur. Ce fichier sera placé dans un répertoire contenant des fichiers temporaires et, s'il ne fait pas l'objet d'un traitement supplémentaire, il sera supprimé à la fin de l'exécution de la requête. C'est pourquoi nous allons devoir faire intervenir PHP.&lt;/p&gt;


&lt;h3&gt;Le script PHP&lt;/h3&gt;

&lt;p&gt;Passons maintenant au script chargé du traitement approprié. Nous allons dans un premier temps en voir une version simple qui autorise tout types de fichiers.&lt;/p&gt;


&lt;p&gt;Lorsque qu'un script reçoit les informations transmises par un formulaire, on utilise habituellement la variable globale &lt;em&gt;$_POST&lt;/em&gt; pour les lire. Cependant, pour les élément de type &lt;em&gt;file&lt;/em&gt;, on devra récupérer les informations concernant le fichiers via la variable &lt;em&gt;$_FILES&lt;/em&gt;. Il s'agit d'un tableau associatif contenant différentes informations concernant les fichiers envoyés. La structure de ce tableau est la suivante&amp;nbsp;:&lt;/p&gt;



&lt;pre&gt;Array
(
    [nom_du_champ_file] =&amp;gt; Array
        (
            [name] =&amp;gt; &quot;MaBelleImage.jpg&quot; // Nom du fichier
            [type] =&amp;gt; &quot;image/jpg&quot; // type de fichier
            [tmp_name] =&amp;gt; &quot;chemin_complet_du_fichier_uploadé&quot; // Emplacement temporaire
            [error] =&amp;gt; 0 // Code d'erreur si erreur il y a
            [size] =&amp;gt; 1000 // Taille du fichier
        )
)&lt;/pre&gt;


&lt;p&gt;A partir de ces informations, nous pourrons effectuer différents contrôles que nous verrons un peu plus tard. Pour le moment, nous allons simplement sauvegarder le fichier envoyé.&lt;/p&gt;

&lt;pre class=&quot;php&quot;&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;lt;?php&lt;/span&gt;
  &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;// On définit notre répertoire cible&lt;/span&gt;
  &lt;span style=&quot;color: #ff0000&quot;&gt;$dstRep&lt;/span&gt; &lt;span style=&quot;color: #66cc66;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;'./fichiers/'&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;;&lt;/span&gt;
&amp;nbsp;
  &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;// On vérifie qu'il est accessible en écriture&lt;/span&gt;
  &lt;span style=&quot;color: #b1b100;&quot;&gt;if&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;!&lt;/span&gt;&lt;span style=&quot;color: #000066;&quot;&gt;is_writable&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #ff0000&quot;&gt;$dstRep&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;
    &lt;span style=&quot;color: #000066;&quot;&gt;die&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;'Impossible d&lt;span style=&quot;color: #000099; font-weight: bold;&quot;&gt;\'&lt;/span&gt;écrire dans le répertoire cible.'&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;;&lt;/span&gt;
&amp;nbsp;
  &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;// On vérifie d'abord que des données ont bien été envoyées&lt;/span&gt;
  &lt;span style=&quot;color: #b1b100;&quot;&gt;if&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;!&lt;/span&gt;&lt;span style=&quot;color: #000066;&quot;&gt;isset&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #ff0000&quot;&gt;$_FILES&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#91;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;'fichier'&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;
    &lt;span style=&quot;color: #000066;&quot;&gt;die&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;'Aucune données'&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;;&lt;/span&gt;
&amp;nbsp;
  &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;// On finit par déplacer le fichier dans le répertoire cible&lt;/span&gt;
  &lt;span style=&quot;color: #000066;&quot;&gt;move_uploaded_file&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #ff0000&quot;&gt;$_FILES&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#91;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;'fichier'&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#91;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;'tmp_name'&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #ff0000&quot;&gt;$dstRep&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #ff0000&quot;&gt;$_FILES&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#91;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;'fichier'&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#91;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;'name'&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;;&lt;/span&gt;
&amp;nbsp;
&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;?&amp;gt;&lt;/span&gt;&lt;/pre&gt;


&lt;p&gt;Bien que ce script soit théoriquement fonctionnel, il est très imparfait. En effet, il ne contrôle pas le bon déroulement de l'envoie du fichier au moyen des codes d'erreur pouvant être indiqués dans le tableau $_FILES. En effet, la valeur de ce code nous indique le type d'erreur qui s'est produite durant l'envoie.&lt;/p&gt;

&lt;pre class=&quot;php&quot;&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;lt;?php&lt;/span&gt;
  &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;// On définit notre répertoire cible&lt;/span&gt;
  &lt;span style=&quot;color: #ff0000&quot;&gt;$dstRep&lt;/span&gt; &lt;span style=&quot;color: #66cc66;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;'./fichiers/'&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;;&lt;/span&gt;
&amp;nbsp;
  &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;// On vérifie qu'il est accessible en écriture&lt;/span&gt;
  &lt;span style=&quot;color: #b1b100;&quot;&gt;if&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;!&lt;/span&gt;&lt;span style=&quot;color: #000066;&quot;&gt;is_writable&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #ff0000&quot;&gt;$dstRep&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;
    &lt;span style=&quot;color: #000066;&quot;&gt;die&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;'Impossible d&lt;span style=&quot;color: #000099; font-weight: bold;&quot;&gt;\'&lt;/span&gt;écrire dans le répertoire cible.'&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;;&lt;/span&gt;
&amp;nbsp;
  &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;// On vérifie d'abord que des données ont bien été envoyées&lt;/span&gt;
  &lt;span style=&quot;color: #b1b100;&quot;&gt;if&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;!&lt;/span&gt;&lt;span style=&quot;color: #000066;&quot;&gt;isset&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #ff0000&quot;&gt;$_FILES&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#91;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;'fichier'&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;
    &lt;span style=&quot;color: #000066;&quot;&gt;die&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;'Aucune données'&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;;&lt;/span&gt;
&amp;nbsp;
  &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;// Vérification du code d'erreur&lt;/span&gt;
  &lt;span style=&quot;color: #b1b100;&quot;&gt;switch&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #ff0000&quot;&gt;$_FILES&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#91;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;'fichier'&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#91;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;'error'&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;
  &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#123;&lt;/span&gt;
     &lt;span style=&quot;color: #b1b100;&quot;&gt;case&lt;/span&gt; &lt;span style=&quot;color: #cc66cc;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;:&lt;/span&gt; &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;// UPLOAD_ERR_INI_SIZE&lt;/span&gt;
        &lt;span style=&quot;color: #000066;&quot;&gt;die&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;Le fichier dépasse la limite autorisée par le serveur (fichier php.ini) !&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;;&lt;/span&gt;
        &lt;span style=&quot;color: #b1b100;&quot;&gt;break&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;;&lt;/span&gt;
      &lt;span style=&quot;color: #b1b100;&quot;&gt;case&lt;/span&gt; &lt;span style=&quot;color: #cc66cc;&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;:&lt;/span&gt; &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;// UPLOAD_ERR_FORM_SIZE&lt;/span&gt;
        &lt;span style=&quot;color: #000066;&quot;&gt;die&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;Le fichier dépasse la limite autorisée dans le formulaire HTML !&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;;&lt;/span&gt;
        &lt;span style=&quot;color: #b1b100;&quot;&gt;break&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;;&lt;/span&gt;
      &lt;span style=&quot;color: #b1b100;&quot;&gt;case&lt;/span&gt; &lt;span style=&quot;color: #cc66cc;&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;:&lt;/span&gt; &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;// UPLOAD_ERR_PARTIAL&lt;/span&gt;
        &lt;span style=&quot;color: #000066;&quot;&gt;die&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;L'envoi du fichier a été interrompu pendant le transfert !&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;;&lt;/span&gt;
        &lt;span style=&quot;color: #b1b100;&quot;&gt;break&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;;&lt;/span&gt;
      &lt;span style=&quot;color: #b1b100;&quot;&gt;case&lt;/span&gt; &lt;span style=&quot;color: #cc66cc;&quot;&gt;4&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;:&lt;/span&gt; &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;// UPLOAD_ERR_NO_FILE&lt;/span&gt;
        &lt;span style=&quot;color: #000066;&quot;&gt;die&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;Le fichier que vous avez envoyé a une taille nulle !&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;;&lt;/span&gt;
        &lt;span style=&quot;color: #b1b100;&quot;&gt;break&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;;&lt;/span&gt;
  &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#125;&lt;/span&gt;
&amp;nbsp;
  &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;// On finit par déplacer le fichier dans le répertoire cible&lt;/span&gt;
  &lt;span style=&quot;color: #000066;&quot;&gt;move_uploaded_file&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #ff0000&quot;&gt;$_FILES&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#91;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;'fichier'&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#91;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;'tmp_name'&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #ff0000&quot;&gt;$dstRep&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #ff0000&quot;&gt;$_FILES&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#91;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;'fichier'&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#91;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;'name'&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;;&lt;/span&gt;
&amp;nbsp;
&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;?&amp;gt;&lt;/span&gt;&lt;/pre&gt;


&lt;p&gt;Ainsi, ce script ne devrait pas lever d'erreur. A noter que &lt;a href=&quot;http://fr.php.net/manual/fr/features.file-upload.errors.php&quot; hreflang=&quot;fr&quot;&gt;d'autres types d'erreurs&lt;/a&gt; ont été introduit avec les nouvelles versions de PHP. Cependant, il reste assez incomplet. Nous allons maintenant lui ajouter quelques fonctionnalités supplémentaires. Et pour commencer, une indispensable, éviter l'écrasement des fichiers.&lt;/p&gt;


&lt;h4&gt;Éviter l'écrasement de fichier&lt;/h4&gt;

&lt;p&gt;Le problème que risque de poser le script que nous avons vu est l'écrasement de fichiers déjà existant. En effet, si vous envoyez deux fichiers portant le même nom, le premier fichier sera écrasé par le second. Pour remédier à cela, je vous propose une solution assez simple qui consiste à préfixer le nom du fichier d'un chiffre. Il existe d'autres solutions mais celle-ci est assez efficace et durable.&lt;/p&gt;

&lt;pre class=&quot;php&quot;&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;lt;?php&lt;/span&gt;
  &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;// On définit notre répertoire cible&lt;/span&gt;
  &lt;span style=&quot;color: #ff0000&quot;&gt;$dstRep&lt;/span&gt; &lt;span style=&quot;color: #66cc66;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;'./fichiers/'&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;;&lt;/span&gt;
&amp;nbsp;
  &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;// On vérifie qu'il est accessible en écriture&lt;/span&gt;
  &lt;span style=&quot;color: #b1b100;&quot;&gt;if&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;!&lt;/span&gt;&lt;span style=&quot;color: #000066;&quot;&gt;is_writable&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #ff0000&quot;&gt;$dstRep&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;
    &lt;span style=&quot;color: #000066;&quot;&gt;die&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;'Impossible d&lt;span style=&quot;color: #000099; font-weight: bold;&quot;&gt;\'&lt;/span&gt;écrire dans le répertoire cible.'&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;;&lt;/span&gt;
&amp;nbsp;
  &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;// On vérifie d'abord que des données ont bien été envoyées&lt;/span&gt;
  &lt;span style=&quot;color: #b1b100;&quot;&gt;if&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;!&lt;/span&gt;&lt;span style=&quot;color: #000066;&quot;&gt;isset&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #ff0000&quot;&gt;$_FILES&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#91;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;'fichier'&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;
    &lt;span style=&quot;color: #000066;&quot;&gt;die&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;'Aucune données'&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;;&lt;/span&gt;
&amp;nbsp;
  &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;// Vérification du code d'erreur&lt;/span&gt;
  &lt;span style=&quot;color: #b1b100;&quot;&gt;switch&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #ff0000&quot;&gt;$_FILES&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#91;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;'fichier'&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#91;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;'error'&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;
  &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#123;&lt;/span&gt;
     &lt;span style=&quot;color: #b1b100;&quot;&gt;case&lt;/span&gt; &lt;span style=&quot;color: #cc66cc;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;:&lt;/span&gt; &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;// UPLOAD_ERR_INI_SIZE&lt;/span&gt;
        &lt;span style=&quot;color: #000066;&quot;&gt;die&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;Le fichier dépasse la limite autorisée par le serveur (fichier php.ini) !&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;;&lt;/span&gt;
        &lt;span style=&quot;color: #b1b100;&quot;&gt;break&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;;&lt;/span&gt;
      &lt;span style=&quot;color: #b1b100;&quot;&gt;case&lt;/span&gt; &lt;span style=&quot;color: #cc66cc;&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;:&lt;/span&gt; &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;// UPLOAD_ERR_FORM_SIZE&lt;/span&gt;
        &lt;span style=&quot;color: #000066;&quot;&gt;die&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;Le fichier dépasse la limite autorisée dans le formulaire HTML !&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;;&lt;/span&gt;
        &lt;span style=&quot;color: #b1b100;&quot;&gt;break&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;;&lt;/span&gt;
      &lt;span style=&quot;color: #b1b100;&quot;&gt;case&lt;/span&gt; &lt;span style=&quot;color: #cc66cc;&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;:&lt;/span&gt; &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;// UPLOAD_ERR_PARTIAL&lt;/span&gt;
        &lt;span style=&quot;color: #000066;&quot;&gt;die&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;L'envoi du fichier a été interrompu pendant le transfert !&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;;&lt;/span&gt;
        &lt;span style=&quot;color: #b1b100;&quot;&gt;break&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;;&lt;/span&gt;
      &lt;span style=&quot;color: #b1b100;&quot;&gt;case&lt;/span&gt; &lt;span style=&quot;color: #cc66cc;&quot;&gt;4&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;:&lt;/span&gt; &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;// UPLOAD_ERR_NO_FILE&lt;/span&gt;
        &lt;span style=&quot;color: #000066;&quot;&gt;die&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;Le fichier que vous avez envoyé a une taille nulle !&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;;&lt;/span&gt;
        &lt;span style=&quot;color: #b1b100;&quot;&gt;break&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;;&lt;/span&gt;
  &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#125;&lt;/span&gt;
&amp;nbsp;
  &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;// On vérifie si le fichier existe déjà dans le répertoire cible&lt;/span&gt;
  &lt;span style=&quot;color: #b1b100;&quot;&gt;if&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000066;&quot;&gt;file_exists&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #ff0000&quot;&gt;$dstRep&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #ff0000&quot;&gt;$_FILES&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#91;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;'fichier'&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#91;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;'name'&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;
  &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#123;&lt;/span&gt;
    &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;// On utilise une boucle pour incrémenter notre préfixe&lt;/span&gt;
    &lt;span style=&quot;color: #ff0000&quot;&gt;$i&lt;/span&gt; &lt;span style=&quot;color: #66cc66;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #cc66cc;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;;&lt;/span&gt;
    &lt;span style=&quot;color: #b1b100;&quot;&gt;while&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000066;&quot;&gt;file_exists&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #ff0000&quot;&gt;$dstRep&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #ff0000&quot;&gt;$i&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;'_'&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #ff0000&quot;&gt;$_FILES&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#91;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;'fichier'&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#91;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;'name'&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #ff0000&quot;&gt;$i&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;++&lt;/span&gt;
&amp;nbsp;
    &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;// Arrivé ici, on a trouvé un nom disponible avec un préfixe numérique&lt;/span&gt;
    &lt;span style=&quot;color: #ff0000&quot;&gt;$cible&lt;/span&gt; &lt;span style=&quot;color: #66cc66;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #ff0000&quot;&gt;$dstRep&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #ff0000&quot;&gt;$i&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;'_'&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #ff0000&quot;&gt;$_FILES&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#91;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;'fichier'&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#91;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;'name'&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;;&lt;/span&gt;
  &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#125;&lt;/span&gt;
  &lt;span style=&quot;color: #b1b100;&quot;&gt;else&lt;/span&gt;
  &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#123;&lt;/span&gt;
    &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;// Le fichier n'existe pas dans le répertoire cible,&lt;/span&gt;
    &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;// Nous pouvons donc utiliser le nom original&lt;/span&gt;
    &lt;span style=&quot;color: #ff0000&quot;&gt;$cible&lt;/span&gt; &lt;span style=&quot;color: #66cc66;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #ff0000&quot;&gt;$dstRep&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #ff0000&quot;&gt;$_FILES&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#91;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;'fichier'&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#91;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;'name'&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;;&lt;/span&gt;
  &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#125;&lt;/span&gt;
&amp;nbsp;
  &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;// On finit par déplacer le fichier dans le répertoire cible&lt;/span&gt;
  &lt;span style=&quot;color: #000066;&quot;&gt;move_uploaded_file&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #ff0000&quot;&gt;$_FILES&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#91;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;'fichier'&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#91;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;'tmp_name'&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #ff0000&quot;&gt;$cible&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;;&lt;/span&gt;
&amp;nbsp;
&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;?&amp;gt;&lt;/span&gt;&lt;/pre&gt;


&lt;p&gt;Avec cette nouvelle version, les fichiers ne pourront pas être écrasés. Nous allons maintenant voir comment limiter l'envoie à certains types de fichiers.&lt;/p&gt;


&lt;h4&gt;Limiter les types de fichiers autorisés&lt;/h4&gt;

&lt;p&gt;Pour instaurer cette limitation, on pourrait éventuellement se baser sur l'information de type (Type mime) contenue dans le tableau $_FILES. Cependant, cette information est fournit par le client, le navigateur. La règle d'or en matière de développement web est de ne jamais avoir confiance dans les données transmises par un tiers inconnu. D'autre part, cette information pourra être un peut différente pour un même type de fichier et des navigateurs différents. Pour ces raisons, on ne tiendra pas compte de cette information.&lt;/p&gt;


&lt;p&gt;Idéalement, il faudrait utiliser une méthode consistant à rechercher le type mime à partir du contenu du fichier. Il existe, pour cela la, fonction &lt;a href=&quot;http://fr.php.net/mime_content_type&quot; hreflang=&quot;fr&quot;&gt;mime_content_type()&lt;/a&gt; mais elle est désormais déprécier au profit de &lt;a href=&quot;http://www.php.net/manual/fr/ref.fileinfo.php&quot; hreflang=&quot;fr&quot;&gt;Fileinfo&lt;/a&gt;.&lt;/p&gt;


&lt;p&gt;Pour faire simple ici, nous baserons notre test sur l'extension du fichier. Pour que cette méthode soit suffisamment sûr, il est important que votre serveur soit correctement configuré pour ne pas exécuter de code côté serveur dans des fichiers ne portant pas une extension propre au langage. On a pu voir, par exemple, des serveurs autorisant l'exécution de code PHP dans fichiers portant l'extension .jpg, ce qui est un peu douteu...&lt;/p&gt;

&lt;pre class=&quot;php&quot;&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;lt;?php&lt;/span&gt;
  &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;// On définit notre répertoire cible&lt;/span&gt;
  &lt;span style=&quot;color: #ff0000&quot;&gt;$dstRep&lt;/span&gt; &lt;span style=&quot;color: #66cc66;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;'./fichiers/'&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;;&lt;/span&gt;
&amp;nbsp;
  &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;// On créé un tableau contenant les extensions autorisées&lt;/span&gt;
  &lt;span style=&quot;color: #ff0000&quot;&gt;$extOk&lt;/span&gt; &lt;span style=&quot;color: #66cc66;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #000066;&quot;&gt;array&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;'.jpg'&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;'.gif'&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;'.png'&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;;&lt;/span&gt;
&amp;nbsp;
  &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;// On vérifie qu'il est accessible en écriture&lt;/span&gt;
  &lt;span style=&quot;color: #b1b100;&quot;&gt;if&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;!&lt;/span&gt;&lt;span style=&quot;color: #000066;&quot;&gt;is_writable&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #ff0000&quot;&gt;$dstRep&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;
    &lt;span style=&quot;color: #000066;&quot;&gt;die&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;'Impossible d&lt;span style=&quot;color: #000099; font-weight: bold;&quot;&gt;\'&lt;/span&gt;écrire dans le répertoire cible.'&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;;&lt;/span&gt;
&amp;nbsp;
  &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;// On vérifie d'abord que des données ont bien été envoyées&lt;/span&gt;
  &lt;span style=&quot;color: #b1b100;&quot;&gt;if&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;!&lt;/span&gt;&lt;span style=&quot;color: #000066;&quot;&gt;isset&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #ff0000&quot;&gt;$_FILES&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#91;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;'fichier'&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;
    &lt;span style=&quot;color: #000066;&quot;&gt;die&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;'Aucune données'&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;;&lt;/span&gt;
&amp;nbsp;
  &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;// Vérification du code d'erreur&lt;/span&gt;
  &lt;span style=&quot;color: #b1b100;&quot;&gt;switch&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #ff0000&quot;&gt;$_FILES&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#91;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;'fichier'&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#91;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;'error'&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;
  &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#123;&lt;/span&gt;
     &lt;span style=&quot;color: #b1b100;&quot;&gt;case&lt;/span&gt; &lt;span style=&quot;color: #cc66cc;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;:&lt;/span&gt; &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;// UPLOAD_ERR_INI_SIZE&lt;/span&gt;
        &lt;span style=&quot;color: #000066;&quot;&gt;die&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;Le fichier dépasse la limite autorisée par le serveur (fichier php.ini) !&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;;&lt;/span&gt;
        &lt;span style=&quot;color: #b1b100;&quot;&gt;break&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;;&lt;/span&gt;
      &lt;span style=&quot;color: #b1b100;&quot;&gt;case&lt;/span&gt; &lt;span style=&quot;color: #cc66cc;&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;:&lt;/span&gt; &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;// UPLOAD_ERR_FORM_SIZE&lt;/span&gt;
        &lt;span style=&quot;color: #000066;&quot;&gt;die&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;Le fichier dépasse la limite autorisée dans le formulaire HTML !&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;;&lt;/span&gt;
        &lt;span style=&quot;color: #b1b100;&quot;&gt;break&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;;&lt;/span&gt;
      &lt;span style=&quot;color: #b1b100;&quot;&gt;case&lt;/span&gt; &lt;span style=&quot;color: #cc66cc;&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;:&lt;/span&gt; &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;// UPLOAD_ERR_PARTIAL&lt;/span&gt;
        &lt;span style=&quot;color: #000066;&quot;&gt;die&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;L'envoi du fichier a été interrompu pendant le transfert !&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;;&lt;/span&gt;
        &lt;span style=&quot;color: #b1b100;&quot;&gt;break&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;;&lt;/span&gt;
      &lt;span style=&quot;color: #b1b100;&quot;&gt;case&lt;/span&gt; &lt;span style=&quot;color: #cc66cc;&quot;&gt;4&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;:&lt;/span&gt; &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;// UPLOAD_ERR_NO_FILE&lt;/span&gt;
        &lt;span style=&quot;color: #000066;&quot;&gt;die&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;Le fichier que vous avez envoyé a une taille nulle !&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;;&lt;/span&gt;
        &lt;span style=&quot;color: #b1b100;&quot;&gt;break&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;;&lt;/span&gt;
  &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#125;&lt;/span&gt;
&amp;nbsp;
  &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;// On lit l'extension du fichier&lt;/span&gt;
  &lt;span style=&quot;color: #ff0000&quot;&gt;$fileExt&lt;/span&gt; &lt;span style=&quot;color: #66cc66;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #000066;&quot;&gt;substr&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #ff0000&quot;&gt;$_FILES&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#91;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;'fichier'&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#91;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;'name'&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #cc66cc;&quot;&gt;-4&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #cc66cc;&quot;&gt;4&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;;&lt;/span&gt;
&amp;nbsp;
  &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;// On vérifie que l'extension est dans le tableau&lt;/span&gt;
  &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;// des extensions autorisées&lt;/span&gt;
  &lt;span style=&quot;color: #b1b100;&quot;&gt;if&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000066;&quot;&gt;in_array&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #ff0000&quot;&gt;$fileExt&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #ff0000&quot;&gt;$extOk&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;
    &lt;span style=&quot;color: #000066;&quot;&gt;die&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;Type de fichier non autorisé.&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;;&lt;/span&gt;
&amp;nbsp;
  &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;// On vérifie si le fichier existe déjà dans le répertoire cible&lt;/span&gt;
  &lt;span style=&quot;color: #b1b100;&quot;&gt;if&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000066;&quot;&gt;file_exists&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #ff0000&quot;&gt;$dstRep&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #ff0000&quot;&gt;$_FILES&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#91;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;'fichier'&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#91;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;'name'&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;
  &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#123;&lt;/span&gt;
    &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;// On utilise une boucle pour incrémenter notre préfixe&lt;/span&gt;
    &lt;span style=&quot;color: #ff0000&quot;&gt;$i&lt;/span&gt; &lt;span style=&quot;color: #66cc66;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #cc66cc;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;;&lt;/span&gt;
    &lt;span style=&quot;color: #b1b100;&quot;&gt;while&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000066;&quot;&gt;file_exists&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #ff0000&quot;&gt;$dstRep&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #ff0000&quot;&gt;$i&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;'_'&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #ff0000&quot;&gt;$_FILES&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#91;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;'fichier'&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#91;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;'name'&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #ff0000&quot;&gt;$i&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;++&lt;/span&gt;
&amp;nbsp;
    &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;// Arrivé ici, on a trouvé un nom disponible avec un préfixe numérique&lt;/span&gt;
    &lt;span style=&quot;color: #ff0000&quot;&gt;$cible&lt;/span&gt; &lt;span style=&quot;color: #66cc66;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #ff0000&quot;&gt;$dstRep&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #ff0000&quot;&gt;$i&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;'_'&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #ff0000&quot;&gt;$_FILES&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#91;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;'fichier'&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#91;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;'name'&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;;&lt;/span&gt;
  &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#125;&lt;/span&gt;
  &lt;span style=&quot;color: #b1b100;&quot;&gt;else&lt;/span&gt;
  &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#123;&lt;/span&gt;
    &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;// Le fichier n'existe pas dans le répertoire cible,&lt;/span&gt;
    &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;// Nous pouvons donc utiliser le nom original&lt;/span&gt;
    &lt;span style=&quot;color: #ff0000&quot;&gt;$cible&lt;/span&gt; &lt;span style=&quot;color: #66cc66;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #ff0000&quot;&gt;$dstRep&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #ff0000&quot;&gt;$_FILES&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#91;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;'fichier'&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#91;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;'name'&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;;&lt;/span&gt;
  &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#125;&lt;/span&gt;
&amp;nbsp;
  &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;// On finit par déplacer le fichier dans le répertoire cible&lt;/span&gt;
  &lt;span style=&quot;color: #000066;&quot;&gt;move_uploaded_file&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #ff0000&quot;&gt;$_FILES&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#91;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;'fichier'&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#91;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;'tmp_name'&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #ff0000&quot;&gt;$cible&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;;&lt;/span&gt;
&amp;nbsp;
&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;?&amp;gt;&lt;/span&gt;&lt;/pre&gt;


&lt;p&gt;Le système que nous venons de voir ne permet que de tester les extensions d'une longueur de trois caractère et nécessitera quelques modifications pour tester des extensions plus longue. Cependant, par convention, on se limite à 3 caractères pour l'extension d'un fichier.&lt;/p&gt;


&lt;h3&gt;Conclusion&lt;/h3&gt;

&lt;p&gt;Nous avons vu l'upload de fichiers avec PHP et quelques test pour aller un peu plus loin. Permettez moi un conseil, restez prudent avec l'envoie de fichiers car ceci peu ouvrir d'énormes failles sur vos sites. N'hésitez donc pas mettre en œuvre le maximum de test de sécurité sur les données envoyées.&lt;/p&gt;</description>
    
    
    
          <comments>http://www.code-web.org/post/2008/01/14/40-upload-de-fichiers-via-formulaire-avec-php#comment-form</comments>
      <wfw:comment>http://www.code-web.org/post/2008/01/14/40-upload-de-fichiers-via-formulaire-avec-php#comment-form</wfw:comment>
      <wfw:commentRss>http://www.code-web.org/feed/rss2/comments/25</wfw:commentRss>
    						<h3>Abonnement aux commentaires</h3>
						<p>
							<a href="http://www.code-web.org/subscribetocomments?post_id=25">
								<!-- # If the subscriber is logged in -->
																<!-- # If the subscriber is not logged in -->
																	S'abonner pour recevoir les commentaires suivants par email															</a>
						</p>
					  </item>
    
  <item>
    <title>Les Pseudo Frames</title>
    <link>http://www.code-web.org/post/2007/12/27/15-les-pseudo-frames</link>
    <guid isPermaLink="false">urn:md5:ebe173b4a57ff70396beb2a8d9d5cabd</guid>
    <pubDate>Thu, 27 Dec 2007 20:18:00 +0000</pubDate>
    <dc:creator>Thierry Geindre</dc:creator>
        <category>PHP</category>
        <category>accessibilité</category><category>html</category><category>php</category><category>référencement</category>    
    <description>&lt;p&gt;Vous connaissez peut-être les frames. Il s'agit d'un système fournit par le langage html permettant de découper une page en plusieurs autres pages. Bien que présentant un certain nombres d'avantages, ce système apporte également son lot d'inconvénients qui nous pousserons à utiliser une autre méthode que constituent les pseudo frames.&lt;/p&gt;    &lt;h3&gt;Pourquoi utiliser des frames?&lt;/h3&gt;

&lt;p&gt;Comme je le disais, les frames permettent de mettre en place un découpage d'une page en plusieurs autres. Ceci permet de régler certains problèmes de maintenances d'un site. En effet lorsqu'un menu apparaît sur de nombreuses pages, il peut être fastidieux de le modifier sur chacune de ces pages.&lt;/p&gt;


&lt;p&gt;Grâce aux frames, il est possible de n'avoir ce menu que sur une seule page. Les autres pages du site seront toujours affichées accompagnées de ce menu grâce à un système de cadres.&lt;/p&gt;


&lt;p&gt;Cependant, les frames posent un certain nombre de problèmes.&lt;/p&gt;


&lt;h3&gt;Pourquoi ne pas utiliser les frames?&lt;/h3&gt;

&lt;p&gt;En règle générale, il est bon de développer un site Web en respectant une norme la plus strict et la plus récente possible. Le &lt;a href=&quot;http://www.la-grange.net/w3c/xhtml1/&quot; hreflang=&quot;fr&quot;&gt;XHTML 1.0 Strict&lt;/a&gt; est un bon exemple, et cette norme interdit l'utilisation des frames ou frameset. Ceci n'explique pas pourquoi faut-il se priver des frames mais constitue déjà un premier argument.&lt;/p&gt;


&lt;p&gt;Les frames posent tout une série de problèmes&amp;nbsp;:&lt;/p&gt;

&lt;h4&gt;Des problèmes d'accessibilité&lt;/h4&gt;

&lt;p&gt;Un certain nombre de navigateurs sont incapables d'interpréter les frames. En les utilisant, vous réduisez ainsi le nombre potentiel de visiteurs que votre site pourrait accueillir.&lt;/p&gt;

&lt;h4&gt;De référencement&lt;/h4&gt;

&lt;p&gt;On sait aujourd'hui que les bot des différents moteurs de recherche existant sur le Web ne sont pas vraiment friant des frames. De plus, un visiteur qui cliquera sur un résultat d'une recherche pourra arriver directement sur une page sans avoir les frames affichées. Il perdra ainsi le menu et, là, aucune chance pour qu'il visite d'autre pages de votre site.&lt;/p&gt;


&lt;p&gt;Il existe bien un moyen de régler ce second problème à l'aide de java script, mais on en revient alors à des problèmes d'accessibilité.&lt;/p&gt;


&lt;p&gt;Les frames posent encore d'autre problèmes, une petite recherche vous en donnera certainement la liste complète. Nous avons vu ici les principaux.&lt;/p&gt;


&lt;h3&gt;Les pseudo frames&lt;/h3&gt;

&lt;p&gt;Pour palier aux problèmes des frames, il nous faut une solution qui apporte les mêmes avantages sans les inconvénients bien sur. C'est justement là qu'interviennent les pseudo frames. Il s'agit d'un système qui consiste à construire une page à partir de plusieurs fichiers mais côté serveur uniquement. Ainsi, le mécanisme est totalement invisible pour le client qui ne verra qu'une page correctement construite et sans frame.&lt;/p&gt;


&lt;p&gt;Pour ce faire, nous allons devoir utiliser un langage côté serveur. PHP satisfera parfaitement ce besoin, mais il faut tout de même savoir que n'importe quel autre langage côté serveur pourra faire l'affaire suivant le même principe.&lt;/p&gt;


&lt;h4&gt;Fichiers et arborescence&lt;/h4&gt;

&lt;p&gt;Classiquement, on retrouve pour une page quatre parties biens distinctes&amp;nbsp;:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;L'en-tête (header)&lt;/li&gt;
&lt;li&gt;Le pied de page (footer)&lt;/li&gt;
&lt;li&gt;Le menu&lt;/li&gt;
&lt;li&gt;Le contenu de la page&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;On aura donc 3 fichiers qui seront systématiquement les mêmes pour toutes les pages d'un site, à savoir&amp;nbsp;:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;header.html (L'en-tête)&lt;/li&gt;
&lt;li&gt;footer.html (Le pied de page)&lt;/li&gt;
&lt;li&gt;menu.html (Le menu)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Et un quatrième fichier sera utilisé pour construire une page, il ne s'agira pas du même fichier en fonction de la page que l'on souhaite afficher. Pour structurer un peu l'arborescence de notre site, ou pourra créer deux répertoires. L'un contenant les fichier communs à toute les pages du site et l'autre contenant les différents fichiers qui contiendront les contenus des pages. Ces répertoires pourront être nommés respectivement includes (du verbe To Include (inclure) de l'anglais) et pages.&lt;/p&gt;


&lt;p&gt;En plus de ces répertoires et fichiers, il nous faudra un fichier supplémentaire qui contiendra les instructions de constructions de la page à partir des différents fichiers. Nous utiliserons pour cela un fichier index.php placé à la racine de notre arborescence.&lt;/p&gt;


&lt;p&gt;On obtiendra donc l'arborescence suivante&amp;nbsp;:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;index.php&lt;/li&gt;
&lt;li&gt;includes/
&lt;ul&gt;
&lt;li&gt;header.html&lt;/li&gt;
&lt;li&gt;footer.html&lt;/li&gt;
&lt;li&gt;menu.html&lt;/li&gt;
&lt;/ul&gt;&lt;/li&gt;
&lt;li&gt;pages/
&lt;ul&gt;
&lt;li&gt;accueil.html&lt;/li&gt;
&lt;li&gt;autre.html&lt;/li&gt;
&lt;li&gt;etc.html&lt;/li&gt;
&lt;/ul&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Bien entendu, d'autres arborescences pourront être mises en place mais celle-ci satisfera la majorité des cas.&lt;/p&gt;


&lt;h4&gt;Contenu des fichiers&lt;/h4&gt;

&lt;p&gt;Excepté le fichier index.php, tout les autres fichiers devront être remplis avec le code html qui compose les pages de votre site. Cependant, vous ne devrez pas y placer exactement le même contenu que si vous aviez utilisé des frames. En effet, il ne faudra pas placer les en-têtes de pages dans chacun des fichiers mais seulement dans header.html. Un exemple sera certainement plus parlant&amp;nbsp;:&lt;/p&gt;


&lt;p&gt;includes/header.html&lt;/p&gt;
&lt;pre class=&quot;html&quot;&gt;&lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;lt;&lt;/span&gt;?xml &lt;span style=&quot;color: #000066;&quot;&gt;version&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;1.0&amp;quot;&lt;/span&gt; encoding&lt;span style=&quot;color: #66cc66;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;UTF-8&amp;quot;&lt;/span&gt;?&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
﻿&lt;span style=&quot;color: #00bbdd;&quot;&gt;&amp;lt;!DOCTYPE html PUBLIC &amp;quot;-//W3C//DTD XHTML 1.0 Strict//EN&amp;quot; &amp;quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd&amp;quot;&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;lt;html&lt;/span&gt; xmlns&lt;span style=&quot;color: #66cc66;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;http://www.w3.org/1999/xhtml&amp;quot;&lt;/span&gt; xml:&lt;span style=&quot;color: #000066;&quot;&gt;lang&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;fr&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: #000066;&quot;&gt;lang&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;fr&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;lt;head&amp;gt;&lt;/span&gt;&lt;/span&gt;
  &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;lt;title&amp;gt;&lt;/span&gt;&lt;/span&gt;Le titre de la page&lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;lt;/title&amp;gt;&lt;/span&gt;&lt;/span&gt;
  &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;lt;link&lt;/span&gt; &lt;span style=&quot;color: #000066;&quot;&gt;rel&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;stylesheet&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: #000066;&quot;&gt;type&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;text/css&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: #000066;&quot;&gt;href&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;/dotclear/themes/CodeWeb/style.css&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: #000066;&quot;&gt;media&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;screen&amp;quot;&lt;/span&gt; 
&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;lt;/head&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;lt;body&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;


&lt;p&gt;Comme nous l'avons déjà vu, notre script index.php vas se charger de construire &lt;strong&gt;une seule page&lt;/strong&gt; à partir des fichiers que nous avons énuméré. Il ne s'agit donc plus de créer plusieurs pages et de les afficher dans des cadres. Ainsi, le code d'en-tête (&amp;lt;head&amp;gt;) ne devra se trouver que dans le fichier header.html et dans aucun autre. De même, le code qui sera destiné à fermer le &amp;lt;body&amp;gt; et &amp;lt;html&amp;gt; ne devra se trouver que dans le fichier footer.html.&lt;/p&gt;


&lt;p&gt;includes/footer.html&lt;/p&gt;
&lt;pre class=&quot;html&quot;&gt;&lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;


&lt;p&gt;Notre menu pourra être formé d'une simple liste&amp;nbsp;:&lt;/p&gt;


&lt;p&gt;includes/menu.html&lt;/p&gt;
&lt;pre class=&quot;html&quot;&gt;&lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;lt;ul&lt;/span&gt; &lt;span style=&quot;color: #000066;&quot;&gt;id&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;menu&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
  &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;lt;li&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;lt;a&lt;/span&gt; &lt;span style=&quot;color: #000066;&quot;&gt;href&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;index.php&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;Accueil&lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;lt;/a&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;lt;/li&amp;gt;&lt;/span&gt;&lt;/span&gt;
  &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;lt;li&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;lt;a&lt;/span&gt; &lt;span style=&quot;color: #000066;&quot;&gt;href&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;index.php?page=autre&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;Autre&lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;lt;/a&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;lt;/li&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;lt;/ul&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;


&lt;p&gt;Il ne nous restera ensuite plus qu'a créer autant de pages que nous le souhaitons dans le répertoire pages. Le minimum serait une page index.html qui sera celle qui sera affichée par défaut. C'est à dire lorsque la page demandée au script index.php ne correspond à aucune page connue ou lorsqu'aucune page n'est demandée. On créera ici une page supplémentaire nommée autre.html pour coller au menu que je vous proposé en exemple.&lt;/p&gt;


&lt;p&gt;pages/index.html&lt;/p&gt;
&lt;pre class=&quot;html&quot;&gt;&lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;lt;h1&amp;gt;&lt;/span&gt;&lt;/span&gt;Accueil&lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;lt;/h1&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;lt;p&amp;gt;&lt;/span&gt;&lt;/span&gt;Bienvenue sur l'accueil.&lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;


&lt;p&gt;pages/autre.html&lt;/p&gt;
&lt;pre class=&quot;html&quot;&gt;&lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;lt;h1&amp;gt;&lt;/span&gt;&lt;/span&gt;Autre&lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;lt;/h1&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;lt;p&amp;gt;&lt;/span&gt;&lt;/span&gt;Une autre page.&lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;


&lt;h4&gt;Construction de la page&lt;/h4&gt;

&lt;p&gt;Comme nous l'avons vu, c'est le script index.php placé à la racine de notre arborescence qui sera chargé de construire une page à partir de tout les fichiers dont il dispose. Ainsi, pour afficher une page on fera toujours appel au fichier index.php avec un paramètre page dans l'URL (méthode GET). On pourra ensuite lire ce paramètre dans notre script via la variable $_GET['page'].&lt;/p&gt;


&lt;p&gt;Par exemple, pour afficher la page autre.html, on utilisera le lien suivant&amp;nbsp;: http://www.site.com/index.php?page=autre Il s'agit du second lien proposé dans le menu d'exemple.&lt;/p&gt;


&lt;p&gt;index.php&lt;/p&gt;
&lt;pre class=&quot;php&quot;&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;lt;?php&lt;/span&gt;
&amp;nbsp;
  &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;// On définit une liste de pages autorisées au moyen d'un tableau&lt;/span&gt;
  &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;// Vous devrez ajouter toutes les pages se trouvant dans le répertoire&lt;/span&gt;
  &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;// pages pour les rendre accessibles.&lt;/span&gt;
  &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;// Vous pouvez également automatiser la génération de ce tableau&lt;/span&gt;
  &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;// en lisant le contenu du répertoire pages/&lt;/span&gt;
  &lt;span style=&quot;color: #ff0000&quot;&gt;$pagesOk&lt;/span&gt; &lt;span style=&quot;color: #66cc66;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #000066;&quot;&gt;array&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;'index'&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;'autre'&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;;&lt;/span&gt;
&amp;nbsp;
  &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;// On commence par lire la page demandée&lt;/span&gt;
  &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;// Si ce n'est pas vide&lt;/span&gt;
  &lt;span style=&quot;color: #b1b100;&quot;&gt;if&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;!&lt;/span&gt;&lt;span style=&quot;color: #000066;&quot;&gt;empty&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #ff0000&quot;&gt;$_GET&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#91;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;'page'&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;
    &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;// On lit la valeur demandée&lt;/span&gt;
    &lt;span style=&quot;color: #ff0000&quot;&gt;$page&lt;/span&gt; &lt;span style=&quot;color: #66cc66;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #ff0000&quot;&gt;$_GET&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#91;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;'page'&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;;&lt;/span&gt;
  &lt;span style=&quot;color: #b1b100;&quot;&gt;else&lt;/span&gt;
   &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;// Sinon on utiliser index comme page par défaut&lt;/span&gt;
   &lt;span style=&quot;color: #ff0000&quot;&gt;$page&lt;/span&gt; &lt;span style=&quot;color: #66cc66;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;'index'&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;;&lt;/span&gt;
&amp;nbsp;
  &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;// On vérifie que la page demandée se trouve dans les pages autorisées&lt;/span&gt;
  &lt;span style=&quot;color: #b1b100;&quot;&gt;if&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;!&lt;/span&gt;&lt;span style=&quot;color: #000066;&quot;&gt;in_array&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #ff0000&quot;&gt;$page&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #ff0000&quot;&gt;$pagesOk&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;
  &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#123;&lt;/span&gt;
&amp;nbsp;
    &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;// Elle n'y est pas, dans ce cas on peut renvoyer une erreur 404&lt;/span&gt;
    &lt;span style=&quot;color: #000066;&quot;&gt;header&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;HTTP/1.0 404 Not Found&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;;&lt;/span&gt;
&amp;nbsp;
    &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;// On stop l'exécution du script ici&lt;/span&gt;
    &lt;span style=&quot;color: #000066;&quot;&gt;exit&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;;&lt;/span&gt;
&amp;nbsp;
  &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#125;&lt;/span&gt;
&amp;nbsp;
  &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;// Arrivé ici, on sait que la page demandée existe forcément.&lt;/span&gt;
  &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;// On va donc pouvoir commencer la construction de la page.&lt;/span&gt;
&amp;nbsp;
  &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;// On commence par inclure l'entête&lt;/span&gt;
  &lt;span style=&quot;color: #b1b100;&quot;&gt;require&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;'./includes/header.html'&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;;&lt;/span&gt;
&amp;nbsp;
  &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;// Puis le menu&lt;/span&gt;
  &lt;span style=&quot;color: #b1b100;&quot;&gt;require&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;'./includes/menu.html'&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;;&lt;/span&gt;
&amp;nbsp;
  &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;// On inclut la page demandée&lt;/span&gt;
  &lt;span style=&quot;color: #b1b100;&quot;&gt;require&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;'./pages/'&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #ff0000&quot;&gt;$page&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;'.html'&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;;&lt;/span&gt;
&amp;nbsp;
  &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;// On inclut enfin le pied de page&lt;/span&gt;
  &lt;span style=&quot;color: #b1b100;&quot;&gt;require&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;'./i