$tmpUploadFolder = "C:\\www\\intranet\\uploads";
//$finalUploadFolder = "file:////server//photos//overwrite";
$finalUploadFolder = "file://server/photos/overwrite";
//$finalUploadFolder = "\\\\server\\photos\\overwrite";
//$finalUploadFolder = "\\server\photos\overwrite";
//$finalUploadFolder = "P:\\overwrite";
//$finalUploadFolder = "P:/overwrite";
$from = $tmpUploadFolder . "\\" . $_REQUEST['ext'];
$to = $finalUploadFolder. "\\" . $_REQUEST['ext'];
copy($from, $to);
I am trying to do a PHP upload using a jquery tool. The Jquery tool nicely places the file onto the PHP upload dir before the page submit. So i want to (upon post of the form) quickly move the file from it's tmp folder location (it'll already be there you see) to it's final destination on an image store server (I use the _REQUEST['ext'] variable to hold the filename jquery held.
Rest assured these paths are good they work lovely in dos. As you can see I have tried every known unc syntax I know.
I cannot for the life of me get php to work I have written a VBS "copy . file" and tried to trigger it under whost.exe via system() in php, i've downloaded the oldeskool runas.exe and tried to get it to copy via system(), I have used unc paths and network shares, and mapped network drives, I have made apache service "log on as " administraor and even a custom adhoc new user made just for this and given it full permissions
It works fine if I change P:\ to C:\
I KNOW IT'S EFECTIVE PERMISSONS RE: APACHE - BUT WE DO NOT RUN ACTIVE DIRECTORY AND I CAN'T GET IT TO WORK
it simply will not let me copy this file onto a network and this is a major major MAJOR problem child for me.
Is there a solution? If you are going to help me with things like "it's file permissions" then I am going to need a break down of exact and careful instructions because I am pulling my hair out because I know it's file permissions rights but I just can't get it to work
I am tired now.. please help?
ok I figured it out so for the benefit of those going after me here is the solution THAT WORKS
1.make sure php windows "apache2.2" service is running as a administrator user (I made a user called apacheusr and gave it a password and popped it into local administrators) you do this by right clicking properties on the "apache2.2" service in administrative tools->services and going to the logon tab->this account and picking the apacheusr
2.because I don't run active directory I made this apacheusr user on BOTH machines (phpserver/ imageserver) as a local administrator user and gave them BOTH the same username password and tick password never expires.
3.I then log in/out at least once onto windows with both these accounts. (don't ask me why but it seemed to help, it stopped the runasexe --that I gave up with-- moaning in dos)
4.finally on the php server right click share the destination folder on imageserver and make damn well sure this apacheusr can log in to that folder. The simplest way to do this is when you log/in/out as apacheusr on your php server and try to go to your image server folder - you then need to be on the imagesever and tick everything correctly in the share/permissions bit
THEN the final bit is (where _REQUEST['ext'] is a file name EG: "pic.jpg")
$tmpUploadFolder = "C:\\www\\intranet\\uploads";
$finalUploadFolder = "\\\\server\\photos\\overwrite";
$from = $tmpUploadFolder . "\\" . $_REQUEST['ext'];
$to = $finalUploadFolder. "\\" . $_REQUEST['ext'];
copy($from, $to);
The above code works!
In what environment do you run php? Apache? IIS? These run most of the time as a service with System Credentials and cannot access Network shares...
Change the Webserver Account to a User that can write and it should work (with one of those URLs at least)
Related
im trying to parse pdf to text via PHP and XPDF (pdftotext.exe). On my localhost everythings works well, but when im trying to move everything on server, im getting into troubles.
First of all i checked some settings on server and safe_mode is off, exec is not disabled and permissions are rwxrwxrwx.
Then im trying this
$command = "\\\\149.223.22.11\\cae\\04_Knowledge-base\\tools\\pdftotext.exe -enc UTF-8 ". $fileName . " \\\\149.223.22.11\\cae\\04_Knowledge-base\\output.txt";
$result = exec($command,$output,$args);
echo shell_exec($command);
which isnt working. When i look into $result, $output, are empty, but $args returns 1 which coresponds to Incorrect function by this document windows system error codes
Whole command looks like \\149.223.22.11\cae\04_Knowledge-base\tools\pdftotext.exe -enc UTF-8 \\149.223.22.11\cae\04_Knowledge-base\testpdf\04_egerland_final_paper.pdf \\149.223.22.11\cae\04_Knowledge-base\output.txt and when is dirrectly inputed into commandline, its working.
So im a bit out of ideas. Have someone any hint?
edit 20160201 - aditional trying
So i made aditional tests and when im trying to run similar command with exec from localhost (target .exe file, input and output file is in same location, only im using localhost not server) its working. Im now checking differences in server settings. So can here be problem, that localhosts Server Api is Apache 2.0 Handler and server is CGI/FASTCGI?
So it was all mistake on my side. I badly checked IIS permissions and forgot to assign user to virtual directory which i wana to access. So my only advice and more wisdom from this is to double (maybe triple) check if you have all permissions set correctly.
I'm using php 5.2 with IIS7.5.
I have a network share on a NAS that is username and password protected. I cannot disable or change authentication info on the NAS. I need to be able to access that network share via php.
I've done the following:
Created new user in windows whose username and password matches those on the NAS.
Created IIS application pool that uses this same auth info.
Created a web.config file inside of the php app directory with an impersonation turned on, using the same auth info.
identity impersonate="true" password="ThePass" userName="TheUser" />
Turned on ASP.NET impersonation in the application authentication in IIS.
None of this seemed to work with this simple line of code in php:
$dir = opendir("\\someservername\somesharename");
Warning: opendir(\someservername\somesharename) [function.opendir]: failed to open dir: No error in C:\websites\site\forum\testing.php on line 7
So, I decided to test the configuration with ASP.NET.
string[] diretories = System.IO.Directory.GetDirectories("\\someservername\somesharename");
The asp.net test worked perfectly.
Going further down the rabbit hole, I ran phpinfo() and checked the username info in it. Down in the "Environment" section of phpinfo, I found the "USERNAME" item. Its value was "TheUser," as was what I expected.
Everything points to the system being configured correctly until I tried:
echo get_current_user();
Which returned, "IUSR." That surely isn't what I expected.
So, how in the world do I get php + IIS7.5 to read from a foreign network share?
Update:
Thanks to a few of the answers, I've added
$result = shell_exec("net use o: \\\\pathToServer\\27301 /persistent:yes 2>&1");
Which returns a success. I'm still getting the error on opendir. I tried another test and used is_dir. This returned false on my newly created mapped drive.
var_dump(is_dir("o:\\"));
// Prints: bool(false)
UPDATE
I ran the script from the command line when logged in as the user that created. The scripts executes correctly. Could this take us back to get_current_user() which returns IUSR? I tryied getmypid() which returned a process ID. I cross referred that process id with the task manager and found that it was for php-cgi.exe, running under the custom user account that I made.
The recommended way to access a network share in PHP is to "mount" it. Try to connect your share as a network drive.
Btw. your command is wrong
$dir = opendir("\\someservername\somesharename");
You have to use 4 "\" because it's the escape character
$dir = opendir("\\\\someservername\somesharename");
NOTE: get_current_user() returns the owner of the process on IIS
You can try a test by mapping it like
$command = "net use $drive \"\\\\$ip\\$share\" $smb_password /user:$domain\\$smb_username";
$result = shell_exec($command);
on opendir you need to have \\\\$server_name\\$share try with 4 '\' and if mapping like that works and 4 '\' is failing on opendir. You may have credentials not matching.
If you map it this way new user you created in windows whose username and password matches those on the NAS will have rights on mapped drive that way you do not need to worry about the scope.
Had the same problem on a similar system. Solved this by going to web site > Authentication > Anonymous Authentication > Change IUSR to whatever your username is or use Application Pool user if correctly configured.
I have a php script that logs into my servers via the ftp function, and backs up the entire thing easily and quickly, but I can't seem to find a way to let the script determine the folder where the main index file is located.
For example, I have 6 servers with a slew of ftp accounts all set up differently. Some log into the FTP root that has httpdocs/httpsdocs/public_html/error_docs/sub_domains and folders like that, and some log in directly to the httpdocs where the index file is. I only want to backup the main working web files and not all the other stuff that may be in there
I've set up a way to define the working directory, but that means I have to have different scripts for each server or backup I want to do.
Is it possible to have the php script find or work out the main web directory?
One option would be to set up a database that has either the directory to use or nothing if the ftp logs in directly to that directory, but I'm going for automation here.
If it's not possible I'll go with the database option though.
You cannot figure out through FTP alone what the root directory configured in apache is - unless you fetch httpd.conf and parse it, which I'm fairly sure you don't want to do. Presumably you are looping to do this backup from multiple servers with the same script?
If so, just define everything in an array, and loop it with a foreach and all the relevant data will be available in each iteration.
So I would do something like this:
// This will hold all our configuration
$serverData = array();
// First server
$serverData['server1']['ftp_address'] = 'ftp://11.22.33.44/';
$serverData['server1']['ftp_username'] = 'admin';
$serverData['server1']['ftp_password'] = 'password';
$serverData['server1']['root_dir'] = 'myuser/public_html';
// Second server
$serverData['server2']['ftp_address'] = 'ftp://11.22.22.11/';
$serverData['server2']['ftp_username'] = 'root';
$serverData['server2']['ftp_password'] = 'hackmeplease';
$serverData['server2']['root_dir'] = 'myuser/public_html';
// ...and so on
// Of course, you could also query a database to populate the $serverData array
foreach ($serverData as $server) {
// Process each server - all the data is available in $server['ftp_address'], $server['root_dir'] etc etc
}
No, you can't do it reliably without knowledge of how Apache is setup for each of those domains. You'd be better off with the database/config file route. One-time setup cost for that plus a teensy bit of maintenance as sites are added/modded/removed.
You'll probably spend days getting a detector script going, and it'll fail the next time some unknown configuration comes up. Attemping to create an AI is hard... you have to get it to the Artificial Stupidity level first (e.g. the MS Paperclip).
I have Wamp (server called emerald) running and Mamp running on my Mac. People register on Mamp. Emerald is basically file hosting.
Emerald connects to Mamp's mysql database, to login users. However, I want to create a directories for new registrations on Emerald using PHP.
How can I do this? I have tried using this code:
$thisdir = "192.168.1.71";
$name = "Ryan-Hart";
if(mkdir($thisdir ."/documents/$name" , 0777))
{
echo "Directory has been created successfully...";
}
But had no luck. It basically needs to connect the other server and create a directory, in the name of the user.
I hope this is clear.
You can't create directories through http. You need a filesystem connection to the remote location (a local hard disk, or a network share for example).
The easiest way that doesn't require setting up FTP, SSH or a network share would be to put a PHP script on Emerald:
<?php
// Skipping sanitation because it's only going to be called
// from a friendly script. If "dir" is user input, you need to sanitize
$dirname = $_GET["dir"];
$secret_token = "10210343943202393403";
if ($_GET["token"] != $secret_token) die ("Access denied");
// Alternatively, you could restrict access to one IP
error_reporting(0); // Turn on to see mkdir's error messages
$success = mkdir("/home/www/htdocs/docs/".$dirname);
if ($success) echo "OK"; else echo "FAIL";
and call it from the other server:
$success = file_get_contents("http://192.168.1.71/create_script.php?token=10210343943202393403&dir=HelloWorld");
echo $success; // "OK" or "FAIL"
Create a script on another server that creates the dir and call it remotely.
Make sure you have security check (+a simple password at least)
There is no generic method to access remote server filesystems. You have to use a file transfer protocol and server software to do so. One option would be SSH, which however requires some setup.
$thisdir = "ssh2.sftp://user:pass#192.168.1.71/directory/";
On Windows you might get FTP working more easily, so using an ftp:// url as directory might work.
As last alternative you could enable WebDAV (the PUT method alone works for file transfers, not creating directories) on your WAMP webserver. (But then you probably can't use the raw PHP file functions, probably needs a wrapper class or curl to utilize it.)
I know this is old but i think this might me useful, in my experience:
if(mkdir($thisdir ."/documents/name" , 0777))
doesn't work, i need to do it:
mkdir($thisdir, 0777);
mkdir($thisdir ."/documents" , 0777);
mkdir($thisdir ."/documents/name" , 0777));
hope it helps :)
within PHP (XAMPP) installed on a Windows XP Computer Im trying to read a dir which exists on a local network server. Im using is_dir() to check whether it is a dir that I can read.
In Windows Explorer I type \\\server\dir and that dir is being shown.
When I map a network drive a can access it with z:\dir as well.
In PHP I have that script:
<?php if( is_dir($dir){ echo 'success' } ) ?>
For $dir I tried:
/server/dir
//server/dir
\server\dir
\\server\dir
\\\\server\\dir
and
z:\dir
z:\\dir
z:/dir
z://dir
But I never get success?
Any idea?
thx
I solved it by changing some stuff in the registry of the server as explained in the last answer of this discussion:
http://bugs.php.net/bug.php?id=25805
Thanks to VolkerK and Gumbo anyway!
I love stackoverflow and their great people who help you so incredibly fast!!
EDIT (taken from php.net):
The service has limited access to network resources, such as shares
and pipes, because it has no credentials and must connect using a null
session. The following registry key contains the NullSessionPipes and
NullSessionShares values, which are used to specify the pipes and
shares to which null sessions may connect:
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\LanmanServer\Parameters
Alternatively, you could add the REG_DWORD value
RestrictNullSessAccess to the key and set it to 0 to allow all null
sessions to access all pipes and shares created on that machine.`
add RestrictNullSessAccess=0 to your registery.
You probably let xampp install apache as service and run the php scripts trough this apache. And the apache service (running as localsystem) is not allowed to access the network the way your user account is.
A service that runs in the context of the LocalSystem account inherits the security context of the SCM. The user SID is created from the SECURITY_LOCAL_SYSTEM_RID value. The account is not associated with any logged-on user account.
This has several implications:
...
* The service presents the computer's credentials to remote servers.
...
You can test this by starting the apache as console application (apache_start.bat in the xampp directory should do that) and run the script again. You can use both forward and backward slashes in the unc path. I'd suggest using //server/share since php doesn't care about / in string literals.
<?php
$uncpath = '//server/dir';
$dh = opendir($uncpath);
echo "<pre>\n";
var_dump($dh, error_get_last());
echo "\n</pre>";
Try the file: URI scheme:
file://server/dir
file:///Z:/dir
The begin is always file://. The next path segment is the server. If it’s on your local machine, leave it blank (see second example). See also File URIs in Windows.
Yes, I know this is an old post, but I still found it, and if anyone else does...
On Windows, with newer servers, verify the SMB is installed and enabled on the target machine.