<?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 - apache</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>De belles URL sous Dotclear 2</title>
    <link>http://www.code-web.org/post/2008/03/31/Reecriture-dURL-sous-Dotclear-2</link>
    <guid isPermaLink="false">urn:md5:b73f58956a06c6ec421317c6b4fc5670</guid>
    <pubDate>Tue, 01 Apr 2008 07:10:00 +0200</pubDate>
    <dc:creator>Thierry Geindre</dc:creator>
        <category>Dotclear</category>
        <category>apache</category><category>dotclear</category><category>référencement</category>    
    <description>&lt;p&gt;On sait aujourd'hui que, pour un meilleur référencement, il est important d'utiliser des URL &lt;em&gt;human friendly&lt;/em&gt;. Vos visiteurs en seront ravis également. Pour cela, Dotclear vous propose les URL au format &lt;em&gt;PATH_INFO&lt;/em&gt;. Nous allons voir dans ce petit billet comment activer les URL au format &lt;em&gt;PATH_INFO&lt;/em&gt; et se débarrasser du vilain &lt;em&gt;index.php&lt;/em&gt;.&lt;/p&gt;    &lt;h3&gt;Réécriture d'URL&lt;/h3&gt;

&lt;p&gt;Ce premier paragraphe ne vous intéressera que si vous souhaitez utiliser la réécriture d'URL, c'est à dire faire disparaître le vilain &lt;em&gt;index.php&lt;/em&gt; de vos URL. Si ce n'est pas le cas, vous pouvez sauter au second paragraphe.&lt;/p&gt;


&lt;p&gt;Dans le répertoire contenant votre Dotclear 2, créez un fichier nommé &lt;em&gt;.htaccess&lt;/em&gt;. Placez-y le contenu suivant&amp;nbsp;:&lt;/p&gt;
&lt;pre&gt;
# Activation du moteur de réécriture
RewriteEngine on

# Si le fichier demandé n'existe pas physiquement
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

# On envoie la demande à l'index de Dotclear
RewriteRule (.*) /index.php/$1 [L]
&lt;/pre&gt;


&lt;p&gt;Si votre Dotclear est situé dans un sous répertoire et que vous souhaitez que votre site soit accessible par un répertoire de plus haut niveau, vous devrez placer le fichier .htaccess que vous venez de créer dans ce répertoire de plus haut niveau puis remplacer la dernière ligne par ces &lt;strong&gt;deux&lt;/strong&gt; lignes&amp;nbsp;:&lt;/p&gt;

&lt;pre&gt;
RewriteRule (.*) /sous-repertoire/index.php/$1 [L]
RewriteRule ^$ /sous-repertoire/index.php   [L]
&lt;/pre&gt;


&lt;p&gt;Enfin, pour que cela fonctionne, vous devrez également activer les URL au format &lt;em&gt;PATH_INFO&lt;/em&gt;.&lt;/p&gt;


&lt;h3&gt;Activer les URL au format &lt;em&gt;PATH_INFO&lt;/em&gt;&lt;/h3&gt;

&lt;p&gt;La première étape consiste tout naturellement à activer ce format d'URL. Pour ce faire, rendez-vous dans les paramètres du blog accessibles via le tableau de bord. Pour l'option &lt;em&gt;Méthode de lecture de l'URL&lt;/em&gt;, choisissez bien sur &lt;em&gt;PATH_INFO&lt;/em&gt;. Comme vous pourrez le voir dans l'aide, l'activation de cette option nécessite également que l'URL de votre blog, définit dans la même page de configuration, se termine par un /.&lt;/p&gt;


&lt;p&gt;Pour ce dernier paramètre, tout dépend de si vous souhaitez utiliser la réécriture d'URL ou pas. Si c'est la cas, vous pouvez indiquer une URL comme suit&amp;nbsp;: http://www.mon-site.com/ suivit d'un éventuel sous répertoire mais toujours terminé par un /.Si vous n'utiliserez pas de réécriture d'URL, saisissez une URL comme suit&amp;nbsp;: http://www.mon-site.com/index.php/ ou avec un sous répertoire&amp;nbsp;: http://www.mon-site.com/sous-repertoire/index.php/.&lt;/p&gt;


