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
Solved

Check if directory exists before creating

Posted on 2006-11-14
4
896 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Call a function within the ASP code 4 19
jquery datable hidden section doesn't submit 2 15
Hide Table in merge 3 30
javascript - insert into js doc help 2 19
Today I would like to talk about localizing (Internationalization) JavaScript applications. Introduction When creating an application that is going to be used by many people around the globe, it is important to remember that not everyone speak…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
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…

808 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