Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Is this possible with PHP?

Status
Not open for further replies.

sd0t1

IS-IT--Management
Mar 14, 2007
131
US
I have 2 departments. One that requests files from another location in the city (archives). When archives receives these requests they scan the documents to a shared network directory. Then they email the staff to notify them that their request is scanned and ready for viewing.

Well I am developing an intranet to handles these requests.

My staff scans files to a shared network directory.

What I'm stuck on is this:

My goal is to when the files are scanned to have PHP scan the directory and create hyper links to each file, store that link in the database so I can use it elsewhere later.

I know how to scan the directory via a script.
What I don't know is how to trigger the script to run everytime a new file is copied into that directory. Generating the links in real-time.

Can anyone help me?
How would you accomplish this?
 
Don't store the links in a DB, have PHP dynamically construct the file view page on the intranet based on what's in the folder. If something was added PHP will automatically read it when it loads the page and generate the links on the fly.

If something is removed, refresh the page and link is automatically gone.

Adding the extra DB layer only adds to the maintenance.




----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Behind the Web, Tips and Tricks for Web Development.
 
I understand and that worked. My question now is how do I access a shared network folder.
See the line below that says:
$dirpath = "\\\\hostname10048\\c$\\attachments\\";

I've tried several variation and none of them work. Also, this machine is mapped on my server to the U: drive. I've tried several combination of that also.

Please help me figure out what I'm doing wrong as I'm pretty sure this is possible.

<?php
//if they clicked the 'submit' button,
//upload the file they chose.
//handling file uploads
if ($_POST['submit'])
{
copy($_FILES['file']['tmp_name'], $_FILES['file']['name']);
}

?>
<html>
<head><title>page</title>
</head>
<body>
<?php
//open the current directory (".")
//$dirpath is directory outside of where actual attachments are stored
$dirpath = "\\\\crim10048\\c$\\attachments\\";
$dir=opendir($dirpath);

//make an array we'll use to store the files we find
$files=array();

//read each file or folder in the dir and
//put it into the $files array if it's not
//this file, ".", or ".."
//reading files & folders in a directory
while (($file=readdir($dir)) !== false)
{
if ($file != "." and $file != ".." and $file != "autoscan.php")
{
array_push($files, $file);
}
}
//close the directory
closedir($dir);

//sort the files so they print out in
//alphabetical order
sort($files);

//go through each file we found and print
//out a link to it. change [ and ] to < and >
foreach ($files as $file)
print "<a href='".$dirpath.$file."'>$file</a><br>";
?>

thanks
 
Is the shared folder in the same server as the webserver/PHP? If it is then there's no need to access through a share, but rather just the file system.

If not, then you need to configure your web server to run as a user that has access to network resources.

The PHP online manual has this in the user contributed notes for opendir:

PHP.net said:
...when you run Apache as a service on your Windows computer, it chooses to run as the LocalSystem account by default (usually SYSTEM). The LocalSystem account has no network privileges whatsoever which, while no doubt a good thing, makes it impossible to access networked resources (such as a shared drive) in your Apache service.

First, you have to change the user the Apache service runs as.
Go to your Services panel (Start -> Run -> "services.msc").
Find the Service labeled Apache, right-click, and hit Properties.
Choose the "Log On" tab.
Presumably you'll see that Apache is set up to run as the Local System Account. You'll want to change this to the second option, "This account", and then fill in the details of the User account you would like Apache to run under.
Some sites tell you to create a special Apache-based user account just for this occasion. It's not a bad idea, but then you have to make sure it has all of the proper permissions that an Apache user would need, such as read/write to to htdocs and the .conf and .log files, and permissions to log on as a service, etc etc - as well as the permissions to access the network resource you're trying to get to in the first place.
In light of that process, I chose to just run it under my own account instead. Once you get it working, you can go back and create that special account.
Hit "Apply" - it'll pop up a box saying you need to restart Apache to take effect, but hold off on that for a moment.
This is the tricky part: you have to give the user (the one you're running Apache as) permissions to act as part of the OS.
Go to the Local Security Policy panel (Start -> Run -> "secpol.msc").
Under the navigation section in the left sidebar, choose Local Policies -> User Rights Assignments.
In the right-hand frame, double-click the item "Act as part of the operating system" to open up its properties.
Select "Add User or Group, Enter the appropriate user in the box provided, and hit "OK."
At this point, you are technically complete - Apache can now do the same things to the network resource that your user can - read, write, execute, whatever.

It talks specifically about Apache, but I would imagine IIS and any other web server might require similar setups to access network resources.

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Behind the Web, Tips and Tricks for Web Development.
 
Thanks for the help Vacunita.

I didn't have the permissions set, but I do now and it still isn't working.

I'm pretty sure I have the path wrong in some way and I just don't know enough about that stuff to recognize it.
I've tried all the variations I could think of of forward and back slashes. plus I tried the ip and the host name, even just the drive letter that I have mapped to the network computer.

Still I get an error that says it can't open the directory.
Warning: opendir(//xxx.xxx.xxx/attachments) [function.opendir]: failed to open dir: No error in C:\AppServ\ on line 28

I'm pretty stuck. any other suggestions?
 
What webserver are you using?

I went ahead and followed the above instructions and I can successfully read a shared folder by both IP and UNC name.

Code:
$dir ="\\\\xxx.xxx.xxx.xxx\\shareddir";
/* $dir ="\\\\server\\shareddir"; */
if(!is_dir($dir)){
echo "Cannot Open dir: $dir";
}
else{
$hd=opendir($dir);
$files=array();
while (($file=readdir($hd)) !== false)
{
   if ($file != "." and $file != ".." and $file != "autoscan.php")
   {
    array_push($files, $file);
   }
}

echo '<pre>';
print_r($files);
echo '</pre>';
}

Make sure the user has all the required privileges.


----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Behind the Web, Tips and Tricks for Web Development.
 
Wow, it turned out to be permissions. I didn't have the share permissions setup correctly, only the ones on the apache server.

I got it to work with everyone having full control, i'm throttling back now to see how where the happy medium is.

I really appreciate your help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top