&lt;p&gt;Une fois cette nouvelle configuration enregistrée, vous devrez éditer le fichier &lt;em&gt;/inc/config.php&lt;/em&gt; de Dotclear et y dé commenter les dernières lignes suivant les indications que vous trouverez dans ce même fichier.&lt;/p&gt;


&lt;h3&gt;Des problèmes?&lt;/h3&gt;

&lt;p&gt;Assurez-vous d'avoir bien suivit toutes les indications données dans ce billet. Si c'est bien le cas, vous devrez aussi vous assurer que la réécriture d'URL (aussi appelé URL Rewriting) est disponible et active chez votre hébergeur. Vérifiez également que l'emploi de fichiers &lt;em&gt;.htaccess&lt;/em&gt; est permis.&lt;/p&gt;</description>
    
    
    
          <comments>http://www.code-web.org/post/2008/03/31/Reecriture-dURL-sous-Dotclear-2#comment-form</comments>
      <wfw:comment>http://www.code-web.org/post/2008/03/31/Reecriture-dURL-sous-Dotclear-2#comment-form</wfw:comment>
      <wfw:commentRss>http://www.code-web.org/feed/rss2/comments/40</wfw:commentRss>
    						<h3>Abonnement aux commentaires</h3>
						<p>
							<a href="http://www.code-web.org/subscribetocomments?post_id=40">
								<!-- # 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>Évitez les URL multiples et le contenu dupliqué</title>
    <link>http://www.code-web.org/post/2008/03/09/50-evitez-les-url-multiples-et-le-contenu-duplique</link>
    <guid isPermaLink="false">urn:md5:9462aefff84dc422499d664661b4b28e</guid>
    <pubDate>Sun, 09 Mar 2008 16:12:00 +0000</pubDate>
    <dc:creator>Thierry Geindre</dc:creator>
        <category>Référencement</category>
        <category>apache</category><category>dotclear</category><category>référencement</category>    
    <description>&lt;p&gt;L'un des points nuisant au référencement est le contenu dupliqué ou &lt;em&gt;duplicate content&lt;/em&gt; dans le jargon. En effet, plusieurs pages ayant le même contenu sur un même site pénalisent le référencement de ce site. Malheureusement, on trouve très souvent sur un seule site plusieurs URL permettant d'accéder à la page d'accueil. Comme http://www.lesite.com/, http://lesite.com/ ou encore http://www.lesite.come/index.php etc. Nous allons voir comment régler ce problème.&lt;/p&gt;    &lt;p&gt;Plusieurs solutions sont disponibles pour régler ce problème mais toujours du côté du serveur. Nous allons voir le cas d'apache.&lt;/p&gt;


&lt;p&gt;Il est généralement possible de personnaliser le fonctionnement d'Apache pour un répertoire en particulier en plaçant un fichier .htaccess dans ce répertoire. Il suffira alors d'indiquer dans ce fichier de rediriger le visiteur vers la bonne URL lorsque celle qu'il utilise ne nous satisfait pas.&lt;/p&gt;


&lt;p&gt;Pour cela, il faudra d'abord déterminer l'URL que l'on souhaite utiliser. On choisira www.le-site.com, c'est à dire l'URL contenant le &lt;em&gt;www&lt;/em&gt; car c'est la forme la plus connu et la plus employée.&lt;/p&gt;


