mkdir中的警告以及如何显示所创建目录中的图像


warning in mkdir and how to display images from the directory created

我的程序本应在目录中为上传的图像创建一个文件夹,但给出了以下警告:

mkdir()[function.mkdir]:文件存在于C:''XAMP''examplep''htdocs''gallery''uploader3.php的第26行

这是代码:

<html>
<head>
<title> Sample1  - File Upload on Directory </title>
</head>
<body>
<div align="center">
<form action="uploader3.php" method="post" enctype="multipart/form-data" >
    <input type="hidden" name="MAX_FILE_SIZE" value="100000" />
        Create an Album (limited to 10 images): <br />
    <input type="file" name="uploadedfile[]"  /><br />
    <input type="file" name="uploadedfile[]"  /><br />
    <input type="file" name="uploadedfile[]"  /><br />
    <input type="file" name="uploadedfile[]"  /><br />
    <input type="file" name="uploadedfile[]"  /><br />
    <input type="file" name="uploadedfile[]"  /><br />
    <input type="file" name="uploadedfile[]"  /><br />
    <input type="file" name="uploadedfile[]"  /><br />
    <input type="file" name="uploadedfile[]"  /><br />
    <input type="file" name="uploadedfile[]"  /><br />
    <br />
    <input type="submit" value="Upload File"   />
</form>
</div>
<?php 
$target_path = "uploads1/";
if(!mkdir($target_path))
{
    die('Failed to create folders...');
}
else
{
    for($count = 0; $count < count($_FILES['uploadedfile']); $count++)
    {
        $target_path = $target_path . basename( $_FILES['uploadedfile']['name'][$count]); 
        if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'][$count], $target_path)) 
        {
            echo "The file ".  basename( $_FILES['uploadedfile']['name'][$count]). 
        " has been uploaded";
        } 
        else{
        echo "There was an error uploading the file, please try again!";
        }
    }
}
?>
</body>
</html>

修改以下代码:

if(!mkdir($target_path))
{
    die('Failed to create folders...');
}

至:

if(!file_exist($target_path)) {
  if(!mkdir($target_path))
  {
      die('Failed to create folders...');
  }
}

这将首先检查文件夹,如果它已经存在,则无需再次创建。

对于你的第二个问题,你需要将上传的图像名称存储到某个地方(我想DB是一个不错的选择),然后,你可以在任何你想要的地方显示它们。或者您可以使用以下代码在文件夹中搜索并显示它们:

$image_files = glob("uploads1/*.jpg");
foreach($image_files as $img) {
    echo "<img src='".$img."' /><br/>";
}

在尝试创建之前,您应该首先检查目录是否不存在

if (!file_exists($target_path))
    mkdir($target_path);
if (file_exists($target_path))
{
    // Further processing here
}
else
{
    // Could not create directory
}