Solved

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

Posted on 2007-04-09
1
5,062 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
1 Comment
 
LVL 15

Accepted Solution

by:
Tomeeboy earned 250 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 Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.

707 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

13 Experts available now in Live!

Get 1:1 Help Now