PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, January 15, 2022

[FIXED] Localhost and fopen

 January 15, 2022     codeigniter, fopen, mamp     No comments   

Issue

We are now working in a local environment, rather than working live, which is a good thing. However, I've run into a hiccup I don't know how to fix. My code (CodeIgniter) looks like this:

if (!is_dir(base_url() . 'channel-partners/html/camera-registration/' . $name_url)) {
        mkdir(base_url() . 'channel-partners/html/camera-registration/' . $name_url);
}

$handle = fopen('/path/to/file/channel-partners/html/camera-registration/' . $name_url . '/index.html', 'w') or die('Can\'t open file');
fwrite($handle, $html);
fclose($handle);

But since I'm running MAMP, $_SERVER['DOCUMENT_ROOT'] equals Applications/MAMP/htdocs, so that won't work. So what can I do to write a file in PHP that would work in both a production environment and a local environment?


Solution

Use the FCPATH or APPPATH constants.

FCPATH will give you the path to the directory where the front controller is located (index.php).

APPPATH will give you the path to the application directory.

Usage:

fopen(FCPATH . 'path/relative/to/frontcontroller/etc/' . $name_url . '/index.html', 'w') ... 


Answered By - Brendan
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing