我的脚本无法正确显示链接

发布于 2024-11-26 09:35:04 字数 6133 浏览 0 评论 0原文

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

我为我的图像主机编写了一个脚本,它将包含图像的 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 中的每个图像匹配的多个链接压缩(失败)

想法?

It's gonna be hard for me to explain the whole situation, but I'll try...

I made a script for my image host that unZips a Zip package with images in it to a certain location, renames the files to a random file name and outputs multiple links to the images. The last part is not working properly! I am unable to output multiple links to the images - It simply outputs one link to the image (the first one) and the rest is in the uploaded folder, but not listed as a link.

Same goes with generating a thumbnail for the just renamed images. Only one thumbnail is generated for the first image, and the rest of the images if being ignored.

This is how my code looks like:

<?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="« Upload more" />';
    echo '<br />';
    echo '</div>';
?>
</div>

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

I guess the problem is inside the foreach loop (it was a for loop a few days ago, but faced the same problems), but I can't seem to fix it. I'll reexplain in a short version:

  • I upload a Zip archive
  • Script unZips the archive
  • Script renames the extracted files
  • Thumbnail must be generated for all images that were in the Zip (fails)
  • Multiple links should be outputted matching every image the was in the Zip (fails)

Ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

—━☆沉默づ 2024-12-03 09:35:04

您出于两个不同的目的而重复使用变量 ($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 关键字...似乎没有必要。

You are re-using a variable ($filename) for two different purposes. At the top, add a line like this:

$file_list = array();

Later in the code, where you do this:

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

... change it to this:

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

Later in your code where you loop, use foreach instead:

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;

You've got a lot of other odd things going on in there, like using constants for HTML fragments. I think you should take another look at your process there and eliminate some of the unnecessary steps and variables. I see several global keywords used... none appear to be necessary.

饮惑 2024-12-03 09:35:04

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

file_exists($zip)

I fixed this problem just by removing the following code part:

file_exists($zip)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文