【发布时间】:2016-03-12 10:01:28
【问题描述】:
每次上传新文件时,此应用程序都会创建一个新文件夹,并将这些文件插入到创建的文件夹中。 下面的代码工作正常,但我有点困惑。由于多次上传,我无法检查是否上传了某些内容 我失去了对创建文件夹的控制,因为即使我不上传文件(仅提交表单)也会创建它。 为什么我只提交表单时会创建文件夹?因为“UPLOAD_ERR_OK”在创建文件夹后检查表单(首先是“mkdir”,然后是“UPLOAD_ERR_OK”(但必须这样,因为循环([$i]!)))。 我想做的是把 UPLOAD_ERR_OK 放在开头,但我不能,因为它必须包含一个数组 [$i]!在单次上传中“if ($_FILES['img']['error'] == UPLOAD_ERR_OK)”工作正常,但是 不在 multi 中,因为数组。
下面的脚本有阶段:
- 创建文件夹
- 如果文件没有错误
- 上传文件到文件夹
这是错误的,因为即使我们上传空表单并检查表单,也会在开始时创建文件夹,所以它会创建一个文件夹并报错“UPLOAD_ERR_NO_FILE”。但是不可能先检查错误再创建文件夹,因为错误是在循环后检查数组。
if (!file_exists($pre_path)) // if no folder
{
if (mkdir($pre_path, 0777)) // create folder
{
for ($i=0; $i < count($_FILES['img']['name']) $i++) //LOOP
{
if ($_FILES['img']['error'][$i] == UPLOAD_ERR_OK) //if no error
{
//MOVE UPLOADED FILES[$i], QUERIES AND FUNCTIONS
}
elseif ( $img_error[$i] == UPLOAD_ERR_INI_SIZE) { }//show error
elseif ( $img_error[$i] == UPLOAD_ERR_FORM_SIZE) {} //show error
elseif ( $img_error[$i] == UPLOAD_ERR_PARTIAL) { } //show error
elseif ( $img_error[$i] == UPLOAD_ERR_NO_FILE) {} //show error
elseif ( $img_error[$i] == UPLOAD_ERR_NO_TMP_DIR) { } //show error
elseif ( $img_error[$i] == UPLOAD_ERR_CANT_WRITE) { } //show error
elseif ( $img_error[$i] == UPLOAD_ERR_EXTENSION) { }//show error
else { }//show error
}
}
}
我希望它是这样的: 1.如果文件没有错误(不是“UPLOAD_ERR_NO_FILE”) 2.创建文件夹 3. 上传文件到文件夹
if ($_FILES['img']['error'][$i] == UPLOAD_ERR_OK)
{
if (mkdir($pre_path, 0777))
{
for ($i=0; $i < count($_FILES['img']['name']) $i++)
{
//MOVE UPLOADED FILES[$i], QUERIES AND FUNCTIONS
}
}
}
elseif ( $img_error[$i] == UPLOAD_ERR_INI_SIZE) { }//show error
elseif ( $img_error[$i] == UPLOAD_ERR_FORM_SIZE) {} //show error
elseif ( $img_error[$i] == UPLOAD_ERR_PARTIAL) { } //show error
elseif ( $img_error[$i] == UPLOAD_ERR_NO_FILE) {} //show error
elseif ( $img_error[$i] == UPLOAD_ERR_NO_TMP_DIR) { } //show error
elseif ( $img_error[$i] == UPLOAD_ERR_CANT_WRITE) { } //show error
elseif ( $img_error[$i] == UPLOAD_ERR_EXTENSION) { }//show error
else { }//show error
我应该怎么做才能将“if ($_FILES['img']['error'][$i] == UPLOAD_ERR_OK)” 与 [$i] 数组放在一起并在 errros 之后创建一个文件夹被检查?我们不能将 mkdir 放在循环中,因为循环会创建很多文件夹。我只需要 1 个文件夹,所以它必须在循环之前。 :/ 太疯狂了。 完整代码:
if (isset($_SESSION['admin'], $_POST['upload_images']))
{
$img_tmp_name = $_FILES['img']['tmp_name'];
$img_name = $_FILES['img']['name'];
$img_error = $_FILES['img']['error'];
$img_type = $_FILES['img']['type'];
$img_size = $_FILES['img']['size'];
$image_quantity = count($img_name);
$error_text = array (
1 => 'The uploaded file exceeds the upload_max_filesize directive in php.ini',
2 => 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form',
3 => 'The uploaded file was only partially uploaded',
4 => 'No file was uploaded',
6 => 'Missing a temporary folder',
7 => 'Failed to write file to disk.',
8 => 'A PHP extension stopped the file upload.',
9 => 'file couldnt be moved!',
10 => 'file isnt uploaded',
11 => 'za duzy rozmiar pliku',
12 => 'obrazek musi byc w formacie JPEG',
13 => 'nie mozna stworzyc folderu',
14 => 'Query has not started, but folder has created with number: ',
15 => 'Unknown upload error'
);
$conn = lacz_bd();
if ($q = $conn->prepare('SELECT MAX(gallery_id) FROM galleries'))
{
$q->execute();
$q->bind_result($gallery_id);
$q->store_result();
while($q->fetch())
{
$improved_gallery_id = $gallery_id + 1;
}
$pre_path = ('./galerie/'.$improved_gallery_id);
//------------------------------- START UPLOAD ----------------------------------
if (!file_exists($pre_path))
{
if (mkdir($pre_path, 0777))
{
for ($i=0; $i < $image_quantity; $i++)
{
if ($img_error[$i] == UPLOAD_ERR_OK)
{
if ($img_type[$i] = 'image/jpeg') //to do: preg match instead
{
if ($img_size[$i] <= 2621440) //2,5MiB
{
if (is_uploaded_file($img_tmp_name[$i]))
{
$path = $pre_path.'/'.basename($img_name[$i]);
if (move_uploaded_file($img_tmp_name[$i], $path))
{
if ($q = $conn->prepare('INSERT INTO galleries (path, gallery_id) VALUES (?, ?)'))
{
$q->bind_param('si', $path, $improved_gallery_id);
$q->execute();
if ($q->affected_rows > 0)
{
image_compression($path, 800, 536);
echo '<a href="ustaw_miniature.php?m='.urlencode($img_name[$i]).'&gid='.urlencode($improved_gallery_id).'">';
echo '<img src="'. htmlspecialchars($path, ENT_QUOTES).'" width="80%" height="80%"></img></a><br />';
}
}
} else { echo $error_text[9]; }
} else { echo $error_text[10]; }
} else { echo $error_text[11]; }
} else { echo $error_text[12]; }
}
elseif ( $img_error[$i] == UPLOAD_ERR_INI_SIZE) { echo $error_text[1]; }
elseif ( $img_error[$i] == UPLOAD_ERR_FORM_SIZE) { echo $error_text[2]; }
elseif ( $img_error[$i] == UPLOAD_ERR_PARTIAL) { echo $error_text[3]; }
elseif ( $img_error[$i] == UPLOAD_ERR_NO_FILE) { echo $error_text[4]; }
elseif ( $img_error[$i] == UPLOAD_ERR_NO_TMP_DIR) { echo $error_text[6]; }
elseif ( $img_error[$i] == UPLOAD_ERR_CANT_WRITE) { echo $error_text[7]; }
elseif ( $img_error[$i] == UPLOAD_ERR_EXTENSION) { echo $error_text[8]; }
else { echo $error_text[15]; }
} // END FOR
}
} else { echo $error_text[14].' '.htmlspecialchars($improved_gallery_id, ENT_QUOTES);}
$q->free_result();
$q->close();
}
$conn->close();
}
我坐在电脑前等待帮助。谢谢。这对我很重要,因为我为开发人员制作了一个网站。
【问题讨论】:
标签: php arrays loops file-upload mkdir