PHP - Reading Files In A Directory
I have a directory full of pictures that I would like to display in a gallery. Originally I was going to write HTML like this...
<li> <img src=/photos/img_001.jpg"> </li>
And then simply copy and paste that for the number of photos I have, and then tweak the code to: img_002.jpg, img_003.jpg, and so on. I guess that is silly considering that I have over 500 photos to display! How could I use PHP to go to my /photos/ directory, scan all of the files in that directory, and then grab the file name so I could automate this process? Sorry, but I haven't written PHP in several years so all of this escapes me! ☹️ Similar Tutorialshey guys im tyring to read my directory (whole of the web server)...i want to be able to put the file name and root into an array...im just wanting to know whats the best function i need to do this please?...thank you i have made an delete files script which works for only one directory but not sub directory so i want to delete files of same extention from directory and subdirectory. My current code is Code: [Select] <? $dir = 'hmm/'; function scanr($dir){ $arr = glob($dir.'/*.jpg'); foreach($arr as $vv){ //check if $vv is a file if(is_file($vv)){ //if file, get the filename $vx=explode('/',$vv); $file=$vx[count($vx)-1]; // if no extension delete the file unlink($vv); // print the deletion message echo $vv." deleted!<br>";}else{ // if $vv is a dir then scan it again for files scanr($vv); }} } scanr($dir); ?> Hello, I have this script that I am trying to work with, it works and It is a basic image viewer/Gallery and it reads from a directory, I have all my images like so 1.png, 2.png, 3.png etc. They show up just wonderful and does what I want it to do. but I cannot get it to read the directory in numeric form. Here is my code that I am working with <?php require_once("data/connect.php"); // set the absolute path to the directory containing the images define ('IMGDIR', 'birthday/'); // same but for www define ('WEBIMGDIR', 'birthday/'); // set session name for slideshow "cookie" define ('SS_SESSNAMEB', 'slideshow_sessb'); // global error variable $err = ''; // start img session session_name(SS_SESSNAMEB); session_start(); // init slideshow class $ss = new slideshow($err); if (($err = $ss->init()) != '') { header('HTTP/1.1 500 Internal Server Error'); echo $err; exit(); } // get image files from directory $ss->get_images(); // set variables, done. list($curr, $caption, $first, $prev, $next, $last) = $ss->run(); /* slideshow class, can be used stand-alone */ class slideshow { private $files_arr = NULL; private $err = NULL; public function __construct(&$err) { $this->files_arr = array(); $this->err = $err; } public function init() { // run actions only if img array session var is empty // check if image directory exists if (!$this->dir_exists()) { return 'Error retrieving images, missing directory'; } return ''; } public function get_images() { // run actions only if img array session var is empty if (isset($_SESSION['imgarr'])) { $this->files_arr = $_SESSION['imgarr']; } else { if ($dh = opendir(IMGDIR)) { while (false !== ($file = readdir($dh))) { if (preg_match('/^.*\.(jpg|jpeg|gif|png)$/i', $file)) { $this->files_arr[] = $file; } } closedir($dh); } $_SESSION['imgarr'] = $this->files_arr; } } public function run() { $curr = 1; $last = count($this->files_arr); if (isset($_GET['img'])) { if (preg_match('/^[0-9]+$/', $_GET['img'])) $curr = (int) $_GET['img']; if ($curr <= 0 || $curr > $last) $curr = 1; } if ($curr <= 1) { $prev = $curr; $next = $curr + 1; } else if ($curr >= $last) { $prev = $last - 1; $next = $last; } else { $prev = $curr - 1; $next = $curr + 1; } // line below sets the caption name... $caption = str_replace('-', ' ', $this->files_arr[$curr - 1]); $caption = str_replace('_', ' ', $caption); $caption = preg_replace('/\.(jpe?g|gif|png)$/i', '', $caption); $caption = ucfirst($caption); return array($this->files_arr[$curr - 1], $caption, 1, $prev, $next, $last); } private function dir_exists() { return file_exists(IMGDIR); } } ?> <? // Image files array $image_files = array( ); // Array to store the images that were sorted $image_sorted = array( ); // Read the extension of the file name passed function readExtension( $file ) { $extension = substr( $file, strrpos( $file, '.' ) + 1 ); return $extension; } // This may already exist but I was in a coding frenzy. Obviously, this checks if it is an image or not function isImage( $file ) { $extension = readExtension( $file ); $valid_images = array( "gif", "jpg", "jpeg", "png", "gif" ); for( $i = 0; $i < sizeof( $valid_images ); $i++ ) { if( $extension == $valid_images[ $i ] ) { return true; } } } // Traverse the directory and get all the images in that folder function getImagesInFolder( $dir ) { if( $handle = opendir( $dir ) ) { while( false !== ( $file = readdir( $handle ) ) ) { if( $file != "." && $file != ".." && $file != "Thumbs.db" ) { if( !( is_dir( $dir . $file ) ) ) { $file_fp = $dir . $file; if( isImage( $file_fp ) ) array_push( $GLOBALS['image_files'], $file_fp ); } } } closedir( $handle ); } } //The brunt of the post. Explained below in more detail function sortByModDate( $array_in, &$array_out ) { for( $i = 0; $i < sizeof( $array_in ); $i++ ) { $time_stamp = date("G:i:s-d-m-y", filemtime( $array_in[ $i ] ) ); $array_out[ $array_in[ $i ] ] = $time_stamp; } arsort( $array_out, SORT_STRING ); $array_out = array_keys( $array_out ); if( $array_out[ 0 ] == "0" ) array_shift( $array_out ); unset( $array_in ); } // Get the sorted file by index number. This seems a bit redundant so you can modify as you see fit. function getSortedImageByIdx( $index_num ) { sortByModDate( $GLOBALS['image_files'], $GLOBALS['image_sorted'] ); return $GLOBALS[ '{image_sorted[ $index_num ]}' ]; } ?> <!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" /> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>A Great Escape Spalon</title> <!-- BEGIN CSS STYLES --> <link rel="stylesheet" href="css/lightbox.css" type="text/css" media="screen" /> <style type="text/css"> body{margin: 0;padding: 0;font: 100% Verdana, Arial, Helvetica, sans-serif;font-size: 14px;} div#gallery{margin: 40px auto;text-align: center;} div#gallery img{margin: 20px;} div#gallery p{color: #004694;} div#gallery div.pn{padding: 10px;margin: 0 5px;} a{color:#333;} a:hover{color:#cc0000;} a.sp{padding-right: 40px;} </style> <style type="text/css"> #main { position: relative; width: 1024px; margin: 0 auto; height:auto; vertical-align: middle; } #footer {position: absolute; bottom: 0; left: 0; width: 600px; height: 50px;} html, body { background-image:url(img/background.png); background-repeat:repeat-x; } #apDiv1 { position:absolute; background-image:url(img/sides.png); top: 1px; left: 1px; width: 1017px; } #apDiv2 { position:absolute; width:206px; height:384px; z-index:3; left: 50px; top: 127px; } #apDiv3 { position:absolute; width:216px; height:386px; z-index:4; left: 715px; top: 128px; } #apDiv4 { position:absolute; width:970px; height:115px; z-index:5; top: -1px; left: 1px; } #apDiv5 { position:absolute; width:200px; height:115px; z-index:5; left: 296px; top: 9px; } #apDiv6 { position:absolute; width:342px; height:115px; z-index:2; left: 101px; top: 129px; } #apDiv7 { position:absolute; width:452px; height:115px; z-index:6; left: 295px; top: 177px; } /* Left Menu */ #apDiv8 { position:absolute; width:0px; height:0px; z-index:7; left: 123px; top: 179px; } #apDiv9 { position:absolute; width:3px; height:2px; z-index:8; left: 123px; top: 233px; } #apDiv10 { position:absolute; width:13px; height:2px; z-index:10; left: 123px; top: 272px; } #apDiv11 { position:absolute; width:3px; height:0px; z-index:11; left: 123px; top: 313px; } #apDiv12 { position:absolute; width:1px; height:1px; z-index:12; left: 120px; top: 354px; } #apDiv13 { position:absolute; width:0px; height:0px; z-index:13; left: 120px; top: 399px; } #apDiv14 { position:absolute; width:3px; height:2px; z-index:14; left: 123px; top: 449px; } #apDiv15 { position:absolute; width:1px; height:0px; z-index:15; left: 123px; top: 492px; } #apDiv16 { position:absolute; width:1px; height:1px; z-index:16; left: 123px; top: 537px; } #apDiv17 { position:absolute; width:2px; height:2px; z-index:17; left: 123px; top: 581px; } /* Right Menu */ #apDiv18 { position:absolute; width:2px; height:4px; z-index:18; left: 774px; top: 179px; } #apDiv19 { position:absolute; width:4px; height:0px; z-index:19; left: 774px; top: 233px; } #apDiv20 { position:absolute; width:4px; height:3px; z-index:20; left: 774px; top: 315px; } #apDiv21 { position:absolute; width:49px; height:1px; z-index:21; left: 774px; top: 360px; } #apDiv22 { position:absolute; width:1px; height:1px; z-index:22; left: 774px; top: 405px; } #apDiv23 { position:absolute; width:21px; height:0px; z-index:23; left: 774px; top: 450px; } #apDiv24 { position:absolute; width:3px; height:0px; z-index:24; left: 774px; top: 496px; } #apDiv25 { position:absolute; width:5px; height:0px; z-index:25; left: 774px; top: 536px; } #apDiv26 { position:absolute; width:5px; height:0px; z-index:26; left: 774px; top: 272px; } #apDiv27 { position:absolute; width:0px; height:0px; z-index:27; left: 773px; top: 579px; } #apDiv28 { position:absolute; width:448px; height:501px; z-index:28; left: 277px; top: 137px; } #apDiv29 { position:absolute; width:200px; height:115px; z-index:1; left: 711px; top: 914px; } </style> <!-- END CSS STYLES --> <!-- BEGIN LIGHTBOX JS --> <script type="text/javascript" src="js/prototype.js"></script> <script type="text/javascript" src="js/scriptaculous.js?load=effects,builder"></script> <script type="text/javascript" src="js/lightbox.js"></script> <!-- END LIGHTBOX JS --> </head> <body> <!-- MAIN LAYOUT --> <div id=main> <div id="apDiv1"><img src="img/sides.png" width="995" height="1016"/></div> <div id="apDiv2"><img src="img/left_menu/leftmenu.png" /></div> <div id="apDiv3"><img src="img/left_menu/leftmenu.png" /></div> <div id="apDiv4"><img src="img/flower_top.png" width="1016" /></div> <div id="apDiv5"><img src="img/top_logo.png" width="446" /></div> <div id="apDiv6"><img src="img/middle_white_flower.png" width="840" /></div> <div id="apDiv7"><img src="img/back_center.png" width="453"/></div> <!-- END MAIN LAYOUT --> <!-- LEFT MENU BEGIN --> <div id="apDiv8"><img src="img/left_menu/menu.png" /></div> <div id="apDiv9"><a href="page.php?page=introduction"><img src="img/left_menu/introduction.png" border="0" /></a></div> <div id="apDiv10"><a href="page.php?page=hair_services"><img src="img/left_menu/hair_services.png" border="0" /></a></div> <div id="apDiv11"><a href="page.php?page=nail_services"><img src="img/left_menu/nail_services.png" border="0" /></a></div> <div id="apDiv12"><a href="page.php?page=body_treatments"><img src="img/left_menu/body_treatments.png" border="0"></a></div> <div id="apDiv13"><a href="page.php?page=specialties"><img src="img/left_menu/specialties.png" border="0" /></a></div> <div id="apDiv14"><a href="page.php?page=massage"><img src="img/left_menu/massage.png" border="0" /></a></div> <div id="apDiv15"><a href="page.php?page=packages"><img src="img/left_menu/packages.png" border="0" /></a></div> <div id="apDiv16"><a href="page.php?page=facials"><img src="img/left_menu/facials.png" border="0" /></a></div> <div id="apDiv17"><a href="page.php?page=extras"><img src="img/left_menu/extras.png" border="0" /></a></div> <!-- END OF LINKS --> <!-- RIGHT MENU BEGINS --> <div id="apDiv18"><img src="img/right_menu/info.png" /></div> <div id="apDiv19"><a href="page.php?page=price_list"><img src="img/right_menu/price_lists.png" border="0" /></a></div> <div id="apDiv20"><a href="page.php?page=gift_certificate"><img src="img/right_menu/gift_cert.png" border="0" /></a></div> <div id="apDiv21"><a href="page.php?page=meet_the_team"><img src="img/right_menu/meet_the_team.png" border="0" /></a></div> <div id="apDiv22"><a href="page.php?page=news_reviews"><img src="img/right_menu/news_reviews.png" border="0" /></a></div> <div id="apDiv23"><a href="page.php?page=employment"><img src="img/right_menu/employment.png" border="0" /></a></div> <div id="apDiv24"><a href="page.php?page=galleries"><img src="img/right_menu/galleries.png" border="0" /></a></div> <div id="apDiv25"><a href="page.php?page=products"><img src="img/right_menu/products.png" border="0" /></a></div> <div id="apDiv26"><a href="page.php?page=spa_price_list"><img src="img/right_menu/spa_price_lists.png" border="0" /></a></div> <div id="apDiv27"><a href="page.php?page=contact"><img src="img/right_menu/phone_number.png" border="0" /></a></div> <!-- BEGIN CENTER TEXT --> <div id="apDiv28"> <div id="gallery"> <?PHP $title = IMGDIR; $title = str_replace("/"," ",$title); echo $title; ?> <a href="<?=WEBIMGDIR;?><?=$curr;?>" rel="lightbox"><?PHP echo getSortedImageByIdx( 0 ); ?><img src="<?=WEBIMGDIR;?><?=$curr;?>" alt="" width="448" height="350"/></a> <p>Click on the Image to get a better view! or Click <a href="<?=WEBIMGDIR;?><?=$curr;?>" rel="lightbox">Here!</a></p> <div class="pn"> <a href="?img=<?=$first;?>">First</a> | <a href="?img=<?=$prev;?>" class="sp">Previous</a><a href="?img=<?=$next;?>">Next</a> | <a href="?img=<?=$last;?>">Last</a> </div> </div> <!-- END CENTER TEXT --> </div> </div> </body> </html> What I am looking for in help is to be able to get it to show the images in order Here it is working http://agreatescape.org/birthday.php? if you click the image lightbox will pop up but if you look at the image url it will go from 1.png to 58.png etc.. I have looked and looked and tried for hours and cant seem to get my head around it.. Thank you in advance I have a very simple piece of code to create a grid based gallery. The thumbnails are loaded from a single directory and are name 1.jpg... 2.jpg... 3.jpg etc At the moment the images appear to be loaded in randomly. I want them to load in numerically in terms of their filename. I know I may need to use 'sort' or 'natsort' but where in the code? Thanks for any help you can give me Code: [Select] <? $images = "Images/Bag Thumbs/"; # Location of small versions $big = "ruxxwomens.php?id="; # Location of big versions (assumed to be a subdir of above) $cols = 4; # Number of columns to display if ($handle = opendir($images)) { while (false !== ($file = readdir($handle))) { if ($file != "." && $file != ".." && $file != rtrim($big,"/")) { $files[] = $file; } } closedir($handle); } $colCtr = 0; echo '<table width="800" cellspacing="0" cellpadding="0" border="0"><tr>'; foreach($files as $file) { if($colCtr %$cols == 0) echo '</tr><tr>'; echo '<td align="center"><a href="' . $big . $file . '"><img src="' . $images . $file . '" cellspacing="0" cellpadding="0" border="0"></a></td>'; $colCtr++; } echo '</table>' . "\r\n"; ?> Hi guys, im logging activity on my site to text files using; Code: [Select] date_default_timezone_set('GMT'); $LogFileLocation = "xxxxxxxx.log"; $fh = fopen($_SERVER['DOCUMENT_ROOT'].$LogFileLocation,'at'); fwrite($fh,date('d.M.Y H:i:s')."\t".$_SERVER['REMOTE_ADDR']."\t".$_SERVER['REQUEST_URI']."\n"); fclose($fh); This is write something like; [date] [time] [ip address] [folder accessed] I then post it like; Code: [Select] <? $fn = "xxxxxxxxxx.log"; print htmlspecialchars(implode("",file($fn))); ?> That works fine but what i want to be able to do is read/edit the text files, so the main things i need to add is; The ability to clear the log (using password for confirmation) not sure how to go about this because i don't know how to edit the text file <input name="password" type="text"> <input type="submit" value="Clear Log?"> I also need a script to read the log and alert me an ip address that isn't mine is found, maybe something like if (an ip that isn't mine == yes) echo "an unknown ip accessed something" Also lets say i have a script that checks if a name exists, how can i use a text file containing a list of names to be read by a script if (name exists in text file) echo "name exists" else echo "name doesn't exist" All these questions are similar in the fact i need to understand how to open and read text files for specific entries, hope you understand, thanks for any help. trying to create a simpleprogram that will read a text file and output information and calculations using the data in the text file. I have 4 radio buttons which represent an item Number. when one is selected the output form should print to a table the ID,Part,Count, Price, and the inventory Value= ($count * $price), can anyone tell me what i am doing wrong? This is what the .txt file looks like: AC1000:Hammers:122:12.50 AC1001:Wrenches:5:5.00 AC1002:Handsaws:10:10.00 AC1003:Screwdrivers:222:3.00 Here's what i have so far: Code: [Select] <?php $inf = 'infile.txt'; $FILEH = fopen($inf, 'r') or die ("Cannot open $inf"); $inline = fgets($FILEH,4096); $found = 0; //$ptno = //if (isset($_POST['AC1000']) || isset($_POST['AC1000']) || isset($_POST['AC1000']) || isset($_POST['AC1000'])) { while (!feof($FILEH) && !($found)){ list($ptno,$ptname,$num,$price) = split (':', $inline); if ($ptno == $id) { print '<table border=1>'; print '<th> ID <th> Part <th> Count <th> Price'; print "<tr><td> $ptno </td><td>$ptname</td>"; print "<td> $num </td><td> \$price</td><tr>"; print '</table>'; $found = 1; } $inline = fgets($FILEH,4096); } if ($found !=1) { print "Error: PartNo=$id not found"; } fclose ($FILEH); ?> Is it possible to read from a file that is located on the user's local machine and not on the server? I have a CSV file that is about 70MB that I have to parse through and insert the data into a database. I have no problem doing this if I hard code the path to the file. Is there a way to get the path of the file that the user wants to be parsed into the database? I don't think uploading the file would be a good idea because, like I said, it is 70MB and is nearly 2 million lines. Simply parsing the file takes long enough without throwing in upload time.. I almost have it but can't figure why the code below only shows one file per folder. I think it is showing the last file. I need $ul somehow looping to show all the files in $file. <?php $year = date('Y'); if ($handle = opendir("results/$year/Boys")) { while (false !== ($file = readdir($handle))) { if ($file != "." && $file != "..") { if ($handle2 = opendir("results/$year/Boys/$file")) { while (false !== ($file2 = readdir($handle2))) { if ($file2 != "." && $file2 != "..") { $ul = "<li><a href=\"#\">$file2</a></li>"; $li = "<li><a target=\"_blank\" href=\"javascript:void(0)\">$file</a> <ul> $ul </ul> </li>"; } // close if file2 }echo $li; } } } }closedir($handle);closedir($handle2); ?> Hi guys, I am using a MySQL table to store if a user has access permisisons to a file. The files are stored outside the webdirectory in drive F:\ So I have this code I have been playing around with in order to display the file I access and check permissions for: <?php $file = $_GET['file']; $myfile = 'F:\files\/'.$file.'.jpg'; echo "<img src='$myfile' />"; ?> So a sample case would be say on index.php <img src="http://somedomain.com/files?file=abcdefghijkl" /> and only if the current user had permissions to view that file would they see it. Any help on how I can do this is greatly appreciated. I do understand all my above code is probably totally wrong. Cheers Hello everyone. I am having trouble trying to write some script that will unlink all files in a directory. Can anyone help me? I am not sure of the best way to do this? Thankyou. I have a function that reads all the picture files in a folder and displays them as thumbnails. It also displays a large version of the first image in the folder and clicking the thumbnail changes the large picture. I'm trying to use pictures of arrows as a button to allow users to click and have the next picture displayed large rather than having to click each individual thumbnail but don't really know how to get started. Can anyone suggest a method or give me a hint? This is my code so far if that helps: function displayPhotos(){ global $columns,$dir,$album; generateThumbnails(); $act = 0; $keyid = 0; $thumb_selected = (isset($_GET['thumb']) ? $_GET['thumb']:''); if ($thumb_selected !==""){ $dirName = substr($thumb_selected,0,strpos($thumb_selected,basename($thumb_selected))); $thumbName = basename($thumb_selected); $thumbFile = $dirName.$thumbName; $large = str_replace('_th.jpg','.jpg',$thumbFile); } else{ $picture_array = glob("$dir*"); $large = $picture_array[$keyid]; } echo "<tr><td colspan='3' height='300' width='400' align='center'><img src='$large' alt=''></td></tr> <tr><td><img src='gallery/icons/arrow-blue-rounded-left.jpg' alt='Previous'></td><td></td><td align='right'><img src='gallery/icons/arrow-blue-rounded-right.jpg' alt='Next'> </td></tr><tr><td height='50'></td></tr>"; if ($handle = opendir("$dir")) { while (false !== ($file = readdir($handle))) { $file=$dir.$file; if (is_file($file)){ if (strpos($file,'_th.jpg')){ ++$act; if ($act > $columns) { echo "</tr><tr><td width='160' height='120' align='center'><a href='?album=$album&thumb=$file'><img src='".$file."' alt='".$file."'/></a></td>"; $act = 1; } else { echo "<td width='160' height='120' align='center'><a href='?album=$album&thumb=$file'><img src='".$file."' alt='".$file."'/></a></td>"; } } } } } } Thanks very much to anyone having a look at this! Hey guys, I've written a script that is suppose to Display the files within a specified directory /documents/ with check boxes to the right of them.. This worked out great. Then I wanted to devise a method to remove or unlink the selected files (via check box) when the user pressed a Submit button.. File Permissions on the server are 777 and I have hardcoded an unlink to test to see if it works and it does.. So the bug lies within my code.. Before I post the code let me explain my method in doing this. I created a for loop to name each check box after the corresponding file to the left and created a variable with the file name (to use later) Then on button press I made a for loop (it goes up to 50 because I wasn't sure how to make it the exact number of files and I didn't want to overload the server with say 1000000 :?) that goes through and checks the check box's values and if they equal 1, unlink the file in /documents/ Here's the code: Code: [Select] <html> <body> <form action="" method="post" class="delete"> <?php $dir = "documents/"; if ($handle = opendir($dir)) { echo '<h2>List of Files in '.$dir.'</h2>'; echo '<table>'; for ($i = -2; false !== ($file = readdir($handle)); $i++) { if ($file != "." && $file != "..") { echo '<tr><td><a href="'.$dir.''.$file.'">'.$file.'</a></td><td><input type="checkbox" name="Del['.$i.']" /></td</tr>'; $DelFile[$i] = $file; } } echo '</table>'; closedir($handle); } ?> <br /> <input type="submit" name="submit" value="Delete Checked Files" /> </form> <?php for ($i = 0; $i <= 50; $i++) if ('Del['.$i.']' == 1) unlink("'.$dir.''.$DelFile[$i].'"); ?> </body> </html> Help is extremely appreciated, and Thanks in advance guys! I am trying to find a script that will show just the file names of files in a specific directory. I tried this code posted previously on this forum and it works but shows the directory name. Code: [Select] <?php filesInDir('attachments'); function filesInDir($tdir) { $dirs = scandir($tdir); foreach($dirs as $file) { if (($file == '.')||($file == '..')) { } elseif (is_dir($tdir.'/'.$file)) { filesInDir($tdir.'/'.$file); } else { echo $tdir.'/'.$file."<br>"; } } } ?> Result is: attachments/Sedona_and_Slot_Canyon_links.docx Thanks for the help. I am using the following code to list mp3 files from a directory and find out if there is a file for it in mysql if so not to include it in the list. The code below is listing each file twice I only would like them to be listed once. if($_POST['submit'] != 'select') { echo'Choose an mp3 to add information.<p><select name="file">'; $dir = "/uploads/"; $result = mysql_query("SELECT * FROM dj_pool") or die(mysql_error()); $num_rows = mysql_num_rows($result); // Open a known directory, and proceed to read its contents if (is_dir($dir)) { if ($dh = opendir($dir)) { while (($file = readdir($dh)) !== false) { $info = explode(".", $file); if($info[1] == "mp3") { $i = 0; while ($i < $num_rows) { $file2 = mysql_result($result,$i,mp3); if ($file != $file2) { echo "<option value='$info[0]'>$info[0]</option>"; } $i++; } } }closedir($dh);} } print "</select><p><input type=submit value=select name=submit>"; Dear sirs, I need to upload files to FTP and To waste less time, I zip the files, but the problem that since it is Automation it zips with directory - like this C:/DIR.zip in zip file I have a DIR/files.... How to unzip all files to main directory on the server. Right now if the main dir is /main/html/ it unzips to /main/html/dir/files But I need /main/html/files Thank you. Good day.
I have just written my first php phpexcell file importing into mysql without errors. now my next step is to try and read multiple excell files in a directory " files are same format" and import into mysql.
Heres my code so far.
<?php $db_host = "localhost";$db_user = "root";$db_pass = "";$db_name ="kk";$db_table = "test"; require '/Classes/PHPExcel.php'; require_once '/Classes/PHPExcel/IOFactory.php'; $objPHPExcel = PHPExcel_IOFactory::load('Amersfoort_WR.xlsx'); foreach ($objPHPExcel->getWorksheetIterator() as $worksheet) { $worksheetTitle = $worksheet->getTitle(); $highestRow = $worksheet->getHighestRow(); // e.g. 10 $highestColumn = $worksheet->getHighestColumn(); // e.g 'F' $highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn); $nrColumns = ord($highestColumn) - 64; echo "File ".$worksheetTitle." has "; echo $nrColumns . ' columns'; echo ' y ' . $highestRow . ' rows.'; echo "$cellValue"; $link = mysql_connect($db_host,$db_user,$db_pass); if(!$link) die ('Could not connect to database: '.mysql_error()); mysql_select_db($db_name,$link); for ($row = 2; $row <= $highestRow; ++ $row) { $val=array(); for ($col = 0; $col < $highestColumnIndex; ++ $col) { $cell = $worksheet->getCellByColumnAndRow($col, $row); $val[] = $cell->getValue(); } $sql = "insert into test (F1, F2, F3, F4) values('".$val['0']."', '".$val['1']."', '".$val['2']."','".$val['3']."')"; mysql_query($sql); } } mysql_close($link); ?> Hello, Its my first day coding so please forgive me for some of the silly errors I might make.
I am writing a small upload script and I want to have some sort of orginazation by uploading images to a direcotry based on the day. The following is my code but it will fail to save the file Warning: file_put_contents(images/14-09-26/5425905a18bba.png): failed to open stream: No such file or directory This is becuase the directory with the data part is not created how should I go about first checking that this directory is there and then creating it ? <?php ini_set('display_errors',1); error_reporting(E_ALL); $today = date('y-m-j'); define('UPLOAD_DIR', 'images/' .$today. '/'); $img = $_POST['data']; $img = str_replace('data:image/png;base64,', '', $img); $img = str_replace(' ', '+', $img); $data = base64_decode($img); $file = UPLOAD_DIR . uniqid() . '.png'; $success = file_put_contents($file, $data); print $success ? $file : 'Unable to save the file.'; ?> Hi, how do I output the total files in a given directory? Here is code I cannot get to work: Code: [Select] <?php $file=fopen('C:\\dir\\folder\\proj_ath\\', 'a+'); for($i=0;$i<count($file);$i++) { print "tot files so far: $i <br />"; } ?> Any help much appreciated! Hi All I am trying to set up a system that where a user sets up an account the system automatically sets up a new directory and inserts template files into it. I understand mkdir is something to do with it. Does anyone know how to go about this please? thanks How do you include files from a higher up directory? I'm currently working on a file in public_html/Directory/otherdirectory and want to include a config file that's in public_html/Directory so how would I include public_html/Directory/config.php in the public_html_Directory/otherdirectory/index.php file? I've tried using ../ and ../Directory/ in the includes line but got errors both times Edited April 11, 2020 by Nematode128 |