I want to add image in directory. The directory makes dynamically. But there is an error while uploading the image in directory and image can’t upload due to this error the error is given below:
Warning: mkdir(): File exists in C:\wamp\www\test\index.php on line 21
My code is here:
<body>
<form method="post" action="" enctype="multipart/form-data">
<input type="file" name="filename" id="filename" />
<input type="submit" name="pic" />
</form>
</body>
</html>
<?php
if(isset($_POST['pic'])){
$comimages = $_FILES['filename']['tmp_name'];
$targetpath = mkdir("pageimage/pageid");
$compath = $targetpath."/".$_FILES['filename']['name'];
$comFileType=$_FILES['filename']['type'];
$comFileSize=$_FILES['filename']['size'];
$comFileSize=$comFileSize/1024;
if($comFileSize<1000)
{
$arrFileType=array("image/jpeg","image/png","image/gif","image/bmp");
if(in_array($comFileType,$arrFileType))
{
move_uploaded_file($comimages,$compath);
}
else
{
echo("invalid file format");
}
}
else
{
echo("File Size Error");
}
}
?>
The clue is in the error. The directory you are trying to create with
$targetpath = mkdir("pageimage/pageid");already exists… so you can’t make it again!I would suggest doing a quick file exists check before trying to make it. There is a function for that: file_exists()
Also,
mkdir()returns a boolean (success or fail); not a file directory, so you won’t be able to use your$targetpathvariable as you expect.Try this instead…