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

时间:2012-01-07 09:48:22

标签: php image mkdir

我的程序应该为目录上的上传图像创建一个文件夹,但会发出以下警告:

  

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

以下是代码:

<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>

2 个答案:

答案 0 :(得分:2)

修改以下代码:

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

为:

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

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

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

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

答案 1 :(得分:1)

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

if (!file_exists($target_path))
    mkdir($target_path);

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