PHP Noob need help.

Discussion in 'PHP' started by thewindmaster, Jul 22, 2006.

  1. #1
    The following code apparently outputs two arrays, one called $directories and one called $files.

    In my template page there is "if (is_Array($directories))" and "foreach ($directories as $v)" and "if (is_Array($files))" and "foreach ($directories as $f)" to be able to display the page differently if the directories or files array is being used and to call variables with $v['FILENAME'].

    Everything works great. However, in order to get the "if (is_Array($files))" page looking the way I want I need to call a variable from the $directories array. I need access to the $v['FILENAME']. This variable is already in the $files array as $f['DIR'] but it adds ./ to the front and / to the back of the variable.

    How do I add a new variable to the $files array that is equal to $v['FILENAME'] or how do I strip the ./ and / from the $f['DIR'] variable in my templay file?

    Any help is greatly appreciated.


    <?
    /*

    activeGallery

    free script that makes photo album from images in current directory and sub-directories.

    Copyright (C) 2005 ActiveUnit.com

    This program is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public License
    as published by the Free Software Foundation; either version 2
    of the License, or (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

    */

    // GALLERY SETTINGS

    Define('DIR_CACHE', './tmp/'); // directory to store modified images (shoul have right to write files)
    Define('THUMBNAIL_WIDTH', '100'); // thumbnails width
    Define('THUMBNAIL_HEIGHT', '100'); // thumbnails height
    Define('IMAGES_ROW', 3); // images on row












    // ----------------------------------------------------------------------



















    $img=$_GET['img'];

    if (!Is_Dir(DIR_CACHE)) {
    mkdir(DIR_CACHE, 0777);
    }

    // IMAGE RESIZE AND SAVE TO FIT IN $new_width x $new_height
    if ($img!='' && file_exists($img)) {

    $w=$_GET['w'];
    $h=$_GET['h'];

    $thumb=strtolower(preg_replace('/\W/is', "_", "$img $w $h"));

    $changed=0;
    if (file_exists($img) && file_exists(DIR_CACHE.$thumb)) {
    $mtime1=filemtime(DIR_CACHE.$thumb);
    $mtime2=filemtime($img);
    if ($mtime2>$mtime1) $changed=1;
    } elseif (!file_exists(DIR_CACHE.$thumb)) {
    $changed=1;
    }

    if ($changed) {

    $filename=$img;
    $new_width=(int)@$w;
    $new_height=(int)@$h;
    $lst=GetImageSize($filename);
    $image_width=$lst[0];
    $image_height=$lst[1];
    $image_format=$lst[2];

    if ($image_format==1) {
    Header("Content-Type:image/gif");
    readfile($filename);
    exit;
    } elseif ($image_format==2) {
    $old_image=imagecreatefromjpeg($filename);
    } elseif ($image_format==3) {
    $old_image=imagecreatefrompng($filename);
    } else {
    exit;
    }

    if (($new_width!=0) && ($new_width<$image_width)) {
    $image_height=(int)($image_height*($new_width/$image_width));
    $image_width=$new_width;
    // $image_height=$new_height;
    }

    if (($new_height!=0) && ($new_height<$image_height)) {
    $image_width=(int)($image_width*($new_height/$image_height));
    $image_height=$new_height;
    // $image_width=$new_width;
    }


    // $new_image=imageCreate($image_width, $image_height); // for old GDlib
    $new_image=imageCreateTrueColor($image_width, $image_height);

    $white = ImageColorAllocate($new_image, 255, 255, 255);
    ImageFill($new_image, 0, 0, $white);

    // imagecopyresampled
    // imageCopyResized (for old GDlib)
    imageCopyResampled( $new_image, $old_image, 0, 0, 0, 0, $image_width, $image_height, imageSX($old_image), imageSY($old_image));

    imageJpeg($new_image, DIR_CACHE.$thumb);
    }


    Header("Content-type:image/jpeg");
    readfile(DIR_CACHE.$thumb);
    exit;
    }

    // ----------------------------------------------------------------------

    $init_dir="./";

    if (IsSet($HTTP_GET_VARS['dir'])) {
    $dir=$HTTP_GET_VARS['dir'];
    $dir=preg_replace("/\.+/", ".", $dir);
    }

    if (IsSet($dir) && ($dir!=$init_dir)) {
    $directory=$dir;
    $tmp=array_reverse(explode('/', $dir));
    array_shift($tmp);
    array_shift($tmp);
    $out['UP']=urlencode(implode('/', array_reverse($tmp))."/");
    } else {
    $directory=$init_dir;
    }

    $dir=opendir($directory);
    $k=0;
    $on_row=IMAGES_ROW;


    if (file_exists($directory."files.txt")) {
    $data=LoadFile($directory."files.txt");
    $lines=explode("\n", $data);
    for($i=0;$i<count($lines);$i++) {
    $ar=explode(' ', trim($lines[$i]));
    $tmp=array_shift($ar);
    $descriptions[$tmp]=implode(' ', $ar);
    }
    }

    while ($file=readdir($dir)) {
    if (Is_Dir($directory."/$file") && ($file!='tmp') && ($file!='.') && ($file!='..')) {
    $directories[]=array('FILENAME'=>$file, 'DIR'=>$directory, 'DESCRIPTION'=>$descriptions[$file], 'URL'=>urlencode($directory."$file/"));
    }

    if (!Is_file($directory."/$file")) continue;

    $ext=strtolower(substr($file, -3));

    if ($ext!='gif' && $ext!='jpg') continue;

    $rec=array();
    $rec["FILENAME"]=$file;

    $size=(int)(filesize($directory."/".$file)/1024);
    if ($size>1024) {
    $rec["SIZE"]=(((int)($size*100/1024))/100)." Mb";
    } else {
    $rec["SIZE"]=$size." Kb";
    }
    if (IsSet($descriptions[$file])) {
    $rec['DESCRIPTION']=$descriptions[$file];
    }
    $rec["DIR"]=$directory;
    if (($k+1)%$on_row==0) {
    $rec['NEXT_ROW']=1;
    }
    $k++;
    $files[]=$rec;
    }

    include('./template.php');

    // ----------------------------------------------------------------------

    function LoadFile($filename) {
    // loading file
    $f=fopen("$filename", "r");
    $data="";
    if ($f) {
    $data=fread($f, filesize($filename));
    fclose($f);
    }
    return $data;
    }


    ?>
     
    thewindmaster, Jul 22, 2006 IP
  2. tkroll

    tkroll Peon

    Messages:
    161
    Likes Received:
    16
    Best Answers:
    0
    Trophy Points:
    0
    #2
    The easiest and most readable way to strip those chars is:

    $temp = ltrim($f['DIR'], "./");
    $temp = rtrim($temp, "/");

    Is that what you need?

    Good luck,
    Ty
     
    tkroll, Jul 22, 2006 IP
  3. thewindmaster

    thewindmaster The Man with the Plan

    Messages:
    845
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I think so. Let me test it. Thanks.
     
    thewindmaster, Jul 22, 2006 IP
  4. thewindmaster

    thewindmaster The Man with the Plan

    Messages:
    845
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Works great. Awesome. Thanks.
     
    thewindmaster, Jul 22, 2006 IP