Languages :: PHP :: just tryin' to pass some value |
|||
| By: natbha |
Date: 11/05/2003 00:00:00 |
Points: 100 | Status: Answered Quality : Excellent |
|
In the code (garbage) below I need the html links (photos, print) to change the directory I'm reading from. It is so easy, why so hard for me? ---------------------------------- <body> <a href="#" onClick="$imgpath='photos'">photos</a> <a href="#" onClick="$imgpath='print'">print</a> <?php $imgpath = "images"; $handle = opendir( "$imgpath" ); $imgArray = array(); while($file = readdir($handle)) { if( $file != "." && $file != ".." ) { print("<a href='work.php?page=$imgpath&image="); print($file); print("'> + </a>\n</br>"); if($image == "") $image = $file; array_push( $imgArray, $file ); } } closedir( $handle ); print( "<img src=\"$imgpath/" . $image . "\">" ); ?> </body> |
|||
| By: VGR | Date: 11/05/2003 18:42:00 | Type : Comment |
|
| because either you've register_globals=Off or don't use proper coding. For instance in HTML <a href="#" onClick="$imgpath='photos'">photos</a> won't work at all, except if this is (it doesn't seem to be) in an echo <<<EOS...EOS; block is this script named 'work.php' ? if yes, then write it this way : <body> <?php echo <<<EOS <a href="#" onClick="$imgpath='photos'">photos</a> <a href="#" onClick="$imgpath='print'">print</a> EOS; // get the data eventually transmitted by your links below if (isset($_GET['imgpath'])) $imgpath=$_GET['imgpath']; if (isset($_GET['image'])) $image=$_GET['image']; //VGR11052003 MODified your code so that you don't crush transmitted value with default one if (!isset($imgpath)) $imgpath = "images"; // resume normal code (didn't change anything to it) $handle = opendir( "$imgpath" ); $imgArray = array(); while($file = readdir($handle)) { if( $file != "." && $file != ".." ) { print("<a href='work.php?page=$imgpath&image="); print($file); print("'> + </a>\n</br>"); if($image == "") $image = $file; array_push( $imgArray, $file ); } } closedir( $handle ); print( "<img src=\"$imgpath/" . $image . "\">" ); ?> </body> |
|||
| By: natbha | Date: 11/05/2003 19:22:00 | Type : Comment |
|
| Maybe the onClick isn't the best way to change the $imgpath value because that didn't work for me. If you have a suggestion, someway to use the $_GET in another way, maybe that would work. oh, and to note, the file is called work.php. |
|||
| By: VGR | Date: 11/05/2003 20:01:00 | Type : Comment |
|
| ok : what exactly do you try to achieve ? the link with page=$imgpath&image="); is a bit weird, no ? Is "page" supposed to be the "new path" ? And what do you mean by "change the directory I'm reading from" ? |
|||
| By: VGR | Date: 11/05/2003 20:05:00 | Type : Comment |
|
| I think you try to make an output of all images present in directories, navigate through directories etc but this isn't the way I would have done it. |
|||
| By: natbha | Date: 11/05/2003 20:47:00 | Type : Comment |
|
| I am trying to output links to images in certain directories. I was thinking, maybe I could use something like $_POST(index.php) $imgpath = "dirName"; But I don't know if that'd work, and I can't find any documentation that I understand for PHP. For all your help, I'm increasing the points to 100 thanks, nat |
|||
| By: VGR | Date: 11/05/2003 21:39:00 | Type : Answer |
|
| ok ok ok well you're on the right track. Let me see. first I would build links to images via "normal" URIs, not with parameters (after all they are accessible) and build only links with ?newpath= for directories. Thus I would use is_dir() on $file in your loop, inside the test "not . nor .." then perhaps you'll find enlightning those scripts I "made" (fixed) for other people : 1) this lists files having .htm extensions <?php // set this constant $directories=array('.'); //,'./subdir1'); // now proceed for ($i=0;$i<count($directories);$i++) { $directory = $directories[$i]; if($dir_handle = opendir($directory)) { while($file = readdir($dir_handle)) if (! is_dir($file)) // candidate ok : file if (($file <> '.') AND ($file <> '..')) { // file OK // check extension if ($a=strpos($file,'.htm')) { // takes also .html but also .htm.old for example } // else NOP, file ignored } // if file OK closedir($dir_handle); } // if dir opened OK } // for $i ?> 2) this more complex lists all files and enables to navigate up and down through directories. <?php $DEBUG=FALSE; if (isset($_GET['nextdir'])) $nextdir = $_GET['nextdir']; if (isset($_GET['userdir'])) $userdir = $_GET['userdir']; // for the first time only (session var afterwards) // delete the above line if the $userdir may be known as a session var since the beginning of the script (better security) if (!isset($nextdir)) $nextdir='/'; else $nextdir = urldecode($nextdir); // GIVEN : $userdir (=user name) is somewhat transmitted, via a session variable usually. session_start(); if (!isset($_SESSION['userdir'])) $_SESSION['userdir']=$_GET['userdir']; // first time it's in the URL... function nextdiris($path,$file) { GLOBAL $DEBUG; if ($file=='..') { // let's work if (($path=="")or($path=='/')) $poubStr='/'; else { if ($DEBUG) echo ".. for path='$path' "; //let's get over up level $zar=explode('/',$path); $j=count($zar)-1; while ($zar[$j]=="") $j--; $zar[$j]=""; // empty last component // no because array count stays unchanged... $poubStr=implode('/',$zar); $poubStr=$zar[0]; // can not be empty unless path was empty itself for ($j=1;$j<count($zar);$j++) if ($zar[$j]<>"") $poubStr.='/'.$zar[$j]; if ($DEBUG) echo ".. for path : result '$poubStr' "; } // if already at the top } else $poubStr=(($path=='/')?$file:$path.'/'.$file); return urlencode($poubStr); } // nextdiris String Function // dir to open ############################################################################################ $rootdir ='i:/www'; // no trailing slash // "d:/website_development/root"; $userrootdir = $rootdir.'/'.$userdir; if ($DEBUG) echo "userrootdir='$userrootdir' "; if (($nextdir=='/')or($nextdir=="")) { $diropen=$userrootdir; $curdir=$userdir; } else { $diropen=$userrootdir.'/'.$nextdir; $curdir=$userdir.$nextdir; } // selon le cas if ($DEBUG) echo "diropen= '$diropen' "; if ($DEBUG) echo "curdir= '$curdir' "; // end of dir to open ###################################################################################### // set strings ################ $total=0; $nbdir=0; $nbfile=0; $fdtotal=0; // Open and read directory ################################################################### //////////////////////////// html stuff normally //VGR12042003 ADDed this for test display echo "<TABLE>"; if ($handle = opendir($diropen)) { //VGR12042003 FIXed unnecessary quotes while ($file = readdir($handle)) { //VGR12042003 FIXed (removed) silly "false !== " if ($file != ".") { //VGR12042003 FIX parenthesing and chose clearer Boolean operator, FIXed indenting, ADDed .. case if (!( ($file=='..') AND ($nextdir=='/') )) { $checkdir = $diropen.'/'.$file; $isdir = is_dir($checkdir); // File or directory listing column ################################################################ //////////////////////////// html stuff normally //VGR12042003 ADDed this for test display echo "<TR><td>"; if ($isdir){ $a=nextdiris($nextdir,$file); if ($a<>'%2F') echo " <a href='$SCRIPT_NAME?nextdir=$a'><font color=red>".(($file != "..")?$file:'UP')."</font></a></b>"; //VGR12042003 ADDed colour to better test directories found echo "</font></font></td>"; } else { //if ($DEBUG) echo "file '$file' : curdir= '$curdir' "; echo " <a href='/$curdir/$file'>$file</a></b>"; echo "</font></font></td>"; } // File or directory size ###################################################################### echo "<td height='20' width='200' align='right'>"; echo "<font face='Arial, Helvetica, sans-serif'><font size='1'>"; $openfilesize = $diropen.'/'.$file; $fsize = filesize($openfilesize); if (($fsize =='0') AND (! $isdir)) { //ditto on test echo "0 bytes"; } else if($isdir) { // ditto on ==1 if ($a<>'%2F') echo "<b><u>Directory</u></b>"; } else { echo "".round($fsize/1.024)." bytes"; } // if isdir if ($isdir) $nbdir++; // ditto on ==1 $total += $fsize; if (! $isdir) $nbfile++; // ditto on ==0 $fdtotal++; } // if fsize and isdir echo "</font></font></td>"; } // if not current root and .. } // if not . //VGR12042003 ADDed this for test display echo "</tr>"; } // while //VGR12042003 ADDed this for test display echo "</TABLE>"; ?> |
|||
| By: natbha | Date: 19/05/2003 07:21:00 | Type : Comment |
|
| so, I didn't really use the code,b ut I like it, so I'll give ya the points. Thanks! nat |
|||
|
Do register to be able to answer |
|||
| Add This Article To: | |||
| |
|
|
|
| |
|
|
|








