Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

PHP mkdir: Warning: mkdir() [function.mkdir]: File exists in (PathToScript.php) on line #

Posted on 2007-04-09
1
Medium Priority
?
5,193 Views
Last Modified: 2013-12-13
I have a PHP form handler that uploads files. It is below. It is on PHP server version 5.1.1 with Windows 2003. It gives the following error. All the permission seem to be right to write to the directories. Where did I go wrong?

ERROR RESULTS:
Warning: mkdir() [function.mkdir]: File exists in c:\Inetpub\wwwroot\mae-oaa\trav\uploader.php on line 7

File is valid, and was successfully uploaded.
Here is some more debugging info:Array
(
    [userfile] => Array
        (
            [name] => Todo.txt
            [type] => text/plain
            [tmp_name] => C:\PHP\uploadtemp\php1476.tmp
            [error] => 0
            [size] => 288
        )
)


UPLOADER.PHP
<?php
// In PHP versions earlier than 4.1.0, $HTTP_POST_FILES should be used instead
// of $_FILES.

$uploaddir = '/inetpub/wwwroot/trav/uploads/';
$nominee = $nominee_first_name . " " . $nominee_last_name;
mkdir($uploaddir . $nominee);
$uploadfile = $uploaddir . $nominee . "/" . basename($_FILES['userfile']['name']);

echo '<pre>';
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
echo "File is valid, and was successfully uploaded.\n";
} else {
echo "Error!\n";
}
echo 'Here is some more debugging info:';
print_r($_FILES);
print "</pre>";
?>
0
Comment
Question by:travisjbennett
[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
1 Comment
 
LVL 15

Accepted Solution

by:
Tomeeboy earned 1000 total points
ID: 18877307
Looks like you are getting the error because the directory exists, not the file.  Change your code to this to check if the directory already exists first:

<?php
// In PHP versions earlier than 4.1.0, $HTTP_POST_FILES should be used instead
// of $_FILES.

$uploaddir = '/inetpub/wwwroot/trav/uploads/';
$nominee = $nominee_first_name . " " . $nominee_last_name;
if (!is_dir($uploaddir . $nominee) {
     mkdir($uploaddir . $nominee);
}
$uploadfile = $uploaddir . $nominee . "/" . basename($_FILES['userfile']['name']);

echo '<pre>';
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
echo "File is valid, and was successfully uploaded.\n";
} else {
echo "Error!\n";
}
echo 'Here is some more debugging info:';
print_r($_FILES);
print "</pre>";
?>

This will check to see if the directory exists, and create the directory if it does not.

Hope that helps!
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

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

688 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