Solved

Check if directory exists before creating

Posted on 2006-11-14
4
888 Views
Last Modified: 2008-01-09
Hi,

I have got a php script that runs perfectly to add/delete directories on a web server.  What i am after i think can be done some how with javascript.

I have a text field in a form, and what i would like to happen is for when a user starts typing in a name of a folder to create i need it to display back on screen (underneath text field for example) whether if directory exist or not.  It would only need to check 1 directory to see if a the new directory name exists when being entered.  In this case for my purpose it is the previous directory to where the script would work... so '../'

I hope some one can shine some light on this.

Many thanks
Pauliostro
0
Comment
Question by:Pauliostro
  • 2
  • 2
4 Comments
 
LVL 6

Expert Comment

by:aescnt
ID: 17943645
The existence of a directory has to be checked through PHP. The PHP function you're looking for is `is_dir()`.

Then you can have a "check for this directory" submit button which submits to a PHP script with the directory-checking logic, and redraw the form with text underneath the text field which would indicate the existence of the directory.

An alternate solution would be to use an XMLHTTPRequest (bound to onblur of the text field) to call a PHP script which would indicate if the directory does or doesn't exist. Then, use JS to change the text "underneath the text field."
0
 

Author Comment

by:Pauliostro
ID: 17944309
Hi Aescnt,

Thanks for your reply.  I like the 2nd option you have mentioned (XMLHTTPRequest).  Unfortunately its a bit out of my expertise, are you able to show me the code of how to do this or somewhere where i could find out about how to implement something like this.

Many thanks in advance!
0
 
LVL 6

Accepted Solution

by:
aescnt earned 500 total points
ID: 17944542
I strongly suggest you go study :) There are tons of AJAX resources on the net!

You can make a php script like so:
directorycheck.php

<?php
$basepath = '/path/to/check';
if (@!$_GET['dir']) { die ("Please enter a name."); }

// Check if the path is valid
// (e.g., inputs like "../../x" will be invalid)
if (get_magic_quotes_gpc()) { $_GET['dir'] = stripslashes($_GET['dir']); }
$path = realpath($basepath . '/' . $_GET['dir']);
$basepath = realpath($basepath);
if (substr($path, 0, strlen($basepath)) != $basepath) { die("Invalid input."); }

// The actual checking logic
if (is_dir($path)) { die("Directory exists."); }
else { die("Directory doesn't exist."); }
?>


HTML:

<input type="text" name="dir" id="dir" onblur="checkDirectory(this, 'dir-result');">
<div id="dir-result"></div>


JS:
(Make sure the URL in checkDirectory points to the right path to the PHP file)

function createRequestObject()
{
    if (XMLHttpRequest) { return new XMLHttpRequest(); }
    else {  return new ActiveXObject("Microsoft.XMLHTTP"); }
}

function checkDirectory(_this, targetID)
{
   var url = 'directorycheck.php';
   var target = document.getElementById(targetID);
   if (target) { target.innerHTML = "Checking..."; }

    var http = createRequestObject();
    http.open('get', url + '?dir='+encodeURIComponent(_this.value));
    http.onreadystatechange = function() {
        if(http.readyState == 4){
            target.innerHTML = http.responseText;
        }
    };
    http.send(null);
}


0
 

Author Comment

by:Pauliostro
ID: 17976132
aescnt, you are a living champion.  Thanks so much!!!!!!

Script works perfect...  thank you.... thank you... thank you....
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at the …
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

762 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now