我正在尝试从post值创建一个目录,然后将图像插入到该目录但不知何故我错过了什么,无法弄清楚我做错了什么。我的代码
<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: " ;
?>
我使用相同的表单发布图片并创建文件夹。
答案 0 :(得分: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);
}
}
}