{"id":51,"date":"2017-07-17T15:26:08","date_gmt":"2017-07-17T13:26:08","guid":{"rendered":"https:\/\/numa-bord.com\/miniblog\/?p=51"},"modified":"2017-07-19T09:34:44","modified_gmt":"2017-07-19T07:34:44","slug":"php-creer-fichier-ecrire","status":"publish","type":"post","link":"https:\/\/numa-bord.com\/miniblog\/php-creer-fichier-ecrire\/","title":{"rendered":"PHP : Cr\u00e9er un fichier et \u00e9crire dedans"},"content":{"rendered":"<p>Exemple de code permettant de cr\u00e9er et d&rsquo;\u00e9crire dans un fichier en PHP. Un des cas courant de ce besoins est la cr\u00e9ation de fichiers de configuration. Le code suivant permet de cr\u00e9er le fichier \u00ab\u00a0config.db.php\u00a0\u00bb qui comme son nom l&rsquo;indiquer contiendra les constantes PHP permettant la connexion \u00e0 la base de donn\u00e9es.<\/p>\n<p>Imaginons qu&rsquo;il s&rsquo;agit d&rsquo;une partie d&rsquo;un installeur de site. On demande de remplir les informations de connexion \u00e0 l&rsquo;utilisateur dans un formulaire classique. Une fois soumis on contr\u00f4le les champs, on v\u00e9rifie que la connexion fonctionne et dans ce cas, on cr\u00e9er le fichier de configuration.<\/p>\n<p>Dans le code est utilis\u00e9 le tableau $fields sens\u00e9 contenir les informations envoy\u00e9 par le formulaire (et pr\u00e9alablement contr\u00f4l\u00e9)<\/p>\n<div class=\"codecolorer-container php default\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"php codecolorer\"><span class=\"kw2\">&lt;?php<\/span><br \/>\n<span class=\"coMULTI\">\/*...traitement du formulaire post\u00e9 retournant le tableau $fields...*\/<\/span><br \/>\n<span class=\"co1\">\/\/nom du fichier \u00e0 cr\u00e9er.<\/span><br \/>\n<span class=\"re0\">$filename<\/span> <span class=\"sy0\">=<\/span> <span class=\"st_h\">'config.db.php'<\/span><span class=\"sy0\">;<\/span><br \/>\n<span class=\"co1\">\/\/creation du fichier si il n'existe pas et ouverture en \u00e9criture<\/span><br \/>\n<span class=\"re0\">$conf_file<\/span> <span class=\"sy0\">=<\/span> <a href=\"http:\/\/www.php.net\/fopen\"><span class=\"kw3\">fopen<\/span><\/a><span class=\"br0\">&#40;<\/span><span class=\"re0\">$filename<\/span><span class=\"sy0\">,<\/span> <span class=\"st_h\">'w'<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n<span class=\"co1\">\/\/on \u00e9crit dans le fichier ligne par ligne<\/span><br \/>\n<a href=\"http:\/\/www.php.net\/fwrite\"><span class=\"kw3\">fwrite<\/span><\/a><span class=\"br0\">&#40;<\/span><span class=\"re0\">$conf_file<\/span><span class=\"sy0\">,<\/span> <span class=\"st_h\">'&lt;?php'<\/span> <span class=\"sy0\">.<\/span> <span class=\"st0\">&quot;<span class=\"es1\">\\r<\/span><span class=\"es1\">\\n<\/span>&quot;<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n<a href=\"http:\/\/www.php.net\/fwrite\"><span class=\"kw3\">fwrite<\/span><\/a><span class=\"br0\">&#40;<\/span><span class=\"re0\">$conf_file<\/span><span class=\"sy0\">,<\/span> <span class=\"st_h\">'define(&quot;DB_HOST&quot;,&quot;'<\/span> <span class=\"sy0\">.<\/span> <span class=\"re0\">$fields<\/span><span class=\"br0\">&#91;<\/span><span class=\"st_h\">'host'<\/span><span class=\"br0\">&#93;<\/span> <span class=\"sy0\">.<\/span> <span class=\"st_h\">'&quot;);'<\/span> <span class=\"sy0\">.<\/span> <span class=\"st0\">&quot;<span class=\"es1\">\\r<\/span><span class=\"es1\">\\n<\/span>&quot;<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n<a href=\"http:\/\/www.php.net\/fwrite\"><span class=\"kw3\">fwrite<\/span><\/a><span class=\"br0\">&#40;<\/span><span class=\"re0\">$conf_file<\/span><span class=\"sy0\">,<\/span> <span class=\"st_h\">'define(&quot;DB_USERNAME&quot;,&quot;'<\/span> <span class=\"sy0\">.<\/span> <span class=\"re0\">$fields<\/span><span class=\"br0\">&#91;<\/span><span class=\"st_h\">'username'<\/span><span class=\"br0\">&#93;<\/span> <span class=\"sy0\">.<\/span> <span class=\"st_h\">'&quot;);'<\/span> <span class=\"sy0\">.<\/span> <span class=\"st0\">&quot;<span class=\"es1\">\\r<\/span><span class=\"es1\">\\n<\/span>&quot;<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n<a href=\"http:\/\/www.php.net\/fwrite\"><span class=\"kw3\">fwrite<\/span><\/a><span class=\"br0\">&#40;<\/span><span class=\"re0\">$conf_file<\/span><span class=\"sy0\">,<\/span> <span class=\"st_h\">'define(&quot;DB_PASSWORD&quot;,&quot;'<\/span> <span class=\"sy0\">.<\/span> <span class=\"re0\">$fields<\/span><span class=\"br0\">&#91;<\/span><span class=\"st_h\">'password'<\/span><span class=\"br0\">&#93;<\/span> <span class=\"sy0\">.<\/span> <span class=\"st_h\">'&quot;);'<\/span> <span class=\"sy0\">.<\/span> <span class=\"st0\">&quot;<span class=\"es1\">\\r<\/span><span class=\"es1\">\\n<\/span>&quot;<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n<a href=\"http:\/\/www.php.net\/fwrite\"><span class=\"kw3\">fwrite<\/span><\/a><span class=\"br0\">&#40;<\/span><span class=\"re0\">$conf_file<\/span><span class=\"sy0\">,<\/span> <span class=\"st_h\">'define(&quot;DB_NAME&quot;,&quot;'<\/span> <span class=\"sy0\">.<\/span> <span class=\"re0\">$fields<\/span><span class=\"br0\">&#91;<\/span><span class=\"st_h\">'name'<\/span><span class=\"br0\">&#93;<\/span> <span class=\"sy0\">.<\/span> <span class=\"st_h\">'&quot;);'<\/span> <span class=\"sy0\">.<\/span> <span class=\"st0\">&quot;<span class=\"es1\">\\r<\/span><span class=\"es1\">\\n<\/span>&quot;<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n<span class=\"co1\">\/\/fermeture du fichier<\/span><br \/>\n<a href=\"http:\/\/www.php.net\/fclose\"><span class=\"kw3\">fclose<\/span><\/a><span class=\"br0\">&#40;<\/span><span class=\"re0\">$conf_file<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Exemple de code permettant de cr\u00e9er et d&rsquo;\u00e9crire dans un fichier en PHP. Un des cas courant de ce besoins est la cr\u00e9ation de fichiers de configuration. Le code suivant permet de cr\u00e9er le fichier \u00ab\u00a0config.db.php\u00a0\u00bb qui comme son nom l&rsquo;indiquer contiendra les constantes PHP permettant la connexion \u00e0 la base de donn\u00e9es. Imaginons qu&rsquo;il [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[2,7],"tags":[],"class_list":["post-51","post","type-post","status-publish","format-standard","hentry","category-developpement","category-php"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.2 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>PHP : Cr\u00e9er un fichier et \u00e9crire dedans - Pense b\u00eate d&#039;un d\u00e9veloppeur web<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/numa-bord.com\/miniblog\/php-creer-fichier-ecrire\/\" \/>\n<meta property=\"og:locale\" content=\"fr_FR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"PHP : Cr\u00e9er un fichier et \u00e9crire dedans - Pense b\u00eate d&#039;un d\u00e9veloppeur web\" \/>\n<meta property=\"og:description\" content=\"Exemple de code permettant de cr\u00e9er et d&rsquo;\u00e9crire dans un fichier en PHP. Un des cas courant de ce besoins est la cr\u00e9ation de fichiers de configuration. Le code suivant permet de cr\u00e9er le fichier \u00ab\u00a0config.db.php\u00a0\u00bb qui comme son nom l&rsquo;indiquer contiendra les constantes PHP permettant la connexion \u00e0 la base de donn\u00e9es. Imaginons qu&rsquo;il [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/numa-bord.com\/miniblog\/php-creer-fichier-ecrire\/\" \/>\n<meta property=\"og:site_name\" content=\"Pense b\u00eate d&#039;un d\u00e9veloppeur web\" \/>\n<meta property=\"article:published_time\" content=\"2017-07-17T13:26:08+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2017-07-19T07:34:44+00:00\" \/>\n<meta name=\"author\" content=\"Numa\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u00c9crit par\" \/>\n\t<meta name=\"twitter:data1\" content=\"Numa\" \/>\n\t<meta name=\"twitter:label2\" content=\"Dur\u00e9e de lecture estim\u00e9e\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/numa-bord.com\/miniblog\/php-creer-fichier-ecrire\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/numa-bord.com\/miniblog\/php-creer-fichier-ecrire\/\"},\"author\":{\"name\":\"Numa\",\"@id\":\"https:\/\/numa-bord.com\/miniblog\/#\/schema\/person\/f9d00acd1703f17e5a6895283eb46a7e\"},\"headline\":\"PHP : Cr\u00e9er un fichier et \u00e9crire dedans\",\"datePublished\":\"2017-07-17T13:26:08+00:00\",\"dateModified\":\"2017-07-19T07:34:44+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/numa-bord.com\/miniblog\/php-creer-fichier-ecrire\/\"},\"wordCount\":145,\"commentCount\":1,\"articleSection\":[\"D\u00e9veloppement\",\"PHP\"],\"inLanguage\":\"fr-FR\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/numa-bord.com\/miniblog\/php-creer-fichier-ecrire\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/numa-bord.com\/miniblog\/php-creer-fichier-ecrire\/\",\"url\":\"https:\/\/numa-bord.com\/miniblog\/php-creer-fichier-ecrire\/\",\"name\":\"PHP : Cr\u00e9er un fichier et \u00e9crire dedans - Pense b\u00eate d&#039;un d\u00e9veloppeur web\",\"isPartOf\":{\"@id\":\"https:\/\/numa-bord.com\/miniblog\/#website\"},\"datePublished\":\"2017-07-17T13:26:08+00:00\",\"dateModified\":\"2017-07-19T07:34:44+00:00\",\"author\":{\"@id\":\"https:\/\/numa-bord.com\/miniblog\/#\/schema\/person\/f9d00acd1703f17e5a6895283eb46a7e\"},\"breadcrumb\":{\"@id\":\"https:\/\/numa-bord.com\/miniblog\/php-creer-fichier-ecrire\/#breadcrumb\"},\"inLanguage\":\"fr-FR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/numa-bord.com\/miniblog\/php-creer-fichier-ecrire\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/numa-bord.com\/miniblog\/php-creer-fichier-ecrire\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Accueil\",\"item\":\"https:\/\/numa-bord.com\/miniblog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"PHP : Cr\u00e9er un fichier et \u00e9crire dedans\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/numa-bord.com\/miniblog\/#website\",\"url\":\"https:\/\/numa-bord.com\/miniblog\/\",\"name\":\"Pense b\u00eate d&#039;un d\u00e9veloppeur web\",\"description\":\"(php, javascript, Symfony, Wordpress....)\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/numa-bord.com\/miniblog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"fr-FR\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/numa-bord.com\/miniblog\/#\/schema\/person\/f9d00acd1703f17e5a6895283eb46a7e\",\"name\":\"Numa\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"fr-FR\",\"@id\":\"https:\/\/secure.gravatar.com\/avatar\/f21d1af4658a7106211915940584534c1e0b3eef3f12eb67a697686cad70b64a?s=96&d=mm&r=g\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/f21d1af4658a7106211915940584534c1e0b3eef3f12eb67a697686cad70b64a?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/f21d1af4658a7106211915940584534c1e0b3eef3f12eb67a697686cad70b64a?s=96&d=mm&r=g\",\"caption\":\"Numa\"},\"url\":\"https:\/\/numa-bord.com\/miniblog\/author\/negonner\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"PHP : Cr\u00e9er un fichier et \u00e9crire dedans - Pense b\u00eate d&#039;un d\u00e9veloppeur web","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/numa-bord.com\/miniblog\/php-creer-fichier-ecrire\/","og_locale":"fr_FR","og_type":"article","og_title":"PHP : Cr\u00e9er un fichier et \u00e9crire dedans - Pense b\u00eate d&#039;un d\u00e9veloppeur web","og_description":"Exemple de code permettant de cr\u00e9er et d&rsquo;\u00e9crire dans un fichier en PHP. Un des cas courant de ce besoins est la cr\u00e9ation de fichiers de configuration. Le code suivant permet de cr\u00e9er le fichier \u00ab\u00a0config.db.php\u00a0\u00bb qui comme son nom l&rsquo;indiquer contiendra les constantes PHP permettant la connexion \u00e0 la base de donn\u00e9es. Imaginons qu&rsquo;il [&hellip;]","og_url":"https:\/\/numa-bord.com\/miniblog\/php-creer-fichier-ecrire\/","og_site_name":"Pense b\u00eate d&#039;un d\u00e9veloppeur web","article_published_time":"2017-07-17T13:26:08+00:00","article_modified_time":"2017-07-19T07:34:44+00:00","author":"Numa","twitter_card":"summary_large_image","twitter_misc":{"\u00c9crit par":"Numa","Dur\u00e9e de lecture estim\u00e9e":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/numa-bord.com\/miniblog\/php-creer-fichier-ecrire\/#article","isPartOf":{"@id":"https:\/\/numa-bord.com\/miniblog\/php-creer-fichier-ecrire\/"},"author":{"name":"Numa","@id":"https:\/\/numa-bord.com\/miniblog\/#\/schema\/person\/f9d00acd1703f17e5a6895283eb46a7e"},"headline":"PHP : Cr\u00e9er un fichier et \u00e9crire dedans","datePublished":"2017-07-17T13:26:08+00:00","dateModified":"2017-07-19T07:34:44+00:00","mainEntityOfPage":{"@id":"https:\/\/numa-bord.com\/miniblog\/php-creer-fichier-ecrire\/"},"wordCount":145,"commentCount":1,"articleSection":["D\u00e9veloppement","PHP"],"inLanguage":"fr-FR","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/numa-bord.com\/miniblog\/php-creer-fichier-ecrire\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/numa-bord.com\/miniblog\/php-creer-fichier-ecrire\/","url":"https:\/\/numa-bord.com\/miniblog\/php-creer-fichier-ecrire\/","name":"PHP : Cr\u00e9er un fichier et \u00e9crire dedans - Pense b\u00eate d&#039;un d\u00e9veloppeur web","isPartOf":{"@id":"https:\/\/numa-bord.com\/miniblog\/#website"},"datePublished":"2017-07-17T13:26:08+00:00","dateModified":"2017-07-19T07:34:44+00:00","author":{"@id":"https:\/\/numa-bord.com\/miniblog\/#\/schema\/person\/f9d00acd1703f17e5a6895283eb46a7e"},"breadcrumb":{"@id":"https:\/\/numa-bord.com\/miniblog\/php-creer-fichier-ecrire\/#breadcrumb"},"inLanguage":"fr-FR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/numa-bord.com\/miniblog\/php-creer-fichier-ecrire\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/numa-bord.com\/miniblog\/php-creer-fichier-ecrire\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Accueil","item":"https:\/\/numa-bord.com\/miniblog\/"},{"@type":"ListItem","position":2,"name":"PHP : Cr\u00e9er un fichier et \u00e9crire dedans"}]},{"@type":"WebSite","@id":"https:\/\/numa-bord.com\/miniblog\/#website","url":"https:\/\/numa-bord.com\/miniblog\/","name":"Pense b\u00eate d&#039;un d\u00e9veloppeur web","description":"(php, javascript, Symfony, Wordpress....)","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/numa-bord.com\/miniblog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"fr-FR"},{"@type":"Person","@id":"https:\/\/numa-bord.com\/miniblog\/#\/schema\/person\/f9d00acd1703f17e5a6895283eb46a7e","name":"Numa","image":{"@type":"ImageObject","inLanguage":"fr-FR","@id":"https:\/\/secure.gravatar.com\/avatar\/f21d1af4658a7106211915940584534c1e0b3eef3f12eb67a697686cad70b64a?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/f21d1af4658a7106211915940584534c1e0b3eef3f12eb67a697686cad70b64a?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/f21d1af4658a7106211915940584534c1e0b3eef3f12eb67a697686cad70b64a?s=96&d=mm&r=g","caption":"Numa"},"url":"https:\/\/numa-bord.com\/miniblog\/author\/negonner\/"}]}},"_links":{"self":[{"href":"https:\/\/numa-bord.com\/miniblog\/wp-json\/wp\/v2\/posts\/51","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/numa-bord.com\/miniblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/numa-bord.com\/miniblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/numa-bord.com\/miniblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/numa-bord.com\/miniblog\/wp-json\/wp\/v2\/comments?post=51"}],"version-history":[{"count":7,"href":"https:\/\/numa-bord.com\/miniblog\/wp-json\/wp\/v2\/posts\/51\/revisions"}],"predecessor-version":[{"id":75,"href":"https:\/\/numa-bord.com\/miniblog\/wp-json\/wp\/v2\/posts\/51\/revisions\/75"}],"wp:attachment":[{"href":"https:\/\/numa-bord.com\/miniblog\/wp-json\/wp\/v2\/media?parent=51"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/numa-bord.com\/miniblog\/wp-json\/wp\/v2\/categories?post=51"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/numa-bord.com\/miniblog\/wp-json\/wp\/v2\/tags?post=51"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}