PHP - How To Zip A Directory In Php?
Dear friends, I know Zip a file in PHP but I can't Zip a directory.How to Zip a directory in PHP?Can you give an example?
Similar TutorialsHi guys, I've been working on a script for a while now, and I'm sure it doesn't look great and all, and it's probably really messed up.. But right now I've finally got it working! There's only 1 thing I'd really like to add.. Searching through & listing of remote directories! The directories I'm trying to list have directory listings enabled, and I think it *should* be possible. I just have no clue how. Here's my current code in a beautiful mix of HTML and PHP: <? $border_size = "0"; function returner($what) { $what=explode("/",$what); $tps=count($what); $what=$what[$tps-1]; return $what; } $page_url= ""; $home_url=returner(__FILE__); if(isset($_GET['q'])) { $qtext=$_GET['q']; } else { $qtext=""; } function getdirsize($directory, $format=FALSE) { $size = 0; if(substr($directory,-1) == '/') { $directory = substr($directory,0,-1); } if(!file_exists($directory) || !is_dir($directory) || !is_readable($directory)) { return -1; } if($handle = opendir($directory)) { while(($file = readdir($handle)) !== false) { $path = $directory.'/'.$file; if($file != '.' && $file != '..') { if(is_file($path)) { $size += filesize($path); } elseif(is_dir($path)) { $handlesize = getdirsize($path); if($handlesize >= 0) { $size += $handlesize; } else { return -1; } } } } closedir($handle); } if($format == TRUE) { if($size / 1048576 > 1) { return round($size / 1048576, 1).' MB'; } elseif($size / 1024 > 1) { return round($size / 1024, 1).' KB'; } else { return round($size, 1).' bytes'; } } else { return $size; } } if(isset($_GET['type'])){ $type=$_GET['type']; } else { $type="new"; } $textures=0; $models=0; $avatars=0; $seqs=0; $sounds=0; foreach (glob("textures/*.jpg") as $texture){ $textures++; } foreach (glob("models/*.zip") as $model){ $models++; } foreach (glob("avatars/*.zip") as $avatar){ $avatars++; } foreach (glob("seqs/*.zip") as $seq){ $seqs++; } foreach (glob("sounds/*.zip") as $sound){ $sounds++; } ?> <!DOCTYPE html> <html> <head> <title>ObjectPath Search</title> <style type="text/css"> #wrapper { width: 850px; margin: 30px auto 30px auto; padding: 10px; } body { color:#C6C6C6; background:#1E1E1E; /* margin:0; padding:0; */ overflow-x:hidden; } #tabs { font: 85% "Trebuchet MS", sans-serif; } .left { float: left; } .right { float: right; } a:link, a:visited, a:active { color: #3DB015; text-decoration: none; } a:hover { color: #00E0FF; } h2 { color: #3DB015; padding-bottom: 0.2em; font-size: 110%; } ul#icon {margin: 0; padding: 0;} ul#icon li {margin: 1px; position: relative; padding: 1px 0; cursor: pointer; float: left; list-style: none;} ul#icon span.ui-icon {float: left; margin: 0 1px;} </style> <link type="text/css" href="http://objects.jk-hosting.com/search/css/black-tie/jquery-ui-1.8.2.custom.css" rel="stylesheet" /> <script type="text/javascript" src="http://objects.jk-hosting.com/search/js/jquery-1.4.2.min.js"></script> <script type="text/javascript" src="http://objects.jk-hosting.com/search/js/jquery-ui-1.8.2.custom.min.js"></script> <script type="text/javascript"> function formHandler(form){ var URL = document.form.site.options[document.form.site.selectedIndex].value; window.location.href = URL; }; $(function(){ // Tabs $('#tabs').tabs(); }); </script> </head> <body> <div id="wrapper"> <div id="tabs"> <!-- Tabs start --> <ul> <li><a href="#tab-search">Search</a></li> <li><a href="#tab-list">List Objects</a></li> <li><a href="#tab-info">OP info</a></li> </ul> <div id="tab-search"><!-- Searchtab start --> Please enter a string to search for, and choose a folder to search in. <br /><br /> <form name="Search"> <input type='hidden' value='search' name='type'> <input value='<? print $qtext; ?>' type='text' name='q'> <select name='map'> <option selected='selected' value='models'>Models</option> <option value='avatars'>Avatars</option> <option value='textures'>Textures</option> <option value='seqs'>Seqs</option> <option value='sounds'>Sounds</option></select> <input type='submit' value='Search'> </form> </div> <!-- Searchtab end --> <div id="tab-list"><!-- Listtab start --> Please pick a folder to browse. <br /><br /> <form name="form"> <select name="site" onChange="javascript:formHandler()"> <option value="#">Look in folder...</option> <option value="<? print $page_url; ?>?type=list&map=models">Models</option> <option value="<? print $page_url; ?>?type=list&map=avatars">Avatars</option> <option value="<? print $page_url; ?>?type=list&map=textures">Textures</option> <option value="<? print $page_url; ?>?type=list&map=seqs">Seqs</option> <option value="<? print $page_url; ?>?type=list&map=sounds">Sounds</option> </select> </form> </div> <!-- Listtab end --> <div id="tab-info"><!-- Info tab start --> The OP currently contains: <br /><br /> <table> <tr><td><b><? echo $models; ?></b></td> <td>Models</td></tr> <tr><td><b><? echo $avatars; ?></b></td> <td>Avatars</td></tr> <tr><td><b><? echo $textures; ?></b></td> <td>Textures</td></tr> <tr><td><b><? echo $seqs; ?></b></td> <td>Seqs</td></tr> <tr><td><b><? echo $sounds; ?></b></td> <td>Sounds</td></tr> </table> </div> <!-- Info tab end --> </div> <!-- Tabs end --> </div> <!-- Start PHP generated content --> <? if($type=="search" || $type=="list") { $M=$_GET['map']; if($type=="search") { $Q=$_GET['q']; $empty="Nothing found with <b>\"" . $Q . "\"</b> in it's name.<br />\nPlease make a more general search query, or try a different folder.\n\n"; } else { $Q=""; $empty='This folder is empty'; } if($M=="textures") { $ext="jpg"; } else { $ext="zip"; } $i=0; print "<hr>\n"; $endfile=array(); $endsize=array(); $endsize2=array(); foreach (glob($M."/*".$Q."*.".$ext) as $filename) { $filename = explode(".", $filename); $filename=$filename[0]; $filename = explode("/", $filename); $filename=$filename[1]; $i++; $endfile[$i]=$filename; if($ext=="jpg") { $endfile[$i]="<a name='".$endfile[$i]."' href='".$pageurl."?type=view&name=".$endfile[$i]."&folder=".$M."&from=".$type."&addon=".$Q."'>".$endfile[$i]."</a>"; } $endsize[$i]=$size; $endsize2[$i]=$size2; } if($i != 1) { print "<b>".$i."</b> items were found.\n<hr>\n"; } else { print "<b>".$i."</b> item was found.\n<hr>\n"; } echo("<table width='100%' border='" . $border_size . "' cellspacing='0' cellpadding='0' >\n"); if($i!=0) { for ($t = 1; $t < $i; $t++) { $thumbfile = $M."/".$endfile[$t].'.jpg'; if(file_exists($thumbfile)) { $thumbnail = "<a name='".$endfile[$t]."' href='".$page_url."?type=view&name=".$endfile[$t]."&folder=".$M."&from=".$type."&addon=".$Q."'><ul id='icon'><li class='ui-state-default ui-corner-all' title='".$endfile[$t]."'><span class='ui-icon ui-icon-image'></span></li></ul></a>"; } else { $thumbnail = ""; } if($t=="1") { echo("<tr><td width='10%'>Number</td><td width='3%'><ul id='icon'><li class='ui-state-default ui-corner-all' title='".$endfile[$t]."'><span class='ui-icon ui-icon-image'></span></li></ul></td><td width='60%'>Name</td></tr>\n"); } echo("<tr><td>" . $t . "</td><td>".$thumbnail."</td><td>" . $endfile[$t] . "</td></tr>\n"); flush(); } $thumbfile = $M."/".$endfile[$t].'.jpg'; if(file_exists($thumbfile)) { $thumbnail = "<a name='".$endfile[$t]."' href='".$page_url."?type=view&name=".$endfile[$t]."&folder=".$M."&from=".$type."&addon=".$Q."'><ul id='icon'><li class='ui-state-default ui-corner-all' title='".$endfile[$t]."'><span class='ui-icon ui-icon-image'></span></li></ul></a>"; } else { $thumbnail = ""; } echo("<tr><td>" . $t . "</td><td>".$thumbnail."</td><td>" . $endfile[$t] . "</td></tr>\n"); } print "</table>\n"; if($i=="0") { print $empty; } } elseif($type=="view") { $filename=$_GET['name']; $folder=$_GET['folder']; if($_GET['from']=="list"){ $addon="?type=list&map=".$folder."#".$filename; } if($_GET['from']=="search"){ $addon="?type=search&q=".$filename."&map=".$folder."#".$filename; } print"<center><a href='".$home_url."'>Home</a></center>"; print "<hr>\n<center><img src='".$folder."/".$filename.".jpg'></img></center>\n<hr>\n<br />\n<a href='".$page_url."".$addon."'>Previous Page</a>\n"; } $htmlshow=""; if($_GET['type']=="returnOPfile") { if(isset($_GET['split'])) { $splitter=$_GET['split']; } else { $splitter=" | "; } if(isset($_GET['html'])) { $htmlshow="<br />"; } foreach (glob("textures/*.jpg") as $texture){ if(isset($_GET['size'])) { $size=$splitter.filesize($texture); } $texture = explode("/", $texture); $texture=$texture[1]; print "textures".$splitter.$texture.$size."\n".$htmlshow; } foreach (glob("models/*.zip") as $model){ if(isset($_GET['size'])) { $size=$splitter.filesize($model); } $model = explode("/", $model); $model=$model[1]; print "models".$splitter.$model.$size."\n".$htmlshow; } foreach (glob("avatars/*.zip") as $avatar){ if(isset($_GET['size'])) { $size=$splitter.filesize($avatar); } $avatar = explode("/", $avatar); $avatar=$avatar[1]; print "avatars".$splitter.$avatar.$size."\n".$htmlshow; } foreach (glob("seqs/*.zip") as $seq){ if(isset($_GET['size'])) { $size=$splitter.filesize($seq); } $seq = explode("/", $seq); $seq=$seq[1]; print "seqs".$splitter.$seq.$size."\n".$htmlshow; } foreach (glob("sounds/*.zip") as $sound){ if(isset($_GET['size'])) { $size=$splitter.filesize($sound); } $sound = explode("/", $sound); $sound=$sound[1]; print "sounds".$splitter.$sound.$size."\n".$htmlshow; } } ?> <!-- End PHP generated content --> </body> </html> So right now my question to you PHP freaks is, can you please help me edit my script so I can search through a remote directory? *This* is one of the directories I wish to be able to search through & list.. Thanks in advance. Edit; It might help if you know what the site currently looks like. *Click* 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); ?> I'm trying to echo the directory and sub directory only. I am not looking to show the files contained - only folders. This script reads a directory, and prints out the name of all directories in the directory that I've chosen This works, but it's only displaying the last 2. Right now, I have 6 directories in the the folder I specified. Can anybody tell me why it does not display all of them? Code: [Select] <?php $path = $_SERVER['DOCUMENT_ROOT']."/panel/api/connectors/sample/projects/"; foreach(glob($path . '*', GLOB_ONLYDIR) as $dir) { $dir = basename($dir); echo $dir."<br />"; } ?> Thanks in advance Hello, can someone please help me make this. I want a simple php directory script that lists files and folders. If a folder is clicked on then open that directory and if a file is clicked on it will download or if its a .txt or picture it will open in the browser. BUT I NEED IT TO LOOK EXACTLY LIKE THIS: http://test512.x10.mx/ which would be the default cpanel indexing but i use godaddy and i dont like there indexing page. Please help and whoever make it i will provide a subdomain on my website for you to host your stuff. I am using php's zip class, and I am able to zip files up individually, but can I zip an entire directory with its file and sub-folders? Hey so i need help searching for contacts in my telephone directory...how do i do that in my php? is it a form method or if statements? i need help gettig the size of a directory Why does this code: Code: [Select] require_once "../../media/php/classes/Mysql.inc.php"; Work when I go to: http://mysite.com/API/v1/index.php But when I go to: http://mysite.com/index.php it doesn't work. The require is being called from another class, which I call from the two pages above. The class loads, but in the second link, I get failed to open stream: No such file or directory but the first one works fine. What would cause this? could this be an htaccess thing? htaccess: Code: [Select] Options +FollowSymlinks RewriteEngine On RewriteBase / RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME}.php -f RewriteRule ^(.+)$ /$1.php [L,QSA] 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 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); ?> Not used unlink function before so a bit unsure. I do need it to remove a file in a different directory than the one the unlink function is in - it's actually in a parent sub directory. Is there another way of doing this if unlink is unsuitable or can someone help me correct my code? Code: [Select] elseif ( $action == "dlt" && $step == "2" ) { mysql_query('DELETE FROM items WHERE auto_increment = "'.$auto_increment.'"') or die( mysql_error() ); $small = "../admin/imgs/small/90_'.$auto_increment.'.jpg"; unlink( $small ); $large = "../admin/imgs/small/250_'.$auto_increment.'.jpg"; unlink( $large ); } Cheers I am trying to figure out a way to echo the directory name of the script location. i am using mod_rewrite for seo friendly urls. Take this page for example Code: [Select] http://www.thevault.cz.cc/new_post/thread/forum_and_site_news/welcome_to_asf that page calls a js file located in: Code: [Select] root/modules/parser.php the problem i am having is calling that file using javascript. when i use relative paths ./modules/parser.php it doesnt work. but even using absolute paths doesnt work. it adds the full url onto the existing one so it becomes: Code: [Select] http://www.thevault.cz.cc/new_post/thread/forum_and_site_news/welcome_to_asf/http://www.thevault.cz.cc/modules/parser.php is there a way to do this properly? to get the absolute path in the js file i am using: Code: [Select] document.location.protocol + "//" + document.root.hostname + "/modules/parser.php" Hello, I'm trying to make a little piece of code that will search for all files (including subdirectories) ending with .txt and set them to 777 foreach (glob("*.txt") as $file) { system("chmod 777 '$file'"); } Am I on the right path? thanks directory listing: hi guys, im using a PHP script to output a list of documents in different directory's using a single index.php (apart from the style obv), its not a major issue but bugging me as to how to get it to ignore the "up" link for directory navigation. its outputting the below: . .. this is annoying me... is it possible to get it to ignore these and not output them? Code: [Select] <link rel="stylesheet" type="text/css" href="style.css"> <span class="note"> <?php echo "directoryname3"; ?></span> <?php $dir="directoryname3"; if ($dir_list = opendir($dir)) { while(($filename = readdir($dir_list)) !== false) { ?> <p><a href="<?php echo $filename; ?>"><?php echo $filename; ?></a></p> <?php } closedir($dir_list); } ?> <br><br> <span class="note"> <?php echo "<b>directoryname3</B>"; ?></span> <?php $dir="directoryname3"; if ($dir_list = opendir($dir)) { while(($filename = readdir($dir_list)) !== false) { ?> <p><a href="<?php echo $filename; ?>"><?php echo $filename; ?></a></p> <?php } closedir($dir_list); } ?> <br><br> <span class="note"> <?php echo "<b>directoryname3</B>"; ?></span> <?php $dir="directoryname3"; if ($dir_list = opendir($dir)) { while(($filename = readdir($dir_list)) !== false) { ?> <p><a href="<?php echo $filename; ?>"><?php echo $filename; ?></a></p> <?php } closedir($dir_list); } ?> <br><br> <span class="note"> <?php echo "<b>directoryname</B>"; ?></span> <?php $dir="directoryname"; if ($dir_list = opendir($dir)) { while(($filename = readdir($dir_list)) !== false) { ?> <p><a href="<?php echo $filename; ?>"><?php echo $filename; ?></a></p> <?php } closedir($dir_list); } ?> I am trying to create random no. of directories but its always giving this error Warning: mkdir() [function.mkdir]: Invalid argument in C:\xampp\htdocs\gl\movie_process.php on line 173 the code is for(i=0; i<5; i++) { mkdir("".$base."uploads/movies/$year/$month/$title/".$hosting[$i].""); } Hello, I'm trying to walk a directory into a single array from a specific starting point. For example, lets say that I wanted to start at the specific directory "/content/users/n-bomb/" it would then begin to parse from that directory and list all sub-directories and directories and store them into an array. I'm not sure what would be the best array structure, but since I know the directory where I'm starting from (members base directory) I just want the array to be from their home directory. Ideally, I'd want a full-path to each directory as well, but I'm not sure how to incorporate that into the array structure. Example array of "/content/users/n-bomb/": [n-bomb] => Array ( [0] => /content/users/n-bomb/form.vb [1] => /content/users/n-bomb/song.mp3 [2] => /content/users/n-bomb/movie.avi [images] => Array ( [0] => /content/users/n-bomb/images/funny.png [1] => /content/users/n-bomb/images/mad.png [2] => /content/users/n-bomb/images/sad.png ) [documents] => Array ( [porn links] => Array ( [0] => /content/users/n-bomb/porn links/good websites.txt [1] => /content/users/n-bomb/porn links/bad websites.txt ) [0] => /content/users/n-bomb/documents/bills.txt [1] => /content/users/n-bomb/documents/website.txt [2] => /content/users/n-bomb/documents/emails.txt ) ) I started to mock up some code, but I can't quite get it right: private function walk($directory, $start = False) { if(!is_dir($directory)) return false; if(!$start) $this->information["structure"][basename($directory)] = array(); $scan = scandir($directory); natcasesort($scan); foreach($scan as $item) { if($item == "." || $item == "..") continue; $item = $directory . "/" . $item; if(is_dir($item)) $this->walk($item); if(is_file($item)) ($start) ? $this->information["structure"][] = $item : $this->information["structure"][basename($directory)][] = $item; } } With the above code I'm getting results like this: Array ( [documents] => Array ( [0] => /content/users/n-bomb/documents/bills.txt [1] => /content/users/n-bomb/documents/website.txt [2] => /content/users/n-bomb/documents/emails.txt ) [porn links] => Array ( [0] => /content/users/n-bomb/porn links/good websites.txt [1] => /content/users/n-bomb/porn links/bad websites.txt ) ) As you can see that "porn links" is not going inside of "documents". How can I resolve this issue so my code will output like the array structure that I want? Hi Guys!! I am in need of creating a categories' listing similar to that of Google Directory, http://www.google.com/dirhp All the categories along with their respective sub categories, subcategories of the subcategories, and the subsub categories of the subsub categories ( as deep down as the category system goes ) should be displayed which is exactly similar to that of the Google Directory. Could anyone suggest me how to do it as am pretty much new to PHP. The categories list is stored in a database and should be displayed using PHP and MySQL. This is exactly what i require. I have gone through some articles related to this, (both in this forum and elsewhere) but in all those tutorials, the depth of the categories is known (say, up to 4 or 5 levels). But, what i require is dynamic creation of these categories whose depth is unknown and also provide a way that any user can add a category from the front-end and also the admin can add a category from the back-end. Am in urgent need of this. Can anyone guide me in this regard, pls? And an example of the same would be of great help. Thanks in advance!! Ok so I don't even know where to start with this idea so I thought I would poke around here for some inspiration. On my server I have a Directory. In that directory I have roughly 300 folders with 5,000 files + 1 index.php (so 5,001 files but I want to ignore the index.php in the listings) per directory. Anyway what I am trying to do is list all the files and load them into a database. What my ideal goal would be is device a script that I would put in the root directory with the 300 or so subdirectories and let it run through the directories automatically one by one, listing all the files in them and then storing them in the database. I'd break it down more but that part I can figure out on my own cause I do want to do more than just store the file names and corresponding directory its in in the DB. My issue is I can find anything or figure anything out that will cycle through all directories, thats what I need to figure out first. |