0

我有两个目录:一个有图像,另一个有 ZIP 文件。两个目录中的文件具有相同的名称,例如:1.zip、1.png

我像这样扫描每个文件夹:

$images    = 'screenshots/';
$scanned_images = array_diff(scandir($images), array('..', '.'));
$zips = 'download/';
$scanned_zips = array_diff(scandir($zips), array('..', '.'));

然后 :

foreach ($scanned_images as $value)
{
echo '<div class="portfolioItem">';
echo '<a href="screenshots/'.$value.'" class="zoom img" title="'.$value.'"      rel="portfolio">';
echo '<img src="screenshots/'.$value.'" class="portfolio-image" alt="'.$value.'" /> </a>';
foreach ($scanned_zips as $val)
{
echo '<div class="portfolioDescription">';
echo'<h4>Download:'.$val.'</h4>';
echo'<p><a href="download/'.$val.'">Click here to download</a></p>';
echo'</div></div>';
}
}

这不起作用。第一个目录中的每个图像都将在其描述中包含第二个目录的整个 zip 文件!

我也尝试将两个数组组合成一个数组并使用foreach ($result as list ($a, $b)),但as list总是出错。

如何克服这一点?

4

3 回答 3

0

在内部循环break;的末尾添加一条语句,这应该可以解决它。foreach您有两个foreach循环,因此下载会被多次列出。要解决此问题,

将您的代码更改为:

<?php
foreach ($scanned_images as $value)
{
echo '<div class="portfolioItem">';
echo '<a href="screenshots/'.$value.'" class="zoom img" title="'.$value.'"      rel="portfolio">';
echo '<img src="screenshots/'.$value.'" class="portfolio-image" alt="'.$value.'" /> </a>';
    foreach ($scanned_zips as $val)
    {
    echo '<div class="portfolioDescription">';
    echo'<h4>Download:'.$val.'</h4>';
    echo'<p><a href="download/'.$val.'">Click here to download</a></p>';
    echo'</div></div>';
    break; //exiting
    }
}

?>
于 2013-07-14T17:23:30.047 回答
0

不需要嵌套foreach。

用这个:

<?php

$images    = 'screenshots/';
$scanned_images = array_diff(scandir($images), array('..', '.'));
$zips = 'download/';
$scanned_zips = array_diff(scandir($zips), array('..', '.'));

foreach ($scanned_images as $value)
{
    $name = substr($value, 0, strrpos($value, '.'));
    $pos = array_search($name.'.zip', $scanned_zips);
    if($pos != null){
        echo '<div class="portfolioItem">';
        echo    '<a href="'.$images.$value.'" class="zoom img" title="'.$value.'" rel="portfolio">';
        echo        '<img src="'.$images.$value.'" class="portfolio-image" alt="'.$value.'" />';
        echo    '</a>';
        echo    '<div class="portfolioDescription">';
        echo        '<h4>Download:'.$scanned_zips[$pos].'</h4>';
        echo        '<p><a href="'.$zips.$scanned_zips[$pos].'">Click here to download</a></p>';
        echo    '</div>';
        echo '</div>';
    }
}
于 2013-07-14T17:26:50.320 回答
0

一种方法是按名称散列文件而不使用扩展名。然后使用相同的密钥检索图像数据和 zip 数据。例子:

$scanned_images = array('1.png', '2.png');
$scanned_zips = array('1.zip', '2.zip');

//Should be like that after hashing
$scanned_images = array('1' => '1.png', '2' => '2.png');
$scanned_zips = array('1' => '1.zip', '2' => '2.zip');

所以代码可能是:

function get_file_name($path) {
    $name = basename($path);
    $name = substr($name, 0, strrpos($name, '.'));
    return $name;
}

function hash_files_by_name($items) {
    $hashed = array();
    foreach($items as $item) {
        $name = get_file_name($item);
        $hashed[$name] = $item;
    }

    return $hashed;
}


$scanned_images = array('1.png', '2.png'); // get images directory filesnames
$scanned_zips = array('1.zip', '2.zip'); // get zips directory filenames.

$imgs = hash_files_by_name($scanned_images);
$zips = hash_files_by_name($scanned_zips);

foreach ($imgs as $key=>$value)
{
    echo '<div class="portfolioItem">';
    echo '<a href="screenshots/'.$value.'" class="zoom img" title="'.$value.'"      rel="portfolio">';
    echo '<img src="screenshots/'.$value.'" class="portfolio-image" alt="'.$value.'" /> </a>';
    if(isset($zips[$key])) {
        echo '<div class="portfolioDescription">';
        echo'<h4>Download:'.$zips[$key].'</h4>';
        echo'<p><a href="download/'.$zips[$key].'">Click here to download</a></p>';
        echo'</div></div>';
    }
}
于 2013-07-14T17:23:00.680 回答