首页 文章

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

提问于
浏览
1

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

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): 
<input type="file" name="uploadedfile[]" />
<input type="file" name="uploadedfile[]" />
<input type="file" name="uploadedfile[]" />
<input type="file" name="uploadedfile[]" />
<input type="file" name="uploadedfile[]" />
<input type="file" name="uploadedfile[]" />
<input type="file" name="uploadedfile[]" />
<input type="file" name="uploadedfile[]" />
<input type="file" name="uploadedfile[]" />
<input type="file" name="uploadedfile[]" />

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

  • 1

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

    if (!file_exists($target_path))
        mkdir($target_path);
    
    if (file_exists($target_path))
    {
        // Further processing here
    }
    else
    {
        // Could not create directory
    }
    
  • 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."' />
    "; }

相关问题