2

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.

ryryan
  • 3,890
  • 13
  • 43
  • 72
  • 1
    You can mount the disk from the wamp, you can run a process on the wamp that listens to create-folder requests (even a script), or you can connect to your wamp (using SSH, FTP, SAMBA...) and create the folder... what is running on the wamp already? Can you mount a disk? That would be easiest. – Konerak Mar 05 '11 at 14:17
  • However you decide to solve this, take great care to ensure that hackers can't create/modify/delete arbitrary files! – Mark Eirich Mar 05 '11 at 14:21
  • you may check [this answer](http://stackoverflow.com/questions/1688564/php-directory-list-from-remote-server) – pablov Mar 05 '11 at 14:18
  • Thanks a lot! I never knew PHP could do that! Works straight away! :D – ryryan Mar 05 '11 at 17:26

4 Answers4

2

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"
Pekka
  • 442,112
  • 142
  • 972
  • 1,088
1

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)

simon
  • 1,161
  • 10
  • 27
1

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.)

mario
  • 144,265
  • 20
  • 237
  • 291
0

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 :)