我的脚本没有正确显示链接

时间:2011-07-26 11:31:39

标签: php html zip archive unzip

我很难解释整个情况,但我会尝试......

我为我的图像主机创建了一个脚本,将Zip包中的图像解压缩到某个位置,将文件重命名为随机文件名并输出多个链接到图像。最后一部分工作不正常!我无法输出多个链接到图像 - 它只是输出一个链接到图像(第一个),其余链接在上传文件夹中,但没有列为链接。

为刚刚重命名的图像生成缩略图也是如此。仅为第一个图像生成一个缩略图,如果忽略则为其余图像生成。

这就是我的代码的样子:

<?php
session_start();
include('includes/imgit.class.php');
$IMGit = new imgit();

/**
* @ignore
*/
if (!defined('IN_IMGIT'))
{
    exit;
}

$IMGit->error_report(true);
$IMGit->disable(false);
$IMGit->ieNote(true);

if (isset($_POST['zipsent']) || $_POST['zipsent'] == true && isset($_FILES['archive']))
{   
    if ($_FILES['archive']['size'] <= MAX_ZIPSIZE)
    {
        // Main variables
        $key        = $IMGit->random_key(10);
        $move_zip   = move_uploaded_file($_FILES['archive']['tmp_name'], ZIP_PATH . $key . $_FILES['archive']['name']); 
        $zip        = ZIP_PATH . $key . $_FILES['archive']['name'];
        $extension  = substr($zip, -3);
        $filename   = $IMGit->zipContent($zip); // array
        $url        = str_replace('www.', '', $IMGit->generate_site_url());

        // ZIP limit is 100 images  
        if (sizeof($filename) <= 100)
        {
            // Only ZIP archives
            if ($extension == 'zip')
            {   
                if ($filename)
                {
                    foreach($filename as $key => $value)
                    {
                        // Get extension
                        $image_extension = substr($value, -3);
                        $image_extension = (strtoupper($image_extension)) ? strtolower($image_extension) : $image_extension;
                        $image_extRule   = $image_extension == JPG || $image_extension == JPEG || $image_extension == GIF || $image_extension == PNG || 
                                           $image_extension == BMP || $image_extension == ICO;

                        if ($image_extRule)
                        {
                            // Set variables and do some processing
                            $unZip       = $IMGit->unZip($zip, IMAGES_PATH);
                            $url         = str_replace('www.', '', $IMGit->generate_site_url());
                            $image_name  = $IMGit->random_key(7) . $value;
                            $image_name  = (strpos($image_name, ' ') !== false) ? str_replace(' ', '', $image_name) : $image_name;

                            if (file_exists(IMAGES_PATH . $filename[$key]))
                            {
                                // Rename extracted files
                                $rename = rename(IMAGES_PATH . $filename[$key], IMAGES_PATH . $image_name);

                                if ($rename && file_exists($zip) && sizeof($image_name))
                                {
                                    // Delete ZIP
                                    unlink($zip);   

                                    // Set URL variables
                                    $image_urls = $url . IMAGES_PATH . $image_name; 
                                    $image      = IMAGES_PATH . $image_name;    

                                    // Generate a thumbnail
                                    $IMGit->generate_thumbnail($image_urls, $image_name, THUMBNAIL_SIZE, THUMBNAIL_SIZE, true, 'file', false, false, THUMBS_PATH);
                                    $thumb_urls = $url . THUMBS_PATH . $image_name;

                                    $filename[] = array('direct' => $image_urls, 'thumb' => $thumb_urls);
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}
else 
{
    header('Location: index.php');  
}

include('includes/header.php');
{
    if      ($_FILES['archive']['size'] > MAX_ZIPSIZE) { echo '<span id="home-info">The ZIP archive is bigger than 100 MB.</span>'; }
    else if ($extension != 'zip') { echo '<span id="home-info">Only ZIP archives are upload ready.</span>'; }
    else if (sizeof($filename) > 100) { echo '<span id="home-info">The number of the images inside the archive was more than 100.</span>'; }
    else if (!$image_extRule) { echo '<span id="home-info">The extensions inside the ZIP did not match our allowed extension list.</span>'; unlink($zip); } // unlink zip if failed
    else    { echo '<span id="home-info">Image(s) was/were successfully uploaded!</span>'; }
}
?>
</div>
<br /><br /><br />
<a href="index.php"><img src="css/images/site-logo.jpg" id="logo" /></a>
<br /><br /><br /><br /><br />
</div>

<div id="box">
<?php   
    global $filename, $image_urls, $thumb_urls;

    echo '<br />';
    echo '<div id="links">';
    echo '<table>';

    echo LINKS_DIRECT;
    for($i = 0; $i < sizeof($filename); $i++) { echo $filename[$i]['direct'] . "\n"; }
    echo LINKS_CLOSE;

    echo LINKS_THUMB;
    for($i = 0; $i < sizeof($filename); $i++) { echo $filename[$i]['thumb'] . "\n"; }
    echo LINKS_CLOSE;

    echo LINKS_BBCODE;
    for($i = 0; $i < sizeof($filename); $i++) { echo '[IMG]' . $filename[$i]['direct'] . '[/IMG]' . "\n"; } 
    echo LINKS_CLOSE;

    echo LINKS_HTML;
    for($i = 0; $i < sizeof($filename); $i++) { echo '<a href="' . $filename[$i]['direct'] . '"><img src="' . $filename[$i]['thumb'] . '" /></a>' . "\n"; }
    echo LINKS_CLOSE;

    echo '</table>';
    echo '<br />';
    echo '<input type="reset" id="resetbtn-remote" class="button-sub" value="&laquo; Upload more" />';
    echo '<br />';
    echo '</div>';
?>
</div>

<?php include('includes/footer.php'); ?>
</div>
</body>
</html>

我猜问题是在foreach循环内部(几天前它是for循环,但面临同样的问题),但我似乎无法修复它。我将在短版本中重新解释:

  • 我上传了一个Zip档案
  • 脚本解压缩存档
  • 脚本重命名提取的文件
  • 必须为Zip(失败)中的所有图像生成缩略图
  • 应输出多个链接,与Zip中的每个图像匹配(失败)

想法?

2 个答案:

答案 0 :(得分:1)

您正在重复使用变量($ filename)用于两个不同的目的。在顶部,添加如下所示的行:

$file_list = array();

稍后在代码中,您执行此操作:

$filename[] = array('direct' => $image_urls, 'thumb' => $thumb_urls);

...改为:

$file_list[] = array('direct' => $image_urls, 'thumb' => $thumb_urls);

稍后在您循环的代码中,请改为使用foreach

echo LINKS_DIRECT;
foreach ($file_list as $this_file)
    echo $this_file['direct'] . "\n";
echo LINKS_CLOSE;

echo LINKS_THUMB;
foreach ($file_list as $this_file)
    echo $this_file['thumb'] . "\n";
echo LINKS_CLOSE;

echo LINKS_BBCODE;
foreach ($file_list as $this_file)
    echo '[IMG]' . $this_file['direct'] . '[/IMG]' . "\n";
echo LINKS_CLOSE;

echo LINKS_HTML;
foreach ($file_list as $this_file)
    echo '<a href="' . $this_file['direct'] . '"><img src="' . $this_file['thumb'] . '" /></a>' . "\n";
echo LINKS_CLOSE;

你还有很多其他奇怪的事情,比如使用HTML片段的常量。我认为你应该再看看你的过程并消除一些不必要的步骤和变量。我看到使用了几个global个关键字......似乎没有必要。

答案 1 :(得分:0)

我通过删除以下代码部分修复了这个问题:

file_exists($zip)