Solved

Referencing IIS Virtual Directories with PHP

Posted on 2011-09-27
7
1,938 Views
Last Modified: 2012-05-12
We're running PHP v5.3.6 on a Windows Server 2008 R2 (x64) machine with IIS v7.5.  We've created an IIS Virtual Directory pointing to a shared directory on a remote server (via UNC).  With directory browsing enabled (in IIS), we're able to hit the URL of the virtual directory with a web browser and see the files contained within the corresponding directory on the remote server.  However, PHP doesn't seem to be able to utilize the IIS Virtual Directory.

PHP's Filysystem and Directory functions work for local, physical directories, but don't seem to be able to utilize IIS' Virtual Directories.

phpinfo() shows "Virtual Directory Support" is disabled, but from what I've been able to gather, this has more to do with PHP's mod_rewrite URL.


Is there a way to make PHP aware of IIS' Virtual Directories?  If so, how?  And if not, is there an alternative way to reference a remote file system with PHP on IIS?
0
Comment
Question by:cadcollin
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
7 Comments
 
LVL 11

Expert Comment

by:Paolo Santiangeli
ID: 36711296
hi,

you can use directly the same unc used in iis to create the virtual dir
es: $filepath = '//serverip/foldername/';

0
 

Author Comment

by:cadcollin
ID: 36711704
I've tried the following four variants to account for potential syntax issues, but the condition is never satisfied.

if(is_dir("//server/PDF_Output/" . $_REQUEST["directory_name"]))
if(is_dir("//server/PDF_Output/" . $_REQUEST["directory_name"]."/"))
if(is_dir("\\\\server\\PDF_Output\\" . $_REQUEST["directory_name"]))
if(is_dir("\\\\server\\PDF_Output\\" . $_REQUEST["directory_name"]."/"))

