August 17, 2017

How to determine absolute path with a tiny php script

I was recently installing the fantastic forms plugin for WordPress, cForms on a new website I was building and ran in to an issue trying to determine what the absolute path was to a specific directory.

Let me digress for a moment… cForms is an immensely powerful forms tool that you can use on your blog to create stunning forms. Multi-page forms, forms that use ajax, forms that enable users to upload files and the features really go on and on. It may be overkill for you if you just need simple contact form type functionality, but if you’re looking for the atom bomb of form generation, cForms is your ticket to paradise. Now back to the story…

cForms was asking me for the absolute path to where I wanted files to be uploaded. So using Fetch (my ftp client of choice) I simply copied the address and pasted it into the cForms field. Well, cForms complained that it was not correct and that it couldn’t find that directory I specified.

After digging a bit I discovered a simple php script that you can insert into an otherwise blank text document. Name it basically anything you want. I named my file, “absolute-path-script.php” and uploaded that file into the directory that I wanted to be the upload directory for cForms. Once you’ve uploaded the file, you simply need to access that php file from your browser. For instance if you put the php file in your wp-content/uploads directory, then you’d point your browser to
“http:// www.YourWonderfulWebsite .com/wp-content/uploads/absolute-path-script.php” and in your browser window will appear the absolute path to that directory.

It’s so simple it hurts.

Here is the code that makes the magic happen. And don’t laugh when you see how short and sweet it is. You’ll thank me later when you realize it saved you many frustrating minutes trying to figure out what your absolute path was because you’re on Blue Host or somewhere like that. By the way this is nothing more than a Unix command that translates to “Get current working directory”.

<?php
echo getcwd();
?>

Hope that helps you out.