is_dir How to check if dir exists avoid error message
I think I have missed something with the code below... Maybe you can help
<?php
$imageurl = $pagename1;
$imageurl = preg_replace('/\.asp/', ''.$g_sites_img2.'.jpg', $pagename1);
$filename = $_SERVER{'DOCUMENT_ROOT'}.'/images'.$imageurl;
if (file_exists($filename)) {
echo '<img src="'.$mainurl.'/images'.$imageurl.'" width="'.$g_sites_img2.'" align="left" />';
} else {
$imgremoteurl = $imgremoteurl.str_replace(' ', '_', strtolower($g_page_identify));
$imageurlfolder = dirname($pagename1);
mkdir($_SERVER{'DOCUMENT_ROOT'}.'/images'.$imageurlfolder, 0755, true);
copy('http://www.dominate-seo.com/images/'.$imgfolder.'/'.$imgremoteurl.'.jpg', $_SERVER{'DOCUMENT_ROOT'}.'/images'.$imageurl);
$thumb = new Imagick($_SER开发者_如何转开发VER{'DOCUMENT_ROOT'}.'/images'.$imageurl);
$thumb->scaleImage($g_sites_img2, 0);
$thumb->writeImage($_SERVER{'DOCUMENT_ROOT'}.'/images'.$imageurl);
$thumb->destroy();
}
?>
The code is suppose to check if a image exist. If the image does not exists it should create the image folder, but should not create it if it already exists. This is where my problem lies. If the folder exists it gives me an error
Warning: mkdir() [function.mkdir]: File exists in /home/game1/public_html/includes
If I refresh the page, the error message disappears when the file is created.
How can I write the script so that it checks the folder, if it exists it does not throw the error?
The is_dir() method returns false if the directory doesn't exist. Instead of just suppressing the error you could replace the mkdir() line with this check:
if(!is_dir($_SERVER{'DOCUMENT_ROOT'} . '/images' . $imageurlfolder)) {
// Create the directory since it doesn't exist.
mkdir($_SERVER{'DOCUMENT_ROOT'} . '/images' . $imageurlfolder, 0755, true);
}
You're showing all errors. For this operation, I think the easiest is to just try to make the folder and suppress your error messages by adding an '@' symbol in front of your mkdir:
@mkdir($_SERVER{'DOCUMENT_ROOT'}.'/images'.$imageurlfolder);
精彩评论