很抱歉,如果我的标题错了。
让我在这里详细解释。
我想将每个文件名以数字方式存储在php
变量中,如
$image0 = filename.jpg
$image1 = filename.jpg
$image2 = filename.jpg
$image3 = filename.jpg
等......
这个filename.jpg是从for循环生成的,下面是我的代码,当前的OP和预期的OP。
$t = glob("/var/www/localhostmyproject.com/media/catalog/product/uploads/".$productnumber."/".$productnumber."*", GLOB_BRACE);
print_r($t);
for ($i = 0; $i <= 5; $i++) {
//$t =asort($t);
$proinfo = $t[$i];
$path_parts = pathinfo($proinfo);
echo $path_parts['basename'], "\n";
echo $imagename = "/uploads/".$productnumber."/".$path_parts['basename']."\n\n";
}
当前OP
Array
(
[0] => /var/www/localhostmyproject.com/media/catalog/product/uploads/8573228/8573228a.jpg
[1] => /var/www/localhostmyproject.com/media/catalog/product/uploads/8573228/8573228b.jpg
[2] => /var/www/localhostmyproject.com/media/catalog/product/uploads/8573228/8573228c.jpg
[3] => /var/www/localhostmyproject.com/media/catalog/product/uploads/8573228/8573228d.jpg
}
8573228a.jpg
/uploads/8573228/8573228a.jpg
8573228b.jpg
/uploads/8573228/8573228b.jpg
8573228c.jpg
/uploads/8573228/8573228c.jpg
8573228d.jpg
/uploads/8573228/8573228d.jpg
预期OP
Array
(
[0] => /var/www/localhostmyproject.com/media/catalog/product/uploads/8573228/8573228a.jpg
[1] => /var/www/localhostmyproject.com/media/catalog/product/uploads/8573228/8573228b.jpg
[2] => /var/www/localhostmyproject.com/media/catalog/product/uploads/8573228/8573228c.jpg
[3] => /var/www/localhostmyproject.com/media/catalog/product/uploads/8573228/8573228d.jpg
}
8573228a.jpg
/uploads/8573228/8573228a.jpg
$image0 = "/uploads/8573228/8573228a.jpg";
8573228b.jpg
/uploads/8573228/8573228b.jpg
$image1="/uploads/8573228/8573228b.jpg";
8573228c.jpg
/uploads/8573228/8573228c.jpg
$image2="/uploads/8573228/8573228c.jpg";
8573228d.jpg
/uploads/8573228/8573228d.jpg
$image3="/uploads/8573228/8573228d.jpg";
所以我可以在mysql查询中使用这个变量$image0,$image1,$image2,$image3
来插入数据。
我试图在echo $imagearr
之后将此行添加到for循环中。但它没有显示出预期的OP。
echo $image.$i = $path_parts[filename].$i."\n";
请让我知道如何实现这一目标。
修改
将$imagearr
变量名更新为$imagename
。它可能会混淆数组$t
**
答案 0 :(得分:4)
您可以通过分配动态变量(Variable Variables,因为Darren正确评论)来实现它,如下所示:
$files = ['sunset.jpg', 'moon.jpg', 'jupiter.png', 'banana.gif'];
$count = 0;
foreach($files as $file) {
${'file' . $count} = $file;
$count++;
}
// output gives you 4 strings
var_dump($file0, $file1, $file2, $file3);
答案 1 :(得分:0)
由于您已经获得了多个文件名并上传了目录路径,而您只需需要分配给自动增量变量。
您只需添加以下内容:
echo $image.$i=$imagename; // it will give desired output '$image2="/uploads/8573228/8573228c.jpg";'
您的完整代码:
$t = glob("/var/www/localhostmyproject.com/media/catalog/product/uploads/".$productnumber."/".$productnumber."*", GLOB_BRACE);
print_r($t);
for ($i = 0; $i <= 5; $i++) {
//$t =asort($t);
$proinfo = $t[$i];
$path_parts = pathinfo($proinfo);
echo $path_parts['basename'], "\n";
echo $imagename= "/uploads/".$productnumber."/".$path_parts['basename']."\n\n";
echo $image.$i = $imagename; // it will give desired output '$image2="/uploads/8573228/8573228c.jpg";'
}
答案 2 :(得分:0)
试试这个例子:
<?php
$array = array("variable1" => "value1","variable2" => "value2", "variable3" => "value3");
extract($array);
var_dump($variable1);
var_dump($variable2);
var_dump($variable3);
?>
输出:
string(6) "value1" string(6) "value2" string(6) "value3"
在您的情况下,以下代码应该有效:
$t = glob("/var/www/localhostmyproject.com/media/catalog/product/uploads/".$productnumber."/".$productnumber."*", GLOB_BRACE);
$images = array();
for ($i = 0; $i <= 5; $i++) {
$proinfo = $t[$i];
$path_parts = pathinfo($proinfo);
$imagename = "/uploads/".$productnumber."/".$path_parts['basename'];
$images['image'.$i] = $imagename;
}
extract($images);
var_dump($image0);
var_dump($image1);
var_dump($image2);
var_dump($image3);
var_dump($image4);
var_dump($image5);
?>
答案 3 :(得分:-1)
将此代码添加到您的代码中,您可以获得输出:
echo $imagearr = "/uploads/".$productnumber."/".$path_parts['basename']."\n\n";
echo $image.$i=$imagearr;