PHP - Optimizing Is_dir()
I have approximately 50k-100k files in a directory. I'm running a script to check if any of the files are being used by the DB, if not then delete them. The problem is that I have made a quick test on a directory of just 1k files and it dies. Is there a way to optimize it? I know the script works, it's just that it takes too long to run. Even with just 1k files. And I'm pretty sure is the is_dir that's taking its sweet time. Any ideas?
<?php require_once 'db_connect.php'; $default_dir = "storage/2011/"; if(!($dp = opendir($default_dir))) die("Cannot open $default_dir."); while($file = readdir($dp)) { if(is_dir($file)) { continue; } else if($file != '.' && $file != '..') { $query = "SELECT * FROM images Where filename = '".$file."' OR thumbname = '".$file."'"; $dbResult = mysql_query($query); $num_rows = mysql_num_rows($dbResult); if ($num_rows == 0){ unlink($default_dir.$file); echo $file."<br />"; } } } closedir($dp); ?> Similar TutorialsI have the following code... While(($file = readdir($handle)) !== FALSE){ if(is_dir($file))){ echo "$file - Directory<br>"; }else{ echo "$file - Not Directory<br>"; } }
And I have the following files/folders... 2019-holiday-party _photos _thumbnails xxx IMG_2205.png
I get these results... . - Directory .. - Directory _photos - Not Directory _thumbnails - Not Directory IMG_2205.png - Not Directory xxx - Not Directory
Why do the directories "_photos" and "_thumbnails" and "xxx" get reported as "Not Directory"???
Also, where does the "." (dot) and ".." (dot dot) come from?? Edited December 23, 2019 by SaranacLake Hi I am trying to learn to clean coding and learning to clean old coding for any vulnerabilities. Can anyone help me with this code and see what I missed. I know theres a lot can anyone help so I can someday be able to do this myself easily please. Thanks for any and all help... Code: [Select] <? include("connect.php"); include("mail.php"); include("config.php"); function SendCounterMail($butlerstat1, $updatestat1) { $content1 = ''; $content1 .= "<font style='font-size: 10px;font-family: Arial, Helvetica, sans-serif;color: #333333;'>". "</font><br>"."<br>". "<p align='center' style='font-size: 14px;font-family: Arial, Helvetica, sans-serif;font-weight:bold;'>Counter Information</p>". "<br>"."<table border='0' cellpadding='3' cellspacing='0' width='100%' align='center' class='style13'>"; if ( $butlerstat1 ) $content1 .= "<tr style='font-size: 10px;font-family: Arial, Helvetica, sans-serif;color: #333333;'>". "<td>Butler file is not running so it is now running by this process.</td></tr>"; if ( $updatestat1 ) $content1 .= "<tr style='font-size: 10px;font-family: Arial, Helvetica, sans-serif;color: #333333;'>". "<td>Records file is not running so it is now running by this process.</td></tr>"; $content1 .= "<tr style='font-size: 10px;font-family: Arial, Helvetica, sans-serif;color: #333333;'>". "<td>This mail is testing for check file running in.</td></tr></table>"; $subject = "Counter Information"; $from=$adminemailadd;//= "newuser@thissite.com"; $email = $adminemailadd; //SendHTMLMail($email, $subject, $content1, $from); } $butlerstat = FALSE; $updatestat = FALSE; $ressel = mysql_query("select referral_bids from auction_pause_management where id=3"); if ( mysql_num_rows($ressel) == 0 ) { mysql_free_result($ressel); mysql_query("Insert into auction_pause_management (referral_bids) values (1)"); $ressel = mysql_query("select referral_bids from auction_pause_management where id=3"); } $oldvalue1 = mysql_result($ressel, 0); mysql_free_result($ressel); $ressel = mysql_query("select referral_bids from auction_pause_management where id=4"); if ( mysql_num_rows($ressel) == 0 ) { mysql_free_result($ressel); mysql_query("Insert into auction_pause_management (referral_bids) values (1)"); $ressel = mysql_query("select referral_bids from auction_pause_management where id=4"); } $oldvalue = mysql_result($ressel, 0); mysql_free_result($ressel); sleep(2); $ressel = mysql_query("select referral_bids from auction_pause_management where id=3"); $newvalue1 = mysql_result($ressel, 0); mysql_free_result($ressel); $ressel = mysql_query("select referral_bids from auction_pause_management where id=4"); $newvalue = mysql_result($ressel, 0); mysql_free_result($ressel); //if ( $oldvalue1 == $newvalue1 ) { $output1 = exec("php ".getcwd()."/update_butler.php >/dev/null &"); $butlerstat = TRUE; //} //if ( $oldvalue == $newvalue ) { $output = exec("php ".getcwd()."/update_records.php >/dev/null &"); $updatestat = TRUE; //} //SendCounterMail($butlerstat, $updatestat); //mysql_close($db); ?> Code: [Select] <?php $query = 'SELECT COUNT(*) FROM `fonts`'; $result = mysql_query($query) or die('Sorry, we could not count the number of results: ' . mysql_error()); $numberofresults = mysql_result($result, 0); $numberofresults = $numberofresults + 1; /*** query the database ***/ $i=1; /*** loop over the results ***/ while($i<$numberofresults) { $querySelect1 = "SELECT * FROM fonts WHERE id='$i'"; $resultSelect1 = mysql_query($querySelect1); $row2 = mysql_fetch_array($resultSelect1); /*** create the options ***/ echo '<option value="'.$row2['id'].'"'; if ($row2['id']==$font){ echo "selected='selected'"; } echo '>'.$row2['font']. '</option>'."\n"; $i++; } ?> Currently my site is still underdevelopment and I was having trouble creating a dynamically driven drop down to select which font the user wanted out of the selection that are available. I managed to get this to work just for proof of concept so I could move onto other things. Any help on how to get this to work in a more efficient manner would be greatly appreciated. Hi
I have a small script, on a single file that needs to be optimized.
file is about 300 lines (16Kb), and basiccaly looks like the "PHP-monster" threw up on it.
Script is working ok, but surely could be much better.
PM if interested and I can send you a copy of file. If possible leave price/hourly rate.
I have created a form, that contains multiple text fields. When the user clicks on the Submit button, the form data should be populated in MySQL, I have programmed it in a way, that it works well, but the problem is, Since it contains so many text fields that collects the Manufacturer, Brand and so on repeatedly, I have created multiple queries to populate the DB. Here is the code, Code: [Select] <?php include("connect.php"); if (isset($_POST['confirm_order'])) { $o_manufacturer = trim(@$_POST['o_manufacturer']); $o_brand = trim(@$_POST['o_brand']); $o_type = trim(@$_POST['o_type']); $o_exp_date = trim(@$_POST['o_exp_date']); $o_qty = trim(@$_POST['o_qty']); $o_unit = trim(@$_POST['o_unit']); $o_unit_price = trim(@$_POST['o_unit_price']); $o_package = trim(@$_POST['o_package']); $o_package_price = trim(@$_POST['o_package_price']); $o_selling_price_perUnit = trim(@$_POST['o_selling_price_perUnit']); $query = "INSERT INTO order_entry (o_sno, o_manufacturer, o_brand, o_type, o_exp_date, o_qty, o_unit, o_unit_price, o_package, o_package_price, o_selling_price_perUnit) VALUES ('', '$o_manufacturer', '$o_brand', '$o_type', '$o_exp_date', '$o_qty', '$o_unit', '$o_unit_price', '$o_package', '$o_package_price', '$o_selling_price_perUnit')"; $results = mysql_query($query); if ($results) { echo '<center><p class=small>Order updation successful, Click on LIST ORDERS button below to view the order entry.</p><br></center>'; echo '<center><a href="List_Orders.php" class="myButton">List Orders</a></center>'; } // Second Row Insertion if(!empty($_POST['o_manufacturer1'])) { $query = "INSERT INTO order_entry (o_sno, o_manufacturer, o_brand, o_type, o_exp_date, o_qty, o_unit, o_unit_price, o_package, o_package_price, o_selling_price_perUnit) VALUES ('', '".$_POST['o_manufacturer1']."' , '".$_POST['o_brand1']."' , '".$_POST['o_type1']."' , '".$_POST['o_exp_date1']."' , '".$_POST['o_qty1']."' , '".$_POST['o_unit1']."' , '".$_POST['o_unit_price1']."' , '".$_POST['o_package1']."' , '".$_POST['o_package_price1']."', '".$_POST['o_selling_price_perUnit1']."' )"; $results = mysql_query($query); } // Third Row Insertion if(!empty($_POST['o_manufacturer2'])) { $query = "INSERT INTO order_entry (o_sno, o_manufacturer, o_brand, o_type, o_exp_date, o_qty, o_unit, o_unit_price, o_package, o_package_price, o_selling_price_perUnit) VALUES ('', '".$_POST['o_manufacturer2']."' , '".$_POST['o_brand2']."' , '".$_POST['o_type2']."' , '".$_POST['o_exp_date2']."' , '".$_POST['o_qty2']."' , '".$_POST['o_unit2']."' , '".$_POST['o_unit_price2']."' , '".$_POST['o_package2']."' , '".$_POST['o_package_price2']."', '".$_POST['o_selling_price_perUnit2']."' )"; $results = mysql_query($query); } // Fourth Row Insertion if(!empty($_POST['o_manufacturer3'])) { $query = "INSERT INTO order_entry (o_sno, o_manufacturer, o_brand, o_type, o_exp_date, o_qty, o_unit, o_unit_price, o_package, o_package_price, o_selling_price_perUnit) VALUES ('', '".$_POST['o_manufacturer3']."' , '".$_POST['o_brand3']."' , '".$_POST['o_type3']."' , '".$_POST['o_exp_date3']."' , '".$_POST['o_qty3']."' , '".$_POST['o_unit3']."' , '".$_POST['o_unit_price3']."' , '".$_POST['o_package3']."' , '".$_POST['o_package_price3']."', '".$_POST['o_selling_price_perUnit3']."' )"; $results = mysql_query($query); } // Fifth Row Insertion if(!empty($_POST['o_manufacturer4'])) { $query = "INSERT INTO order_entry (o_sno, o_manufacturer, o_brand, o_type, o_exp_date, o_qty, o_unit, o_unit_price, o_package, o_package_price, o_selling_price_perUnit) VALUES ('', '".$_POST['o_manufacturer4']."' , '".$_POST['o_brand4']."' , '".$_POST['o_type4']."' , '".$_POST['o_exp_date4']."' , '".$_POST['o_qty4']."' , '".$_POST['o_unit4']."' , '".$_POST['o_unit_price4']."' , '".$_POST['o_package4']."' , '".$_POST['o_package_price4']."', '".$_POST['o_selling_price_perUnit4']."' )"; $results = mysql_query($query); } mysql_close(); } ?> Instead of posting the 'o_manufacturer ' value multiple times 'o_manufacturer1', 'o_manufacturer2', 'o_manufacturer3' and 'o_manufacturer4' I m looking for a code, that uses for loop or any other alternative ways that optimizes the code and works the proper way... NOTE : The file that i have attached is working 100% perfect, but I am looking for code optimization. Looking for help from experts.. Thanks a lot in advance... I've been searching for a science that will solve this analysis problem. Since I don't know what I'm looking for, I cannot properly express the task to accomplish.
I have an eCommerce store selling a product that has (let's keep it simple) three properties and each property can hold any one of three possible relevant values (or null).
Combinations: 3^3=27
A typical combination would be round/solid/blue, or cubic/squishy/white.
The task is to efficiently eliminate that part of the array if any particular {set of values} is null. That is, if I currently have no pink items, the relevant sections of the array would get eliminated.
Product variations with low n-values is easy to manually handle. But with five and above, that's the math(?) theory I am trying to know the name of.
(I wonder if any Matlab manuals would give a clue.)
I am trying this elimination of sections of an established array instead of rebuilding the array every time a value is locked in.
On the other hand, maybe I'm trying to be too smart about this and should just let the server do the grunt work over and over.
|