&lt;p&gt;Nous allons donc créer un fichier nommé .htaccess. Pour ce faire, il suffit d'ouvrir un éditeur de fichiers texte et d'enregistrer le fichier créé sous ce nom. A noter que sous Windows, il n'est pas possible de créer des fichiers portant ce nom. Vous pourrez le renommer une fois présent sur votre FTP ou exploiter la commande &lt;em&gt;rename&lt;/em&gt; qui autorise ce genre de nom. Placez dans ce fichiers les lignes suivantes&amp;nbsp;:&lt;/p&gt;

&lt;pre&gt;
# Activation du moteur de réécriture d'URL
RewriteEngine On

# On insère une condition de réécriture
# Si le nom de serveur indiqué ne commence
# pas par www
RewriteCond %{SERVER_NAME} !^www

# Nous allons rediriger le visiteur vers le site avec
# les www
RewriteRule ^(.*)$  http://www.le-site.comg/$1 [R=301,L]&lt;/pre&gt;


&lt;p&gt;Avec ces lignes, n'importe quelle page à laquelle un client accède via un nom de serveur sans les &lt;em&gt;www&lt;/em&gt; sera automatiquement redirigé vers la même page mais avec le bon nom de serveur. D'autre part, vous aurez peut être remarqué que l'erreur 301 est renvoyée au client. Elle signifie &lt;em&gt;Has moved permanently&lt;/em&gt;, et indique aux clients, les moteurs de recherche en particulier, que la page demandée a définitivement été déplacé vers la nouvelle adresse indiquée.&lt;/p&gt;</description>
    
    
    
          <comments>http://www.code-web.org/post/2008/03/09/50-evitez-les-url-multiples-et-le-contenu-duplique#comment-form</comments>
      <wfw:comment>http://www.code-web.org/post/2008/03/09/50-evitez-les-url-multiples-et-le-contenu-duplique#comment-form</wfw:comment>
      <wfw:commentRss>http://www.code-web.org/feed/rss2/comments/35</wfw:commentRss>
    						<h3>Abonnement aux commentaires</h3>
						<p>
							<a href="http://www.code-web.org/subscribetocomments?post_id=35">
								<!-- # 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>Déplacement de pages, utiliser l'erreur 301</title>
    <link>http://www.code-web.org/post/2007/11/13/24-deplacement-de-pages-utiliser-l-erreur-301</link>
    <guid isPermaLink="false">urn:md5:0737bc93ac68df6ca735f4fc372b9fd5</guid>
    <pubDate>Tue, 13 Nov 2007 08:24:00 +0000</pubDate>
    <dc:creator>Thierry Geindre</dc:creator>
        <category>Référencement</category>
        <category>apache</category><category>php</category><category>référencement</category>    
    <description>&lt;p&gt;CodeWeb change d'ici quelques jours de nom de domaine et de serveur. Le site deviendra alors code-web.org. C'est justement l'occasion de faire le point sur les bonnes choses à faire en cas de déplacement de pages. Je pense notamment à l'erreur HTTP 301 et la redirection vers le nouvel emplacement. L'objectif étant bien sur de conserver le référencement acquis et surtout de ne pas créer de liens morts.&lt;/p&gt;    &lt;p&gt;Nous prendrons pour exemple notre cas. Vous verrez donc les quelques éléments que nous avons préparés et que nous allons mettre en place dès que le nouveau nom de domaine sera opérationnel. Mais avant tout, voyons l'objectif à atteindre.&lt;/p&gt;


&lt;h3&gt;Objectif&lt;/h3&gt;

&lt;p&gt;Lorsque vous créez un site sur la toile, il est possible qu'avec le temps des liens externes existent vers vos pages. Les moteurs de recherche constituent l'exemple le plus évident de liens externes. Mais pensons aussi aux liens dans les favoris de vos visiteurs, etc. L'ennui lorsque vous souhaitez déplacer vos pages, c'est que vous n'avez pas le contrôle sur les liens dont nous venons de parler. Notre objectif sera donc de faire en sorte que ces liens fonctionnent toujours.&lt;/p&gt;


&lt;h3&gt;Redirection bien sur!&lt;/h3&gt;

