0

我正在尝试从帖子值创建一个目录,然后将图像插入该目录,但不知何故我遗漏了一些东西,无法弄清楚我做错了什么。我的代码

 <form method="post" action="img.php" enctype="multipart/form-data">
       F name
      <input type="text" name="fname"/>

       L Name
       <input type="text" name="lname"/>

      File 1
      <input type="file" name="image[]"> 

      File 2
      <input type="file" name="image[]"> 

      File 3
      <input type="file" name="image[]"> 

     File 4
     <input type="file" name="image[]"> 

     File 5
     <input type="file" name="image[]"> 

     File 6
     <input type="file" name="image[]"> 
   <input TYPE="submit" name="upload" value="Add Member"/>
 </form>

  <?php
   include 'dbc.php';
    $fname = $_POST['fname'];
    $lname = $_POST['lname'];
    if (!file_exists($_POST['fname'])) {
    $uploaddir = mkdir($_POST['fname']);
  }

for ($i=0;$i<20;$i++)
  {
   if($_FILES['image']['name'][$i])
   {
    $uploadfile = $uploaddir . basename($_FILES['image']['name'][$i]);
    $ext = strtolower(substr($uploadfile,strlen($uploadfile)-3,3));
    if (preg_match("/(jpg|gif|png|bmp)/",$ext))
    {
     move_uploaded_file($_FILES['image']['tmp_name'][$i], $uploadfile); 
    }
   }
  }

  $fname = mysql_real_escape_string($fname);
  $lname = mysql_real_escape_string($lname);
  $sql = "insert into image (fname, lname) values ('$fname','$lname')";
  $result = mysql_query($sql) or die ("Could not insert data into DB: " ;

  ?>

我使用相同的表单来发布图像并创建一个文件夹。

4

1 回答 1

0

mkdir返回布尔值,因此$uploaddir变量不包含您期望的文件夹名称

我的建议是这样的:

$fname = $_POST['fname'];
$uploaddir = $fname; // folder name
if (!is_dir($fname)) {
    mkdir($fname);
}

for($i = 0; $i < sizeof($_FILES['image']); $i++) {
    if($_FILES['image']['name'][$i]) {
        $uploadfile = $uploaddir . basename($_FILES['image']['name'][$i]);
        $ext = strtolower(substr($uploadfile, -3));
        if (preg_match("/(jpg|gif|png|bmp)/",$ext)) {
            move_uploaded_file($_FILES['image']['tmp_name'][$i], $uploadfile); 
        }
    }
}
于 2013-10-25T20:02:46.143 回答