PHP文件名单击即可下载

时间:2014-08-25 07:30:08

标签: php html

您好我需要一些帮助,在每张照片下添加文件名,并且如果可能的话,使名称可以点击以下载照片。文件名应该是照片保存的名称,例如如果照片是123.jpg我希望名称显示为123,当我点击它时应该保存它...

 <?php
        # SETTINGS
        $max_width = 800;
        $max_height = 600;
        $per_page = 10;

        $page = $_GET['page'];

        $has_previous = false;
        $has_next = false;

        function getPictures() {
            global $page, $per_page, $has_previous, $has_next;
            if ( $handle = opendir(".") ) {
                $lightbox = rand();
                echo '<ul id="pictures">';

                $count = 0;
                $skip = $page * $per_page;

                if ( $skip != 0 )
                    $has_previous = true;

                while ( $count < $skip && ($file = readdir($handle)) !== false ) {
                    if ( !is_dir($file) && ($type = getPictureType($file)) != '' )
                        $count++;
                }
                $count = 0;
                while ( $count < $per_page && ($file = readdir($handle)) !== false ) {
    if ( !is_dir($file) && ($type = getPictureType($file)) != '' ) {

        // make the thumbs directory if it doesn't already exist
        if ( ! is_dir('thumbs') ) {
            mkdir('thumbs');
        }
        // make a thumbnail if it doesn't already exist
        if ( ! file_exists('thumbs/'.$file) ) {
            makeThumb( $file, $type );
        }

        // create a link to $file, add the thumbnail
        echo '<li><a href="' . $file . '">';
        echo '<img src="thumbs/'.$file.'" alt="" /></a></li>';
        $count++;
echo '<a href="$file">';
echo substr($file,strlen($folder),strpos($file, '.')-strlen($folder));
echo '</a>';
    }
}
                echo '</ul>';

                while ( ($file = readdir($handle)) !== false ) {
                    if ( !is_dir($file) && ($type = getPictureType($file)) != '' ) {
                        $has_next = true;
                        break;
                    }
                }
            }
        }

        function getPictureType($file) {
            $split = explode('.', $file); 
            $ext = $split[count($split) - 1];
            if ( preg_match('/jpg|jpeg/i', $ext) ) {
                return 'jpg';
            } else if ( preg_match('/png/i', $ext) ) {
                return 'png';
            } else if ( preg_match('/gif/i', $ext) ) {
                return 'gif';
            } else {
                return '';
            }
        }

        function makeThumb( $file, $type ) {
            global $max_width, $max_height;
            if ( $type == 'jpg' ) {
                $src = imagecreatefromjpeg($file);
            } else if ( $type == 'png' ) {
                $src = imagecreatefrompng($file);
            } else if ( $type == 'gif' ) {
                $src = imagecreatefromgif($file);
            }
            if ( ($oldW = imagesx($src)) < ($oldH = imagesy($src)) ) {
                $newW = $oldW * ($max_width / $oldH);
                $newH = $max_height;
            } else {
                $newW = $max_width;
                $newH = $oldH * ($max_height / $oldW);
            }
            $new = imagecreatetruecolor($newW, $newH);
            imagecopyresampled($new, $src, 0, 0, 0, 0, $newW, $newH, $oldW, $oldH);
            if ( $type == 'jpg' ) {
                imagejpeg($new, 'thumbs/'.$file);
            } else if ( $type == 'png' ) {
                imagepng($new, 'thumbs/'.$file);
            } else if ( $type == 'gif' ) {
                imagegif($new, 'thumbs/'.$file);
            }
            imagedestroy($new);
            imagedestroy($src);
        }
    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" dir="ltr" lang="en-US">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UFT-8" />
    <title>Pictures</title>
    <link rel="stylesheet" href="css/lightbox.css" type="text/css" media="screen" />
    <style type="text/css">
    body {
        width:780px;
        margin:0 auto;
    }
    #pictures li {
        float:left;
        height:<?php echo ($max_height + 10); ?>px;
        list-style:none outside;
        width:<?php echo ($max_width + 10); ?>px;
        text-align:center;
    }
    img {
        border:0;
        outline:none;
    }
    .prev {
        float:left;
    }
    .next {
        float:right;
    }
    </style>
    </head>
    <body>

    <?php getPictures(); ?>

    <div style="clear:both"></div>

    <?php
        if ( $has_previous )
            echo '<p class="prev"><a href="?page='.($page - 1).'">&larr; Previous Page</a></p>';

        if ( $has_next )
            echo '<p class="next"><a href="?page='.($page + 1).'">Next Page &rarr;</a></p>';
    ?>

    <div style="clear:both"></div>

    <script type="text/javascript" src="js/prototype.js"></script>
    <script type="text/javascript" src="js/scriptaculous.js?load=effects,builder"></script>
    <script type="text/javascript" src="js/lightbox.js"></script>
    </body>
    </html>

1 个答案:

答案 0 :(得分:1)

目前,您使用的代码会为每个图像生成缩略图,但它不会创建指向图像的链接,这是您需要能够单击并加载图片的链接。还有一些额外的代码,它们来自不同的脚本,可以在页面中添加不需要的html。

要添加图片链接,您需要使用以下代码替换代码的相应部分:

while ( $count < $per_page && ($file = readdir($handle)) !== false ) {
    if ( !is_dir($file) && ($type = getPictureType($file)) != '' ) {

        // make the thumbs directory if it doesn't already exist
        if ( ! is_dir('thumbs') ) {
            mkdir('thumbs');
        }
        // make a thumbnail if it doesn't already exist
        if ( ! file_exists('thumbs/'.$file) ) {
            makeThumb( $file, $type );
        }

        // create a link to $file, add the thumbnail
        echo '<li><a href="' . $file . '">';
        echo '<img src="thumbs/'.$file.'" alt="" /></a></li>';
        $count++;
    }
}