Joshua Bakker
Joshua Bakker

Reputation: 2358

PHP to Javascript: dirname and document_root

I got a little problem. I am working on a project and I got my root, then I got 2 folders: website1 and website2.

website1 is the staff panel, where the upload script is on (where this problem is on). website2 is the website the 'customer' will see. Here all the uploaded images, PDFs etc. are uploaded to.

So, I have a few notes before I start:

  1. I cannot use PHP in my Javascript. The Javascript is in .js files and not in my .php file.
  2. I cannot do this at my AJAX request as something client-side has to be done correctly to see the image.

So basically, I am in website1 and when I upload a file (with DropzoneJS (http://www.dropzonejs.com/)) it does a AJAX request. Here it has to upload a file to a directory in website2. In website1 I have to see that image after uploading it in a IMG tag.

So in PHP I got this:

$uploadPath = filter_input(INPUT_POST, 'uploadPath');
$uploadPath = dirname(getenv('DOCUMENT_ROOT')) . '/' . $uploadPath;

This works and the file gets uploaded to that directory. $uploadPath is this (in Javascript, sent as POST parameter in AJAX request):

/SITE2/assets/uploads/

Now I need to translate the dirname and getenv('DOCUMENT_ROOT') into Javascript. I tried document.location.hostname but this does return a different value than getenv('DOCUMENT_ROOT').

document.location.hostname: 127.0.0.1 getenv('DOCUMENT_ROOT'): D:/xampp/htdocs

How can I get the same value as getenv('DOCUMENT_ROOT') in Javascript?

Upvotes: 2

Views: 1576

Answers (3)

jcubic
jcubic

Reputation: 66490

In php file you can create script with JavaScript variable like this:

<script>
var DOCUMENT_ROOT = "<?= getenv('DOCUMENT_ROOT') ?>";
</script>

the other option is to have your JavaScript file as PHP script then you can use PHP code inside.

Upvotes: 1

Coffee&#39;d Up Hacker
Coffee&#39;d Up Hacker

Reputation: 1446

Check out php.js for a JavaScript port of dirname...

As for getenv('DOCUMENT_ROOT'), you can't get this variable value via JavaScript. The reason for this is the client-side "location" of the JavaScript file says nothing about it's actual location on the server. The best you can do is get the parent directory of the file or the domain name.

For example:

http://example.com/path/to/file

The "DOCUMENT_ROOT" as far as JavaScript is concerned is...

/path/to

It seems very improper to even assume the need to have the server-side location of the file available to the JavaScript. I would simply use either a specific URL query that indicates what location to use, or to send the location back with the AJAX response since you can add whatever you need there.

{
    "document-root": "/SITE2/assets/uploads/",

    "normal-response": {
        ...
    }
}

And then wherever you would normally use what is normally received, this use...

// For example
var response = JSON.parse (request.responseText);

// Normal response
console.log (response['normal-response'][...]);

// Document Root
console.log (response['document-root']);

Upvotes: 0

Don Rhummy
Don Rhummy

Reputation: 25830

You have two options, but I caution you sending the actual file path on the server/disk to the client side is a bad idea.

  1. Use Ajax so the client sends a request like $.ajax({url: "getdir.php", success: someJSFuntion } );
  2. Change your JavaScript file to be a ".php" file and then include the code right in there.

There is no spec that says you can't have your JS file be a ".php" file. So you'd link to it like this:

<script type="text/javascript" src="/js/myjs.php"></script>

And that would let you use PHP directly in your JS file.

Upvotes: 0

Related Questions