在PHP中使用数组创建目录
我有一个如下所示的数组结构。我想使用此数组在文件夹中创建一个文件夹。我该怎么做在PHP中使用数组创建目录,php,Php,我有一个如下所示的数组结构。我想使用此数组在文件夹中创建一个文件夹。我该怎么做 $folder=array(4) { [0]=> string(3) "img" [1]=> string(7) "dummies" [2]=> string(6) "slides" [3]=> string(6) "01.jpg" } 我试过这样做,但没有解决我的问题 $imglength=count($folder); $i=0; $fold=$i&
$folder=array(4) {
[0]=>
string(3) "img"
[1]=>
string(7) "dummies"
[2]=>
string(6) "slides"
[3]=>
string(6) "01.jpg"
}
我试过这样做,但没有解决我的问题
$imglength=count($folder);
$i=0;
$fold=$i<($imglength-1);
while(!file_exists($fold))
{
mkdir($fold);
}
$imglength=count($folder);
$i=0;
$fold=$i这是一种方法。也许这不是最有效的方法,但它确实有效
<?php
$folders = Array(
Array("img", "dummies", "slides", "01.jpg"),
Array("img_24", "dummy", "slideshows", "65.png")
);
for ($i=0;$i<count($folders);$i++) {
for ($j=0;$j<count($folders[$i]);$j++) {
$path .= $folders[$i][$j] . "/";
mkdir($path);
}
unset($path);
}
?>
它基本上读取数组并将每个名称放入一个变量中,该变量会扩展自身以创建路径。当然,JPEG文件不是创建的,但这是您可以使用的,因此,只需给它另一个数组值,称为“file”,而不是默认的“folder”定义。然后,当它读取定义时,会自动创建一个文件而不是文件夹,然后写入位图数据
编辑:对于文件创建,您可以使用文件内容,就像我上面解释的,我制作了一小段代码来演示其用法
<?php
$folders = Array(
Array("img", "dummies", "slides", Array("01.jpg", "11abcdefghijklmnopqrstuvxyz")),
Array("img_24", "dummy", "slideshows", Array("65.png", "22abcdefghijklmnopqrstuvxyz"))
);
for ($i=0;$i<count($folders);$i++) {
for ($j=0;$j<count($folders[$i]);$j++) {
$path .= (gettype($folders[$i][$j]) != "array" ? $folders[$i][$j] . "/" : $folders[$i][$j][0]);
if (gettype($folders[$i][$j]) != "array") {
mkdir($path);
}else{
file_put_contents($path, (isset($folders[$i][$j][1]) ? $folders[$i][$j][1] : "nil"));
}
}
unset($path);
}
?>
JPEG文件的第二个值用于文件内容。通过读取正在使用的文件,然后将其作为值放入,可以将位图数据放入其中。这样就可以正确创建图像。
<html>
<body>
<form method="post" enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file"><br>
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>
<?php
if ($_FILES["file"]["error"] > 0)
{
echo "Error: " . $_FILES["file"]["error"] . "<br>";
}
else
{
$tmp_name = $_FILES["file"]["tmp_name"];
}
$folders = array("img","dummies","slides");
foreach($folders as $folder)
{
$folder_path .= $folder."/";
while(!file_exists($folder_path))
{
mkdir($folder_path);
}
}
move_uploaded_file($tmp_name, "$folder_path/".$_FILES['file']['name']);
文件名:
试试看
<?php
$folder=array(array("img","dummies","slides","01.jpg"));
$imglength=count($folder);
$i=0;
$fold=$i<($imglength-1);
for($k=0;$k<$imglength;$k++)
{
$sub_array_count=count($folder[$k]);
$tmp='';
for($j=0;$j<$sub_array_count;$j++)
{
if($j==0)
{
mkdir($folder[$k][$j]);
}else
{
echo $folder[$k][($j-1)];
echo "<br>";
echo $tmp.=$folder[$k][($j-1)]."/";
mkdir($tmp.$folder[$k][$j]);
}
}
}
?>
$fold=$ifile\u存在($path\u from\u root.'/somedir/file/'.$fold)检查根目录文件夹是否存在数组映射('mkdir',$array)
?(如果file/dir存在,它将失败,但会继续)非常感谢!!!最后一件事啊,我想创建那个01.jpg文件夹,我想把那个图像文件放到那个文件夹里。。。请帮帮我,我也是。。。请……)嗯。如果希望有多个文件夹路径,则可以在基本阵列中创建多个阵列。然后只需为子数组添加另一个for循环,这样您就不必为每个子数组单独执行相同的for循环。非常感谢!!!最后一件事啊,我想创建那个01.jpg文件夹,我想把那个图像文件放到那个文件夹里。。。请帮帮我,我也是。。。请……)@user3048483图片将通过表单上传???@user3048483 ok为您更新代码,希望这是您一直在寻找的东西谢谢您宝贵的工作…:)