JacobTheDev
JacobTheDev

Reputation: 18550

Global Variables Across Files and Folders in PHP

I'm building a PHP CMS, but having trouble with variables. I want to be able to have all the variables in an external file, vars.php, and just include that file in every file that requires the variables. The variables all have to do with URLs and folder paths.

The problem I'm running in to is that if I set the variables to full URLs (ex: $uploadDir = "http://www.example.com/uploads/";), the scripts don't do what they should. Same thing if I use absolute paths (ex: $uploadDir = "/uploads/"; or $uploadDir = "/full/server/path/uploads/";).

If I use full URLs, it seems like it at least tries to work, but doesn't. If I use the full server path or absolute path, I get some error about that path not being allowed. The files are all stored in /edit/ or /edit/(sub-folder-name)/, if that helps.

I'll look around for some code examples where I would be using this, and update this post ASAP.

Thanks.

UPDATE:

Some of the variables from this code snippet aren't included, just didn't think I should post a huge PHP script.

Example of $pageDir:

// The below 2 lines are actually in vars.php, which is included in this file.
$pageDir = "http://www.example.com/edit/pages/";
$url = "http://www.example.com/";

if ($_POST["page"] && $execute == TRUE) {
    $live = $_GET["p"] . ".php";
    // The below line is how this used to be set up.
    // $handle = fopen("pages/$page", "w");
    // The below line is how the new version should be set up.
    $handle = fopen("$pageDir/$page", "w");
    fwrite($handle, $_POST["page"]);
    fclose($handle);
    // The below line is how this used to be set up.
    // echo("<p>Page successfully saved. <a href=\"../$live\" target=\"_blank\">Click here to view this page.</a></p>\n");    
    // The below line is how the new version should be set up.
    echo("<p>Page successfully saved. <a href=\"$url/$live\" target=\"_blank\">Click here to view this page.</a></p>\n");    
    $execute = FALSE;
}

Upvotes: 1

Views: 2639

Answers (2)

netom
netom

Reputation: 3362

What you are trying to do in this snippet is to

Since it's an HTTP URL, you can't write anything to it (you can send POST request some way, but I'm sure you don't want that)

You have to use actual file system paths for file system operations (like storing something in a file ;) )

To do that you might want to use the $_SERVER['DOCUMENT_ROOT'] variable, or you can do a trick:

$my_dir = dirname(__FILE__);

The variable $my_dir then will contain the directory name the current script is in.

Also, you might want to use some database like MySQL or MongoDB (very, very different things) to store contents in. If you don't know these, you should read a few examples, they're great. ;)

Upvotes: 1

mario
mario

Reputation: 145512

Don't use URLs for file-system functions. That will make them do a HTTP request, not load the files from the filesystem.

Also if you use /absolute paths, then that will refer to the filesystem root, not to your webservers document root. PHP filesystem functions do not operate on virtual path names, but on the real system file structure.

You can however use:

 $pageDir = "/edit/pages/";
 readfile("$_SERVER[DOCUMENT_ROOT]/$pageDir/$page");

That's probably what you want.

Other caveats: despite the meme, there is nothing wrong with global vars. You should however not swamp the shared scope with dozens of them. Instead define an array like $dir["page"] for folders. Or define() constants.

Upvotes: 4

Related Questions