?
Solved

Check if directory exists before creating

Posted on 2006-11-14
4
Medium Priority
?
904 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
[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
  • 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 2000 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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…
Suggested Courses

765 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