Jump to content

Folder does / does not exist?


enjoirock

Recommended Posts

Hi all -

 

I have a form that let's a user upload an image to their specified folder. The folder is determined by the "password" the user inputs. The "password" is really just the folder name on my server, so if "michael83" was entered as a password, the directory would be "images/uploaded/michael83". That works great now, thanks to a forum user, litebearer. Thanks again.

 

Here's my second question:

 

I'm working on a "history" page so that a user can input their "password" and view their upload history.

 

The form works right now. If you enter a "password" (folder name) that exists, the code will spit out the contents / files. So, now I'm looking for two things:

 

(1) if a user enters a "password" (folder name) that exists, but there are no files in the folder, "There are no files in the $dir directory." will be echoed; and

 

(2) if a "password" (folder name) is entered that does NOT exist, "The directory $dir does not exist." will be echoed.

 

Here's my code so far (minus the if statements I'm looking for):

 


$dir = $_POST['password'];
$path = "images/uploaded/". $dir;
$dir_handle = @opendir($path);

while ($file = readdir($dir_handle)) {
if($file == "." || $file == ".." )
continue;
if(isset($_POST['Submit']))

$url = "http://www.mysite.com/". $path . "/" . $file;
{
echo "<a href=\"$url\">$url</a><br />"; 
}}

closedir($dir_handle);

 

As always, many thanks in advance for any help on this.

Link to comment
Share on other sites

I'm a little bit closer:

 


$dir = $_POST['password'];
$path = "images/uploaded/". $dir;
$dir_handle = @opendir($path);

if(is_dir($path)){
echo ("Listing for directory <strong>$dir</strong>:<br><br>");
}
else{
echo ("Sorry, the directory <strong>$dir</strong> does not exist.");
echo '<p>If you continue to have problems, please <a href="#">contact us</a>.</p>';
}

while ($file = readdir($dir_handle)) {
if($file == "." || $file == ".." )
continue;

$url = "http://www.mysite.com/". $path . "/" . $file;

{
echo "<a href=\"$url\">$url</a><br />"; 
}

}

if(isset($_POST['Submit']))

closedir($dir_handle);

 

The problem I'm seeing now is right off the bat (before a password is even entered / submitted), the code spits out (showing on the page) the folders/directories within the "uploaded" folder as follows:

 

-----

Listing for directory :

 

http://www.mysite.com/images/uploaded//michael83

-----

 

Probably should have mentioned once again that I'm new to PHP. Anyway, thanks again to anyone that can help out.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.