($_REQUEST["directory_name"] is set to the name of a directory contained within //server/PDF_Output.)

I've made the share's permissions pretty wide-open, but maybe that's where the issue lies?  What permissions should the remote share have?  I've explicitly extended "Full Control", "Change", and "Read" permissions to "Everyone", "ANONYMOUS LOGON" and the local administrator on the remote server's share.  It seems like that ought to cover the gamut, but maybe the required account is excluded from the "Everyone" group and the "ANONYMOUS LOGON" service/account?
0
 
LVL 13

Expert Comment

by:khairil
ID: 36712171
Hi,

It is a security context issues over the shared folder:

Here is what you can try:
Method 1
=======
Make sure your IUSR user has read rights on the folder on the remote server (with same password)

Method 2
=======
Enable Guest acount on remote server and give read access to the shared folder.



0
Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

 

Author Comment

by:cadcollin
ID: 36712593
I've enabled the guest account on the remote server and enabled read permissions -- and write and full control permissions, for now -- for both accounts (IUSR and Guest) on the remote server's share, but PHP still can't see the contents of the remote directory.

The PHP error log isn't generating any errors or warnings.  I'm able to use PHP's Filysystem and Directory functions to reference local directories, so they don't seem to be the issue.

All I'm trying to do is build a list of PDF files contained in a specific remote directory.  Different users are using different directories.

I've attached my code.

Note: I concatenate $base_dir (line 45) and $_REQUEST["directory_name"] to build the UNC path (on line 48).
<html>
<head>
<style>
<!--
body
	{
	font-family:		"Segoe UI", Calibri, Verdana, Arial, sans-serif;
	font-size:			80%;
	}
a
	{
	text-decoration:	none;
	color:				#33c;
	}
a:hover
	{
	color:				#c33;
	}
dt
	{
	text-align:			left;
	display:			block;
	clear:				both;
	float:				left;
	padding:			0 1em;
	}
dd
	{
	color:				#999;
	text-align:			right;
	border-bottom:		1px dotted #ccc;
	padding:			0 1em 0 0;
	display:			block;
	margin:				.25em 0;
	}
.warning
	{
	background:			yellow;
	}
	// -->
</style>
</head>
<body>
<?php
$base_dir = "\\\\server\\PDF_Output\\";
if(isset($_REQUEST["directory_name"]))
	{
	$_REQUEST["directory_name"] = $base_dir . $_REQUEST["directory_name"];
	if(is_dir($_REQUEST["directory_name"]))
		{
		if($directory = opendir($_REQUEST["directory_name"]))
			{
			$count=0;
			while(false !== ($file = readdir($directory)))
				if(filetype($_REQUEST["directory_name"] . "\\" . $file)=="file" && substr($file,-4,4) == ".pdf")
					{
					$warning = (filesize($_REQUEST["directory_name"] . "\\" . $file)<5000?" class=\"warning\"":"");
					$list[$count] = "\t<dt $warning><a title=\"Click here to open " . $file . "\" target=\"pdf\" href=\"file:" . str_replace("\\","/",$_REQUEST["directory_name"]) . "/" . $file . "\">" . str_replace("_","",preg_replace("/(\D+)\d+_(\d+)_\d+/","$2 - $1",substr($file,strpos($file,"_")+1))) . "</a></dt>\n\t\t<dd>" . date("F j, Y, g:i a",filectime($_REQUEST["directory_name"] . "\\" . $file)) . "</dd>\n";
					
					$count++;
					}
			if($count)
				{
				if($count==1)
					echo "<h3>There is one PDF in the <i>" . $_REQUEST["directory_name"] . "</i> folder.</h3>\n";
				elseif($count>1)
					echo "<h3>There are " . number_format($count) . " PDFs in the <i>" . $_REQUEST["directory_name"] . "</i> folder.</h3>\n";
				echo "<dl>\n";
				foreach($list as $listitem)
					echo $listitem;
				echo "</dl>\n";
				}
			else
				
				echo "<h3>The <i>" . $_REQUEST["directory_name"] . "</i> folder is currently empty.</h3>\n";
			closedir($directory);
			}
		}
	else
		echo $_REQUEST["directory_name"] . " is not a valid directory";
	}
else	// If there's no "directory_name" parameter passed in the URL or via HTTP headers.
	{
	$directory_name = "printer_jobs";

	echo "<a href=\"$directory_name\">Click here</a> to see the contents of the IIS virtual directory <i>$directory_name</i>, as seen by IIS.<br /><br />";

	echo "Below is a listing of the contents of the IIS virtual directory <i>$directory_name</i>, as seen by PHP.<br />";
	if(is_dir($directory_name))
		{
		if($directory = opendir($directory_name))
			{
			echo "<ul>\n";
			while(false !== ($file = readdir($directory)))
				echo "\t<li><a href=\"" . $directory_name . "/" . $file . "\">$file</a></li>\n";
			echo "</ul>\n";
			closedir($directory);
			}
		}
	else
		echo "\"$directory_name\" is not a valid directory";
	}
?>
</body>
</html>

Open in new window

0
 

Author Comment

by:cadcollin
ID: 36719666
Since I haven't been able to make any headway getting PHP to see IIS' Virtual Directory or getting PHP's Directory functions to see the remote share, I decided to try creating a Symbolic Link on the host pointing to the target directory.

Windows Explorer sees the symlinked directory as a sub-directory in my project.  Explorer can read and write to the directory.
IIS Manager sees the symlinked directory as a sub-directory in my project.  In Content View, IIS Manager can browse the directory.
When the symlinked sub-directory's URL is hit with a web browser, I'm greeted with an IIS error page.  Error 401, to be specific, which suggests that some kind of permissions issue may still exist.
PHP can actually see the symlinked directory!  The "is_dir()" check passed, but I still fail-out on the open_dir() function -- but now, at least, PHP is writing warnings to the error log...

The open_dir() call generates two warnings each time the script is executed.  The first warning says that access to the directory was denied, the second just says "failed to open dir: No error in ...index.php".
0
 

Accepted Solution

by:
cadcollin earned 0 total points
ID: 36891609
Instead of accessing the files remotely, I'm now copying the files locally and then referencing them locally with PHP's Filysystem and Directory functions to accomplish what I needed to do.  It works, but it's less-than-ideal -- especially because of the volatility of the content on the remote directory.  (There's frequent changes to the directory through-out the day.)
0
 

Author Closing Comment

by:cadcollin
ID: 36915472
The end-solution wasn't even one that I wanted to consider early-on, but one that I had to resort to to resolve the problem-at-hand in a reasonable amount of time.
0

Featured Post

Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
A phishing scam that claims a recipient’s credit card details have been “suspended” is the latest trend in spoof emails.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

695 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question