我在哪里放这个PHP代码?

时间:2012-10-29 01:28:25

标签: php image

我正在尝试设置一个网页来显示图片,但它似乎无法正常工作。

这是代码:

    <?php
$files = glob("images/*.*");
for ($i=1; $i<count($files); $i++)
{
    $num = $files[$i];
    echo '<img src="'.$num.'" alt="random image">'."&nbsp;&nbsp;";
    }
?>

如果代码应该有效,我会把它放在哪里? 如果没有,有更好的方法吗?

4 个答案:

答案 0 :(得分:3)

您需要将此代码放在包含名为“images”的目录的目录中。名为“images”的目录还需要具有*.*名称格式的文件。肯定有更好的方法可以做你想做的事情。这将使用包含您要显示的所有图像的数据库。

如果这不符合您的要求,您必须更具描述性。我不知道你想做什么,我从你向我们展示的代码中得到的就是将名为“images”的目录中的每个文件渲染为图像。

但是,如果这篇文章的这一点只是简单地问“我如何执行PHP?”,请进行一些搜索,从不打扰我们这样的问题。

@zerkms注意到的另一件事是你的for ..循环在迭代1($i = 1)处开始。这意味着将跳过数组中的结果。

for ($i = 0; $i < count($files); $i++) {

答案 1 :(得分:1)

此代码段迭代目录images /中的文件,并回显其<img>标记中包含的文件名。你不想把它放在你想要的图像吗?

答案 2 :(得分:1)

这将进入您列出图像的images.php文件夹的父目录中的PHP文件(例如images)。您还可以使用以下语法简化循环(并更正它,因为数组索引应从0开始,而不是1):

<?php
foreach (glob("images/*.*") as $file){
    echo '<img src="'.$file.'" alt="random image">&nbsp;&nbsp;';
}
?>

答案 3 :(得分:0)

/**
* Lists images in any folder as long as it's inside your $_SERVER["DOCUMENT_ROOT"].
* If it's outside, it's not accessible.
* Returns false and warning or array() like this:
* 
* <code>
* array('/relative/image/path' => '/absolute/image/path');
* </code>
* 
* @param string $Path
* @return array/bool
*/
function ListImageAnywhere($Path){
    // $Path must be a string.
    if(!is_string($Path) or !strlen($Path = trim($Path))){
        trigger_error('$Path must be a non-empty trimmed string.', E_USER_WARNING);
        return false;
    }
    // If $Path is file but not folder, get the dirname().
    if(is_file($Path) and !is_dir($Path)){
        $Path = dirname($Path);
    }
    // $Path must be a folder.
    if(!is_dir($Path)){
        trigger_error('$Path folder does not exist.', E_USER_WARNING);
        return false;
    }
    // Get the Real path to make sure they are Parent and Child.
    $Path = realpath($Path);
    $DocumentRoot = realpath($_SERVER['DOCUMENT_ROOT']);
    // $Path must be inside $DocumentRoot to make your images accessible.
    if(strpos($Path, $DocumentRoot) !== 0){
        trigger_error('$Path folder does not reside in $_SERVER["DOCUMENT_ROOT"].', E_USER_WARNING);
        return false;
    }
    // Get the Relative URI of the $Path base like: /image
    $RelativePath = substr($Path, strlen($DocumentRoot));
    if(empty($RelativePath)){
        // If empty $DocumentRoot ===  $Path so / will suffice
        $RelativePath = DIRECTORY_SEPARATOR;
    }
    // Make sure path starts with / to avoid partial comparison of non-suffixed folder names
    if($RelativePath{0} != DIRECTORY_SEPARATOR){
        trigger_error('$Path folder does not reside in $_SERVER["DOCUMENT_ROOT"].', E_USER_WARNING);
        return false;
    }
    // replace \ with / in relative URI (Windows)
    $RelativePath = str_replace('\\', '/', $RelativePath);
    // List files in folder
    $Files = glob($Path . DIRECTORY_SEPARATOR . '*.*');
    // Keep images (change as you wish)
    $Files = preg_grep('~\\.(jpe?g|png|gif)$~i', $Files);
    // Make sure these are files and not folders named like images
    $Files = array_filter($Files, 'is_file');
    // No images found?!
    if(empty($Files)){
        return array(); // Empty array() is still a success
    }
    // Prepare images container
    $Images = array();
    // Loop paths and build Relative URIs
    foreach($Files as $File){
        $Images[$RelativePath.'/'.basename($File)] = $File;
    }
    // Done :)
    return $Images; // Easy-peasy, general solution!
}

// SAMPLE CODE COMES HERE
// If we have images...
if($Images = ListImageAnywhere(__FILE__)){ // <- works with __DIR__ or __FILE__
    // ... loop them...
    foreach($Images as $Relative => $Absolute){
        // ... and print IMG tags.
        echo '<img src="', $Relative, '" >', PHP_EOL;
    }
}elseif($Images === false){
    // Error
}else{
    // No error but no images
}

请尝试使用此尺寸。评论不言自明。