Solved

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

Posted on 2007-04-09
1
5,135 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 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

734 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