user2014390
user2014390

Reputation:

how to disable php warning mk.dir()

it is about this script: When uploading a file, first check whether the directory "uploads" exists, if not: create the directory. When the directory already exists and uploading a file, there comes a warning:

Warning: mkdir() [function.mkdir]: File exists in....

How to disable this warning?

if($_SERVER['REQUEST_METHOD'] == 'POST') {

// array with allowed extensions
$allowedExts = array("gif", "jpeg", "jpg", "png", "zip", "html", "htm", "js", "css",      "less", "txt", "php");
$temp = explode(".", $_FILES["file"]["name"]);
$extension = end($temp);

// create map "uploads" if doesn't exists
$root = '/uploads/';
    if (!is_dir($root)) {
        mkdir("uploads/", 0777);
                    echo 'The map uploads is created!'; 



if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/jpg")
|| ($_FILES["file"]["type"] == "image/pjpeg")
|| ($_FILES["file"]["type"] == "image/x-png")
|| ($_FILES["file"]["type"] == "image/png")
|| ($_FILES["file"]["type"] == "text/html")
|| ($_FILES["file"]["type"] == "text/javascript")
|| ($_FILES["file"]["type"] == "text/css")
|| ($_FILES["file"]["type"] == "text/less")
|| ($_FILES["file"]["type"] == "application/zip")
|| ($_FILES["file"]["type"] == "text/plain"))
&& ($_FILES["file"]["size"] < 20000000) // 2 Mb max
&& in_array($extension, $allowedExts))
{
// echo's if upload is succeeded
echo 'Status: upload succesvol!<br />';
echo 'Bestand: ' . $_FILES["file"]["name"] . '<br />';
echo 'Type: ' . $_FILES["file"]["type"] . '<br />';
echo 'Grootte: ' . ($_FILES["file"]["size"] / 1024) . ' kB<br />';

  $newfilename = uniqid().".".end(explode(".",$_FILES["file"]["name"]));
  move_uploaded_file($_FILES["file"]["tmp_name"], "uploads/" . $newfilename);
  // het pad naar het bestand dat geupload is; ervan uitgaande dat de map uploads in de  root staat
  echo 'Url: ' . '<b>http://www.jouwwebsite.nl/uploads/' . $newfilename.'</b>';
  echo '<br /><br />';
 }

  else
 {
 // echo if file extension is not allowed
 echo "Niet toegestaan bestand";
 }
 }
 else {

 }

 }
 } // end request_method POST 
?>

Upvotes: 1

Views: 1970

Answers (3)

Pepelotas
Pepelotas

Reputation: 31

$root = '/uploads/'; is an absolute path, and the following condition is probably returning false every time. Then it tries to create a "uploads" folder using a relative path, which was already created on a previous run of your script, thus the error.

Upvotes: 1

Alex Stylianos
Alex Stylianos

Reputation: 84

You can put a @ symbol in front of any function to suppress its error messages, warnings or notices.

@mkdir('/path/to/dir');

However, it is much better if you put an if statement so that you use that function only when you need it.

Upvotes: 4

Manibharathi
Manibharathi

Reputation: 945

Use file_exists($root) instead of is_dir in if condition.

if(!file_exists($root))
  mkdir("uploads/", 0777);

Upvotes: 1

Related Questions