PHP - Displaying Images From A Directory Alphabeticaly In Descending Order By Filename
I am trying to display images on a html page alphabetically in descending order by their filename.
<?php $dir = "./plate_cache"; $handle = opendir($dir); while (($file = readdir($handle))!==false) { if (strpos($file, '.png',1)) { echo "<img id=\"plates\" src=\"/tags/plate_cache/$file\"></a><br />;"; } } closedir($handle); } ?> Any help is greatly appreciated! Similar Tutorialsok I got the images to upload to the directory on the server. now I got some code to try to display the image on the same page after you submit it to the directory. so it's like, submit form-> images goes in server directory->echoes back out on same page but just above the upload form. please any help greatly appreciated. thanks. here is the code so far. Code: [Select] <?php // An Example about listing Images. $dir = "uploads/"; $odir = opendir($dir); while($file = readdir($odir)){ if(filetype($file) == "image/JPEG") { echo $file; } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Untitled Document</title> </head> <body> <p>UPLOAD YOUR PORTFOLIO AND PRICES</p> <p> </p> <p> <form enctype="multipart/form-data" action="uploader.php" method="POST"> <input type="hidden" name="MAX_FILE_SIZE" value="100000" /> Choose a file to upload: <input name="uploadedfile" type="file" /><br /> <input type="submit" value="Upload File" /> </form></p> </body> </html> How can I make this output in descending order from greatest to least? Right now, it appears like: 1940 1941 1942 1943 1944 etc... i'd like it to be like 2012 2011 2010 2009 etc.. until it gets to 1940 any ideas? here is my code: <? $i = 1939; while ($i < 2012) { $i++; echo '<option value="'.$i.'">'.$i.'</option>'; } ?> i am printing the dates from the last 30 days in ascending order and then comparing them in my query to print the results. How can I can print the below array in descending order without affecting my query? Code: [Select] $thirtydaysago = time() - (30 * 24 * 60 * 60); $oneday=24 * 60 * 60; for($i=0;$i<31;$i++) { $d[$i]= $thirtydaysago + ($i*$oneday); echo date('Y-m-d',$d[$i])."<br>"; } for($i=0;$i<31;$i++) { $postsql=mysql_query("SELECT DATE_FORMAT(FROM_UNIXTIME(dateline), '%Y-%m-%d') AS FmtDate, COUNT(postid) AS PostCnt FROM post " . "WHERE dateline < '" . $d[$i+1] . "' AND dateline >= '" . $d[$i] . "' GROUP BY DATE_FORMAT(FROM_UNIXTIME(dateline), '%Y-%m-%d') DESC") or die(mysql_error()); if($postsql_rows=mysql_fetch_assoc($postsql)) { $data_date[$i]["date"]=$postsql_rows['FmtDate']; $data_postcount[$k][$i]["postcount"]=$postsql_rows['PostCnt']; } } $k++; } Hello. I have a directory with files that look like this: priceguide_20100809.pdf priceguide_20100808.pdf priceguide_20100807.pdf priceguide_20100806.pdf I am trying to come up with a function that will grab and save the filename with the biggest date .. ie: priceguide_20100809.pdf. Any assistance would be greatly appreciated!! Hey guys! I'm making a locally run web based application for entering/storing/editing data. I am trying to do this thing where I export a 'records.txt' file to a different location which can be selected by the user. I have a lame <input type='text' value='H:\'> box going on which I was going to use to define the path for the file to be saved, but I want to be able to use a pop up windows (or whatever native OS finder application) just like the one which appears when you use <input type='file'>. Now that I think about it, I think this is more of a html question, but it's 2.30am and my project is due today... See! what the hell, I just habitually pressed ctrl-s after working in dreamweaver for hours and the exact function which I am looking for popped up, how do I do that?! Please thanks guys Hi all, I am working on my PHP script to open my emails by fetching the data from mysql database. When I open my emails I want to fetch the filename data next to `alt` in the img tag, example: what-is-bootstrap.png so I can then send the filename data to `/project/u/index` to display the images. Here is what my image will show as for example: <img src="/project/u/?id=47394f41507856374130672f6d65534430734f7177513d3d&attid=0.2&msgid=1630808059112201633&view=attachment&display=view" alt="what-is-bootstrap.png" width="452" height="302" class="gmail-a6T" tabindex="0"> Here is the index.php code:
<?php //ini_set('display_errors', 1); //ini_set('display_startup_errors', 1); //error_reporting(E_ALL); require_once "Mail.php"; require_once('Mail/IMAPv2.php'); $key = "**************"; $email_number = openssl_decrypt(hex2bin($_GET['id']),'AES-128-CBC', $key); $attach_id = $_GET['attid']; $attachment_display = $_GET['display']; $attachments = array(); $body_attachments = array(); $attachment_number = 0; $inline_number = 0; $body_number = 0; if ($attachments >= 1) { if (($attach_id == 0) or ($attach_id > 0.9)) { if($attachments[$attach_id]) { $filename = $attachments[$attach_id]['name']; $filePath = "attachments/" .$filename; if($attachment_display == 'download') { header("Content-Type: application/octet-stream"); header("Content-Transfer-Encoding: Binary"); header("Content-disposition: attachment; filename=".basename($filename)); echo readfile($filePath); } else if($attachment_display == 'view') { $type = 'image/jpeg'; header('Content-Type:'.$type); header('Content-Length: ' . filesize($filePath)); readfile($filePath); } } } } if ($body_attachments >= 1) { if (($attach_id >= 0.1) && ($attach_id <= 0.9)) { if ($attach_id == '0.1') { $attach_id = 0; } else if ($attach_id == '0.2') { $attach_id = 1; } else if ($attach_id == '0.3') { $attach_id = 2; } else if ($attach_id == '0.4') { $attach_id = 3; } else if ($attach_id == '0.5') { $attach_id = 4; } else if ($attach_id == '0.6') { $attach_id = 5; } else if ($attach_id == '0.7') { $attach_id = 6; } else if ($attach_id == '0.8') { $attach_id = 7; } else if ($attach_id == '0.9') { $attach_id = 8; } else if ($attach_id == '0.10') { $attach_id = 9; } else if ($attach_id == '0.11') { $attach_id = 10; } else if ($attach_id == '0.12') { $attach_id = 11; } else if ($attach_id == '0.13') { $attach_id = 12; } else if ($attach_id == '0.14') { $attach_id = 13; } else if ($attach_id == '0.15') { $attach_id = 14; } else if ($attach_id == '0.16') { $attach_id = 15; } else if ($attach_id == '0.17') { $attach_id = 16; } else if ($attach_id == '0.18') { $attach_id = 17; } else if ($attach_id == '0.19') { $attach_id = 18; } else if ($attach_id == '0.20') { $attach_id = 19; } else if ($attach_id == '0.21') { $attach_id = 20; } else if ($attach_id == '0.22') { $attach_id = 21; } else if ($attach_id == '0.23') { $attach_id = 22; } else if ($attach_id == '0.24') { $attach_id = 23; } else if ($attach_id == '0.25') { $attach_id = 24; } else if ($attach_id == '0.26') { $attach_id = 25; } else if ($attach_id == '0.27') { $attach_id = 26; } else if ($attach_id == '0.28') { $attach_id = 27; } else if ($attach_id == '0.29') { $attach_id = 28; } else if ($attach_id == '0.30') { $attach_id = 29; } if(!$body_attachments[$attach_id]) { $attach_id++; } if($body_attachments[$attach_id]) { $filename = $body_attachments[$attach_id]['name']; $filePath = "attachments/" .$filename; if($attachment_display == 'download') { header("Content-Type: application/octet-stream"); header("Content-Transfer-Encoding: Binary"); header("Content-disposition: attachment; filename=".basename($filename)); echo readfile($filePath); } else if($attachment_display == 'view') { $type = 'image/jpeg'; header('Content-Type:'.$type); header('Content-Length: ' . filesize($filePath)); readfile($filePath); } } } } ?>
<?php // Initialize the session session_start(); //Connect to the database include('config.php'); // Check if the user is logged in, if not then redirect him to login page if(!isset($_SESSION["loggedin"]) || $_SESSION["loggedin"] !== true) { header("location: login.php"); exit; } else { $id = $_POST['email_number']; $mailfolder = $_POST['mailfolder']; $readMail = 'read'; $mailbox = $link->prepare("UPDATE " . $mailfolder . " SET readtype = ? WHERE id = ?"); $mailbox->execute([$readMail, $id]); // set the resulting array to associative $row = $mailbox->fetch(PDO::FETCH_ASSOC); if($mailbox->rowCount() == 1) { $results = array(); $mailbox_from = $row['from_email']; $mailbox_from_ = explode('<', $mailbox_from); $mailbox_from_name = $mailbox_from_[0]; $mailbox_from_email = '<' . $mailbox_from_[1]; $mailbox_from_email = htmlspecialchars($mailbox_from_email); $mailbox_to = $row['to_email']; $mailbox_to_name = $mailbox_to; $mailbox_subject = quoted_printable_decode($row['subject']); $mailbox_fulldate = $row['received_date']; $mailbox_date = convDate($mailbox_fulldate); $mailbox_message = $row['message']; $mailbox_attachment = $row['attachments']; $mailbox_folder = $_POST['folder']; $total = $_POST['total']; $email_encrypt = $row['encryption']; $send_to = $mailbox_from_email; $results['mailbox_from'] = $mailbox_from; $results['mailbox_from_'] = $mailbox_from_; $results['mailbox_from_name'] = $mailbox_from_name; $results['mailbox_from_email'] = $mailbox_from_email; $results['mailbox_to'] = htmlspecialchars($mailbox_to); $results['mailbox_subject'] = $mailbox_subject; $results['mailbox_fulldate'] = $mailbox_fulldate; $results['mailbox_date'] = $mailbox_date; $results['mailbox_message'] = $mailbox_message; $results['mailbox_attachment'] = $mailbox_attachment; $results['mailbox_folder'] = $mailbox_folder; $results['total'] = $total; $results['email_encrypt'] = $email_encrypt; $results['send_to'] = $send_to; $results['time_elapsed'] = $time_elapsed; $results['mailed_by_date'] = $mailed_by_date; $results['mailed_by'] = $mailed_by; $results['mailbox_to_name'] = $mailbox_to_name; $results['attach_test'] = $attach_test; $data = array("start"=>$start_idx,"total"=>$total,"encryption"=>$email_encrypt); $response = array("data"=>$data,"html"=>$html, "total_inbox"=>$total_inbox_unread, "total_draft"=>$total_draft_unread, "total_spam"=>$total_spam_unread, "emailBodyMessage"=>$results, "emailHeader"=>$header); echo json_encode($response); } } //Close connection $mailbox = null; ?>
I want to fetch the filename data next to `alt` so I can send the data to /project/u/index.php to display the image. Thanks in advance. If it would be just CSS I would just put " " around the url, however this is a php script. Here's the line:
<li style="background: url({img server_id=$aSlidePhoto.server_id path='photo.url_photo' file=$aSlidePhoto.destination suffix='_175' title=$aSlidePhoto.title return_url=true}); width: 175px; height: 130px;">It works for all images except, for example: myimage (1).jpg What's the fix for such case? Hi, I'm inserting hours and minutes per user into a database where they have their own fields. (userid, hours, mins) I've a small issue when displaying the data. When I run my query I sum the total hours and minutes per user which results in data such as the following userid1 - 2 hours 15 mins userid2 - 1 hour 100 mins The query orders by hours and then mins desc When I'm displaying the data (as I'm looping through the results array) I perform a calculation to convert the mins to hours so it now reads userid1 - 2 hours 15 mins userid2 - 2 hours 40 mins so the webpage displays userid1 first when i want userid2 to be the first record displayed (Hours desc) Can anyone recommend a solution to this ? Will I need to create another table and update it as hours and minutes are being entered and display results from that table instead ? Can I order the data after I carry out the mins to hours calculation ? many thanks in advance for any suggestions.... tmfl Hi, I have a list of images from a directory that are in order by their "id". How could I display one at a time on a page--starting from the beginning, and have it so the user can click on the image and go to the next one in line. Hi, i want to make an image gallery, this is the first step. uploading files. below is the code I am using , but don't i have to include info about the server? i dont know what to include since it is the server I am working with and not a database, any help greatly appreciated. thanks Code: [Select] <?php // Where the file is going to be placed $target_path = "uploads/"; /* Add the original filename to our target path. Result is "uploads/filename.extension" */ $target_path = $target_path . basename( $_FILES['uploadedfile']['name']); if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) { echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded"; } else{ echo "There was an error uploading the file, please try again!"; } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Untitled Document</title> </head> <body> </body> </html> Hi, I am trying to echo out images on my server's directory "uploads". The images are blank and not showing up. Please help if anyone can. Thank you. here is the page as it is. http://ealike.com/simple_uploader/uploader.php and here is the code. Code: [Select] <?php // Where the file is going to be placed $target_path = "uploads/"; /* Add the original filename to our target path. Result is "uploads/filename.extension" */ $target_path = $target_path . basename( $_FILES['uploadedfile']['name']); if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) { echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded"; } else{ echo "There was an error uploading the file, please try again!"; } //echo out all images $dir = "uploads/"; if (is_dir($dir)) { if ($dh = opendir($dir)) { while (($file = readdir($dh)) !== false) { echo "<img src='$file' alt='$file' height='200' width='200' /> " ; } closedir($dh); } } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Untitled Document</title> </head> <body> </body> </html> How can i display all the images in a certain directory and echo the image names. So if a new image is uploaded, it will still display without edited the gallery.php file. Cheers. So, We are trying to create a animation using the gifs in a directory by using the first 10 images. We just cannot figure out how to do the animation.. needs to have a 3 second delay between each frame Heres the code at the moment. Which creates the starting images and also sorts the images in the directory by time.. <?php $time = date("YmdHis"); $handle_data = file_get_contents('http://www.bungie.net/Stats/Halo3/Nightmap.ashx'); $img = new Imagick(); $img->readImageBlob($handle_data); $img->writeImage('nightmap/'.$time.'.gif'); foreach (glob('nightmap/*.gif') as $f) { # store the image name $list[] = $f; } sort($list);# sort is oldest to newest, $gif_0 = array_pop($list); $gif_1 = array_pop($list); $gif_2 = array_pop($list); $gif_3 = array_pop($list); $gif_4 = array_pop($list); $gif_5 = array_pop($list); $gif_6 = array_pop($list); $gif_7 = array_pop($list); $gif_8 = array_pop($list); $gif_9 = array_pop($list); ?> I am trying to display images on a html page alphabetically in descending order by their filename. This script inserts all the images in a directory on the page. Just can't figure out how to maintain this with a sorting option <?php $dir = "./plate_cache"; $handle = opendir($dir); while (($file = readdir($handle))!==false) { if (strpos($file, '.png',1)) { echo "<img id=\"plates\" src=\"/tags/plate_cache/$file\"></a><br />;"; } } closedir($handle); } ?> I have a PHP script that uploads images to a folder on my server (attachments folder). Currently the folder sits within my webroot and is publicly accessible (I have to use chmod 777 due to permissions issue). So, I created the "attachments" folder outside of my webroot (so that it is not publicly accessible), but I do not know how to set the path in the PHP code to upload it to that "attachments" folder outside of the webroot. As you see in the snippet of PHP code below, the code currently uploads the the "attachments" folder within the www (webroot) directory. How do I make it upload to the "attachments" folder OUTSIDE of the www (webroot) directory? foreach($files[$form] as $file){ $str = $file[1]; if (eval("if($str){return true;}")) { $_values[$file[0]] = $_FILES[$file[0]]["name"]; $dirs = explode("/","attachments//"); $cur_dir ="."; foreach($dirs as $dir){ $cur_dir = $cur_dir."/".$dir; if (!@opendir($cur_dir)) { mkdir($cur_dir, 0777);}} $_values[$file[0]."_real-name"] = "attachments/".date("YmdHis")."_".$_FILES[$file[0]]["name"]."_secure"; copy($_FILES[$file[0]]["tmp_name"],$_values[$file[0]."_real-name"]); @unlink($_FILES[$file[0]]["tmp_name"]); }else{ $flag=true; if ($_isdisplay) { //$ExtFltr = $file[2]; //$FileSize = $file[4]; if (!eval("if($file[2]){return true;}")){echo $file[3];} if (!eval("if($file[4]){return true;}")){echo $file[5];} $_ErrorList[] = $file[0]; } } } Thanks I currently have a file upload form that uploads images to a folder on my server called "attachments". This is the directory path: www.mysite.com/docs/attachments/ However, I want the images to upload to www.mysite.com/data/attachments/ instead. I have included the relevant part of the PHP below. I have tried changing this: $cur_dir = $cur_dir."/".$dir; to this: $cur_dir = $cur_dir."www.mysite.com/data/attachments/".$dir; and changing this: $dirs = explode("/","attachments//"); to this: $dirs = explode("www.mysite.com/data/","attachments//"); and also changing this: $_values[$file[0]."_real-name"] = "attachments/".date("YmdHis")."_".$_FILES[$file[0]]["name"]."_secure"; to this: $_values[$file[0]."_real-name"] = "www.mysite.com/data/attachments/".date("YmdHis")."_".$_FILES[$file[0]]["name"]."_secure"; But nothing has worked so far. Any suggestions would be appreciated. foreach($files[$form] as $file){ $str = $file[1]; if (eval("if($str){return true;}")) { $_values[$file[0]] = $_FILES[$file[0]]["name"]; $dirs = explode("/","attachments//"); $cur_dir ="."; foreach($dirs as $dir){ $cur_dir = $cur_dir."/".$dir; if (!@opendir($cur_dir)) { mkdir($cur_dir, 0777);}} $_values[$file[0]."_real-name"] = "attachments/".date("YmdHis")."_".$_FILES[$file[0]]["name"]."_secure"; copy($_FILES[$file[0]]["tmp_name"],$_values[$file[0]."_real-name"]); @unlink($_FILES[$file[0]]["tmp_name"]); }else{ $flag=true; if ($_isdisplay) { //$ExtFltr = $file[2]; //$FileSize = $file[4]; if (!eval("if($file[2]){return true;}")){echo $file[3];} if (!eval("if($file[4]){return true;}")){echo $file[5];} $_ErrorList[] = $file[0]; } } } I have the following code which displays images and captions from a directory. I would like the images to be displayed in the order they were created. Somebody suggested I use glob(), but I have no idea how. <?php $dir = "exclusive_images/"; if ($opendir = opendir($dir)) { //read directory while(($file = readdir($opendir)) !==FALSE) { if ( file_exists($dir.'/'.$file) && in_array( strtolower(pathinfo($file,PATHINFO_EXTENSION )), array('png','jpg','jpeg','gif'))) { if (file_exists($dir.'/'.$file. '.txt')) $caption = file_get_contents($dir.'/'.$file. '.txt'); else $caption = ucwords(str_replace(array('-','_'),' ', substr($file, 0, (strlen ($file)) - (strlen (strrchr($file,'.')))))); echo '<tr><td><img src="' . $dir.'/'.$file .'" alt="'. $file .'" title="' . $file . '" width="200"></td><td>' . $caption . '</td></tr>'; } } } ?> Hi I'm trying to setup Pagination at the bottom of the page after showing 10 images in the following script but have no clue where to start and how to implement it into the script. My images generator comprises
an array of image names extracted form an images table from a database using a select statement
a random number generator,
and a string that builds the correct pathname for the selected file.
To select one of the images for display, i generate a random number between 1 and the length of the array.
Though the generator is working, I noticed one of the random numbers is throwing up this error:
Warning: getimagesize(images/): failed to open stream: No such file or directory in
An inspection of the array reveals 2 array elements (representing my number of images) but one array element is NULL ( the first entry in the banner table
image_generator.php
require_once('connection.inc.php'); $sql = 'SELECT `filename` FROM banner'; $result = $mysqli->query($sql, MYSQLI_STORE_RESULT) or die(mysqli_error()); $row = $result->fetch_array(MYSQLI_ASSOC);//an array of image names $count = $result->num_rows; for ($i = 1; $i <= $count; ++$i) { $row[$i] = $result->fetch_array(MYSQLI_ASSOC); } $i = rand(1, $count); //a random number generator, //The random number is used in the final line to build the correct pathname for the selected file. $selectedImage = "images/{$row[$i]['filename']}"; if (file_exists($selectedImage) && is_readable($selectedImage)) { $imageSize = getimagesize($selectedImage); }var_dump($row) array (size=1) 'filename' => string 'ginsomin2.jpg' (length=13) nullRANDON IMAGE DISPLAY require_once 'image_generator.php'; <div id="banner" class="wrapper clearfix"> <img src="<?php echo $selectedImage; ?>" alt="banner"> </div>Kindly advice how i may proceed from here? Thanks. Hello I am using this script right here http://www.nearby.org.uk/sphinx/search-example5-withcomments.phps and am trying to show images from my db but I can only get it to show the first image. Does anyone know how to make it show all images if images even exist for that particular search result? I changed the query to $CONF['mysql_query'] = ' SELECT l.link_id AS id, l.title AS title, l.fulltxt AS body, l.url AS url, m.media_id AS im_id, m.title AS im_title, m.thumb_link AS im_t_link FROM search1_links AS l LEFT JOIN search1_media AS m ON (m.link_id = l.link_id) WHERE l.link_id IN ($ids) '; and added if ($row['im_id']) { echo '<img src="'.($row['im_t_link']).'" height="100px" width="100px"> '; } right here //Actully display the Results print "<ol class=\"results\" start=\"".($currentOffset+1)."\">"; foreach ($ids as $c => $id) { $row = $rows[$id]; $link = htmlentities(str_replace('$id',$row['id'],$CONF['link_format'])); print "<li><a href=\"$link\">".htmlentities($row['title'])."</a><br/>"; if ($CONF['body'] == 'excerpt' && !empty($reply[$c])) print ($reply[$c])."</li>"; else if ($row['im_id']) { echo '<img src="'.($row['im_t_link']).'" height="100px" width="100px"> '; } print htmlentities($row['body'])."</li>"; } print "</ol>"; if ($numberOfPages > 1) { print "<p class='pages'>Page $currentPage of $numberOfPages. "; printf("Result %d..%d of %d. ",($currentOffset)+1,min(($currentOffset)+$CONF['page_size'],$resultCount),$resultCount); print pagesString($currentPage,$numberOfPages)."</p>"; } Can anyone help me to be able to display all images if they exist? Thanks. |