PHP - Random Photo From All Sub Folders
G'day, on the home page of a website i'm building, i'm wanting to have a random photo that looks at all the subfolders of the gallery directory, but the best I can achieve is just one subfolder.
Here is the code, if anyone can assist, that'll be appreciated. <?php function getRandomFromArray($ar) { mt_srand( (double)microtime() * 1000000 ); $num = array_rand($ar); return $ar[$num]; } function getImagesFromDir($path) { $images = array(); if ( $img_dir = @opendir($path) ) { while ( false !== ($img_file = readdir($img_dir)) ) { // checks for gif, jpg, png if ( preg_match("/(\.gif|\.jpg|\.png)$/", $img_file) ) { $images[] = $img_file; } } closedir($img_dir); } return $images; } $root = ''; // If images not in sub directory of current directory specify root //$root = $_SERVER['DOCUMENT_ROOT']; $path = 'gallery/topic1/'; // Obtain list of images from directory $imgList = getImagesFromDir($root . $path); $img = getRandomFromArray($imgList); ?> <center><a href="?GoTo=Photo Gallery"><img src="<?php echo $path . $img ?>" alt="" height="267" width="400"/></a></center> Similar TutorialsHey guys i am making a php application and i have a feature where it allows members to upload images. If there a way to secure a folder to only be allowed access when a member is logged in and not someone accessing the folder and downloading images. Stuped question i know would it be better to store the images in the database as BLOB? but then again could make the database big. Thanks Hi all, we are new bee to php, I am developing a project that has config and other sub folders under admin directory ROOT/ADMIN, ROOT/ADMIN/FOLDER1,2,3... how to manage redirection from one file to another folder. Im using XAMPP at the moment and my applicatin im making is in a folder in the htdocs folder where all my files are read from. I notice when im php scripting if I put any files into another folder with in my main folder it wont load the page. so when I want to load a .php file from a script do i need to add more then just go to this .php file? I tried adding folder/*****.php but that didnt work I want to use multiple folders so its more organized hope someone can help me out with this As above, I have a lottery style site that picks a random number between 1-8 but my users complain for some reason that this is not enough. So i was told to look into using fopen and random.org to generate a random number. Anyone have experience of this and perhaps a code snippet for me to look at and possibly use? help will be appreciated. How do you count the number of folders in a directory? Hi, How do you create a folder in the main dir (public_html) when you are a few folders deep? My code... mkdir("../../../../../filestore/images/$page_id/", 0644); I seem to get an error each time failing to create the folder. Is there away to go direct to the public html folder? Thanks for any help. hi, I've tried searching google without luck so I thought I would try here. I'm trying to display the names of the last 5 modified folders within a directory i provide. Does anyone know a tutorial or site that could teach me how to do this? I have written a rule that if a file doesn't exist, then look for it in "sub1" folder, for example: when someone goes to www.example.com/file15 it will rewrite to -> www.example.com/sub1/file15, and it works
RewriteEngine On But there are more folders than sub1, so if a "file15" doesn't exist in sub1, then look for it in folder sub2, and if it's not in sub2, then rewrite to sub3. I tried this but doesn't work:
RewriteEngine On
Hi, I have coded a site, whereby users can log in, and depending on their group, view all files within their group directory: Code: [Select] if ($handle = opendir("storage/Admin/$group")) { while (false !== ($file = readdir($handle))) { if ($file != "." && $file != "..") { echo "<a target=\"_blank\" href=\"storage/Admin/$group/$file\">$file</a> <br />"; } } closedir($handle); } In this example, a user can click on their file, and it will let them download it, but if there is a folder (directory) in there, once clicked on, it will open a index of the directory. Is there a way instead, to let it open a custom page and list the files within it there? I have seen many web sites that will have there page set up with a directory structure instead of the ?x=f&a=s... for example shapeways has for each product a page like this: http://www.shapeways.com/model/194992/ They are not generating a folder and index.php for each product because you can type any file name you want in after the / and you will still get the same page. Was wondering how it is done as it makes much more human remember able addresses. Hi All - hope someone has done this before. I need to create a PHP script that detects the presence of a new file appearing into a folder, and process it. This HAS to be supported by Windows IIS (Have looked at FAM for Linux but can't use it). I could do a loop with some sleep that checks the directory every x seconds - but this has been discounted by the client as either a) too resource hungry or b) not quick enough if there is a delay. Add ons, extensions or even external 3rd party tools would all be acceptable. Any ideas? Phil Hi folks, Working on something and I'm not sure how best to do it. It's a system whereby customers can login and see pdf files. Each customer has a unique reference number which is mirrored with a folder on the server, the folder name being the customer reference number. That's the first bit of checking. Then, within that customer folder there will be between 1 and X sub folders which are labelled by unique product reference numbers. Beyond that, each of the product reference folders will have a year folder with a folder for each month of the year going forward. Here's an example of the folder structu - Customer Number (e.g. 12345678) - Product Number (e.g. 456789) (could be more than one) - Year (e.g. 2011) (could be more than one) -Month (e.g. 01/02/03/04/05/06 etc) Essentially, I'm just unsure how to check if folders exist and display their contents if they do. Here's my code so far, which does the first part of checking to see if the product numbers exist, but not the folder checking (I've not activated the hyperlinks in this code yet). This doesn't go as far as years and months yet, just the product number: Code: [Select] $query = mysql_query("SELECT * FROM customers where id = '$id'"); while($rst = mysql_fetch_array($query)) { echo "$rst[company_name]"; } $query = mysql_query("SELECT * FROM sites where company_name = '$company'"); while($rst = mysql_fetch_array($query)) { echo "$rst[site]<br>"; if ($rst[product1] !== ""){ echo "- <a href='#'>$rst[product1]</a><br>"; } if ($rst[product1] == ""){ echo ""; } if ($rst[product2] !== ""){ echo "- <a href='#'>$rst[product2]</a><br>"; } if ($rst[product2] == ""){ echo ""; } if ($rst[product3] !== ""){ echo "- <a href='#'>$rst[product3]</a><br>"; } if ($rst[product3] == ""){ echo ""; } if ($rst[product4] !== ""){ echo "- <a href='#'>$rst[product4]</a><br>"; } if ($rst[product4] == ""){ echo ""; } if ($rst[product5] !== ""){ echo "- <a href='#'>$rst[product5]</a><br>"; } if ($rst[product5] == ""){ echo ""; } so is it impossible if I have a users folder and a messaging folder located in the same folder. To have a script in the users folder reading from the messaging folder from a script in the messaging folder i thought i could just do this to read the users folder but apparently not Code: [Select] <? require("users/menu.php"); ?> i have some questions on how folders and files permissions work. say i have users directories outside 'protected' as below.. Code: [Select] users -- usera -- docs -- userb -- docs protected i do not want user B who does not have the rights, to access anything in user A directories. also, i do not want any person to access the directories directory via url links. basically i just want users to be able to access their own directories, and no one else. how can it be done? thanks! Hey guys, I'm very new to PHP so I apologize for any stupid mistakes or if I'm a little slow understanding things. I have put together a small website and I wanted to use a PHP script to display a list of files and folders (and sub folders). Basically a ghetto equivalent of an FTP page so I can apply CSS and make things look a little pretty. I have the following code I found from Google that will display all of the files and folders, not perfect but it will work for now! However I can't seem to work out the syntax to make them a hyperlink. Any help would be appreciated! <?php function getDirectory( $path = '.', $level = 0 ){ $ignore = array( 'cgi-bin', '.', '..' ); // Directories to ignore when listing output. Many hosts // will deny PHP access to the cgi-bin. $dh = @opendir( $path ); // Open the directory to the handle $dh while( false !== ( $file = readdir( $dh ) ) ){ // Loop through the directory if( !in_array( $file, $ignore ) ){ // Check that this file is not to be ignored $spaces = str_repeat( ' ', ( $level * 4 ) ); // Just to add spacing to the list, to better // show the directory tree. if( is_dir( "$path/$file" ) ){ // Its a directory, so we need to keep reading down... echo "<strong>$spaces $file</strong><br />"; getDirectory( "$path/$file", ($level+1) ); // Re-call this same function but on a new directory. // this is what makes function recursive. } else { echo "$spaces $file<br />"; // Just print out the filename } } } closedir( $dh ); // Close the directory handle } ?> I'm using the following code in a WordPress functions file to rename a folder in the uploads folder according to the date, but this only works if the chmodd settings of both folders are 777. How can I change this code so I can use at least chmodd 755 to keep my website save. Code: [Select] date_default_timezone_set('Europe/Amsterdam'); // set timezone $foldername = ''; $source = 'uploads'; $allfolders = getDirectory($source); // get folders $folder = $source.'/'.$allfolders[0]; // get first folder $month = (int)date('n'); $day = (int)date('j'); $f = 50-$day; $s = 100-$month; $newfolder = $source.'/'.(string)$f.(string)$s; if($folder != '' && $folder != $newfolder){ rename($folder, $newfolder); $foldername = $newfolder."/"; }else if($folder == $newfolder){ $foldername = $newfolder."/"; } else{ $foldername = 'error'; } I'm pretty new to php and I'm furiously trying to get this code to list the folders/files alphabetically but to no avail! I'd really appreciate it if someone could take a look and guide me as to what to do, I'm pulling what little hair i have left out! I can currently see the folder contents but the order is all over the place. Thanks very much!!! -------------------------- <?php $sub = ($_GET['dir']); if (strstr($sub, "..")) {$sub = "";} $path = '../media/Movies'; $path = $path . "$sub"; $dh = opendir($path); $i=1; while (($file = readdir($dh)) !== false) { if(substr($file,0,1) != ".") { if (substr($file, -4, -3) =="."){ echo "<font size=2 face=\"Arial, Helvetica, sans-serif\">$i. $file <br />"; }else{ echo "<font size=2 face=\"Arial, Helvetica, sans-serif\">$i. <a href='?dir=$sub/$file'>$file</a><br />"; } $i++; } } closedir($dh); ?> -------------------------- hello, I have implemented a snippet that reads images in I create folders dynamically from admin and makes them see lightbox style .. the same works fine but it shows all images in all folders and not those relating to folder clicked .. The result you can see it he aires-restauro.org/beta/gallery2.php code: <?php $folder = opendir('gallery/'); echo "<gallery>"; while ($file = readdir($folder)) { if($file != '.' && $file != '..'){ echo "<album path='gallery/$file/' name='$file'>"; $subfolder="gallery/".$file."/"; $subfolderdir = opendir($subfolder); while ($subfile = readdir($subfolderdir)) { if($subfile != '.' && $subfile != '..'){ echo "<a href='$subfolder$subfile' class='pirobox_gall'>$file</a>"; } } echo "</album>"; } } echo "</gallery>"; ?> repeats the same directory as I would like to see that by clicking the directory do see the images of that directory sorry but I do not speak English well and I thank you in advance Hi Sorry if this is the wrong place for this but I'm totally stumped here and I know its a basic question but I'm very new to php and I need help :-) Basically all i would like to do is list the files and folders in the following directory alphabetically, I know i need to put them in an array etc but I don't know how and would really appreciate some help! So any help would be very gratefully received. Here's my code so far: <?php $sub = ($_GET['dir']); if (strstr($sub, "..")) {$sub = "";} $path = '../media/Documents'; $path = $path . "$sub"; $dh = opendir($path); $i=1; while (($file = readdir($dh)) !== false) { if(substr($file,0,1) != ".") { if (substr($file, -4, -3) =="."){ echo "<font size=2 face=\"Arial, Helvetica, sans-serif\">$i. $file <br />"; }else{ echo "<font size=2 face=\"Arial, Helvetica, sans-serif\">$i. <a href='?dir=$sub/$file'>$file</a><br />"; } $i++; } } closedir($dh); ?> Many thanks in advance! Dear all PHP experts need your help please as I'm very new the PHP. I found this php form script on the net which works perfectly but only uploads one file. I need to upload a second file and send it to a different folders and a different field on the database. I have viewed a number of site and changed the code a number of time without success I have put the code back to original script, can some one please show me what I need to do. Thanks <form action="upload.php" method="post" enctype="multipart/form-data"> <label>First Name</label><input type="text" name="fname" /><br /> <label>Last Name</label><input type="text" name="lname" /><br /> <label>Upload Image</label> <input type="file" name="image" /><br> <label>Spec</label> <input type="file" name="spec" /><br /> <input type="hidden" name="MAX_FILE_SIZE" value="100000" /> <input type="submit" id="submit" value="Upload" /> </form> Script function is_valid_type($file) { $valid_types = array("image/jpg", "image/jpeg", "image/bmp", "image/gif"); if (in_array($file['type'], $valid_types)) return 1; return 0; } function showContents($array) { echo "<pre>"; print_r($array); echo "</pre>"; } $TARGET_PATH = "images/"; $fname = $_POST['fname']; $lname = $_POST['lname']; $image = $_FILES['image']; $fname = mysql_real_escape_string($fname); $lname = mysql_real_escape_string($lname); $image['name'] = mysql_real_escape_string($image['name']); $TARGET_PATH .= $image['name']; if ( $fname == "" || $lname == "" || $image['name'] == "" ) { $_SESSION['error'] = "All fields are required"; header("Location: index.php"); exit; } if (!is_valid_type($image)) { $_SESSION['error'] = "You must upload a jpeg, gif, or bmp"; header("Location: index.php"); exit; } if (file_exists($TARGET_PATH)) { $_SESSION['error'] = "A file with that name already exists"; header("Location: index.php"); exit; } if (move_uploaded_file($image['tmp_name'], $TARGET_PATH)) { $sql = "insert into people (fname, lname, filename,spec) values ('$fname', '$lname', '" . $image['name'] ."')"; $result = mysql_query($sql) or die ("Could not insert data into DB: " . mysql_error()); header("Location: images.php"); exit; } else { $_SESSION['error'] = "Could not upload file. Check read/write persmissions on the directory"; header("Location: index.php"); exit; } |