FunkyELF Posted October 16, 2006 Share Posted October 16, 2006 I thought of an exercize for me to put some of my newly learned PHP to use.I post on a couple of different forums and some allow for offsite avatars. I thought I could create a PHP script to serve up a random avatar.I think I got it working although I haven't tried it on any forums yet.I was hopeing someone could look at this and critisize it.What is the best way to just serve a file from PHP using passthru.All the examples I saw were with programs that were converting things or resizing them but none just giving the image as is.I figured I could use the cat command and it works but is there a better way than this?...[code]passthru("cat " . $file );[/code]Thanks,~EricI know I'm not sanitizing the $_GET['size'] variable and its going right into a command string...what is the best function to sanitize something like this?[code]<?php $dirname = dirname($_SERVER['SCRIPT_FILENAME']); // get the files in the same dir as this script. $files = scandir($dirname); // filter for only .jpg files foreach($files as $file){ if(substr($file,-4) == ".jpg") $pictures[] = $file; } // pick the random file $rfile = $pictures[mt_rand(0,count($pictures) - 1)]; header("Content-type: image/jpeg"); // resize if we need to if(isset($_GET['size'])) passthru("convert -resize {$_GET['size']}x{$_GET['size']} $rfile jpg:-"); else passthru("cat " . $rfile );?>[/code] Quote Link to comment Share on other sites More sharing options...
printf Posted October 16, 2006 Share Posted October 16, 2006 if get $_GET['size'] is a [b]integer[/b], then...[code]$size = intval ( $_GET['size'] );[/code]Also [b]glob()[/b], would be much better than scandir() and that foreach() your using to build the jpg array![code]$dirname = './images/';$pictures = glob ( $dirname . '*.jpg' );[/code]me! Quote Link to comment Share on other sites More sharing options...
FunkyELF Posted October 16, 2006 Author Share Posted October 16, 2006 [quote author=printf link=topic=111702.msg452829#msg452829 date=1161034405]if get $_GET['size'] is a [b]integer[/b], then...[code]$size = intval ( $_GET['size'] );[/code]Also [b]glob()[/b], would be much better than scandir() and that foreach() your using to build the jpg array![code]$dirname = './images/';$pictures = glob ( $dirname . '*.jpg' );[/code]me![/quote]Thanks, the fact that size should only be an int never occured to me.As far as that glob goes, I don't think my book went into it. Its things like that where I'll just wind up reinventing the wheel.Anyway, how about that cat command?...is that the right way to go about things using passthru? Quote Link to comment Share on other sites More sharing options...
FunkyELF Posted October 18, 2006 Author Share Posted October 18, 2006 [quote author=printf link=topic=111702.msg452829#msg452829 date=1161034405]Also [b]glob()[/b], would be much better than scandir() and that foreach() your using to build the jpg array![code]$dirname = './images/';$pictures = glob ( $dirname . '*.jpg' );[/code][/quote]Does glob only work on the current directory? Quote Link to comment Share on other sites More sharing options...
printf Posted October 18, 2006 Share Posted October 18, 2006 glob() works on the directory you give it, such as in my example './images/', so glob will create an array of documents found in that directory! if you don't give it a path './images/', then it will look in the current directory the script is running from!// get the files that are of the type [b]jpg[/b] in the directory [b]images[/b][code]$dirname = './images/';$pictures = glob ( $dirname . '*.jpg' );[/code]// get the files that are of the type [b]jpg[/b] in the current directory, that the script was called from[code]$dirname = '';$pictures = glob ( $dirname . '*.jpg' );// or written like so$pictures = glob ( '*.jpg' );[/code]me! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.