&lt;p&gt;En effet, nous utiliserons une redirection. Ici à l'aide de la fonction &lt;a href=&quot;http://fr.php.net/header&quot; hreflang=&quot;fr&quot;&gt;header&lt;/a&gt; de PHP. Pour vos visiteurs, pas de problème ils pourront être redirigés avec ce seul code&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;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;'location: http://www.nouveau-domain.com/'&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;En revanche, les moteurs de recherche ne vont certainement pas apprécier et vous risquez de perdre le référencement de vos pages. Pou remédier à cela, il faudra renvoyer une erreur 301 du protocole HTTP. Ce code correspond à l'erreur &quot;Moved Permanently&quot;, comprenez &quot;Déplacé définitivement&quot;. La redirection qui suivra cette erreur prendra donc tout son sens pour un moteur de recherche qui pourra ainsi mettre à jour son index.&lt;/p&gt;


&lt;h3&gt;L'exemple&lt;/h3&gt;

&lt;p&gt;Prenons donc CodeWeb comme exemple. Remarquez que les adresses de nos pages sont actuellement construites ainsi&amp;nbsp;:&lt;/p&gt;


&lt;pre&gt;http://codeweb.free.fr/dotclear/index.php/2007/11/12/24-deplacement-de-pages-utiliser-l-erreur-301&lt;/pre&gt;


&lt;p&gt;Sur notre nouveau nom de domaine elles seront formées ainsi&amp;nbsp;:&lt;/p&gt;


&lt;pre&gt;http://www.code-web.org/index.php/2007/11/12/24-deplacement-de-pages-utiliser-l-erreur-301&lt;/pre&gt;


&lt;p&gt;On peut donc constater qu'elle possède une partie commune. C'est la partie de l'URL qui est totalement indépendant du nom de domaine. C'est celle-ci que nous allons récupérer pour effectuer la redirection vers la bonne page. Nous remplacerons donc le fichiers index.php actuellement en place dans le répertoire /dotclear/ par le script 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: #808080; font-style: italic;&quot;&gt;// On créé la nouvelle URL à partir de l'ancienne que l'on vient de récupérer&lt;/span&gt;
	&lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;// via $_SERVER['REQUEST_URI']&lt;/span&gt;
	&lt;span style=&quot;color: #ff0000&quot;&gt;$link&lt;/span&gt; &lt;span style=&quot;color: #66cc66;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;'http://www.code-web.org/'&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;'/dotclear/'&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;$_SERVER&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#91;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;'REQUEST_URI'&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: #808080; font-style: italic;&quot;&gt;// L'erreur 301&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.1 301 Moved Permanently&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;// La redirection&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;'location: '&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #ff0000&quot;&gt;$link&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 indique qu'il n'y a plus rien à voir ici&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;Connection: close&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: #000000; font-weight: bold;&quot;&gt;?&amp;gt;&lt;/span&gt;&lt;/pre&gt;


&lt;p&gt;Ainsi, lorsqu'un visiteur tentera de se rendre sur http://codeweb.free.fr/dotclear/index.php il sera automatiquement redirigé ver http://www.code-web.org/index.php. De même pour les moteurs de recherche qui pourront, en plus, mettre a jour leur index avec les nouvelles adresses.&lt;/p&gt;</description>
    
    
    
          <comments>http://www.code-web.org/post/2007/11/13/24-deplacement-de-pages-utiliser-l-erreur-301#comment-form</comments>
      <wfw:comment>http://www.code-web.org/post/2007/11/13/24-deplacement-de-pages-utiliser-l-erreur-301#comment-form</wfw:comment>
      <wfw:commentRss>http://www.code-web.org/feed/rss2/comments/9</wfw:commentRss>
    						<h3>Abonnement aux commentaires</h3>
						<p>
							<a href="http://www.code-web.org/subscribetocomments?post_id=9">
								<!-- # 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>
    
</channel>
</rss>