PHP - Image Clean Up Script Help
Hi
Im trying to write a script to clean up my image directory which has quite a lot of unused images that have built up over time. In order to do this I am doing the following. First Create a database table called 'image_clean' Then I'm searching through 3 tables and collecting all the image file names and dumping the names in the table 'image_clean' Can do that no problem. So now I have all the images I need in this one table 'image_clean' I now want to go through my directory 'image_uploads' and delete anything thats not in the 'image_clean' table. I know how to delete the files using unlink Im just unsure how to search through the directory file by file and check the file against the database. Im asumming I need to put them in an array. Could anyone give be a clue or two to get me started. I have no problem checking a database against a directory but when its the other way round 'checking a directory against a database I'm lost. What I might do is pop the files to delete in a new database called 'image_delete' so that I can then check the images to delete before I write the unlink script. But I'm just not sure how to pick up each file and compare it to the table. Thanks in advance. Similar TutorialsWell the subject line is pretty explicit. I found this script that uploads a picture onto a folder on the server called images, then inserts the the path of the image on the images folder onto a VACHAR field in a database table. Code: [Select] <?php //This file inserts the main image into the images table. //address error handling ini_set ('display_errors', 1); error_reporting (E_ALL & ~E_NOTICE); //authenticate user //Start session session_start(); //Connect to database require ('config.php'); //Check whether the session variable id is present or not. If not, deny access. if(!isset($_SESSION['id']) || (trim($_SESSION['id']) == '')) { header("location: access_denied.php"); exit(); } else{ // Check to see if the type of file uploaded is a valid image type function is_valid_type($file) { // This is an array that holds all the valid image MIME types $valid_types = array("image/jpg", "image/jpeg", "image/bmp", "image/gif"); if (in_array($file['type'], $valid_types)) return 1; return 0; } // Just a short function that prints out the contents of an array in a manner that's easy to read // I used this function during debugging but it serves no purpose at run time for this example function showContents($array) { echo "<pre>"; print_r($array); echo "</pre>"; } // Set some constants // This variable is the path to the image folder where all the images are going to be stored // Note that there is a trailing forward slash $TARGET_PATH = "images/"; // Get our POSTed variable $image = $_FILES['image']; // Sanitize our input $image['name'] = mysql_real_escape_string($image['name']); // Build our target path full string. This is where the file will be moved to // i.e. images/picture.jpg $TARGET_PATH .= $image['name']; // Make sure all the fields from the form have inputs if ( $image['name'] == "" ) { $_SESSION['error'] = "All fields are required"; header("Location: member.php"); exit; } // Check to make sure that our file is actually an image // You check the file type instead of the extension because the extension can easily be faked if (!is_valid_type($image)) { $_SESSION['error'] = "You must upload a jpeg, gif, or bmp"; header("Location: member.php"); exit; } // Here we check to see if a file with that name already exists // You could get past filename problems by appending a timestamp to the filename and then continuing if (file_exists($TARGET_PATH)) { $_SESSION['error'] = "A file with that name already exists"; header("Location: member.php"); exit; } // Lets attempt to move the file from its temporary directory to its new home if (move_uploaded_file($image['tmp_name'], $TARGET_PATH)) { // NOTE: This is where a lot of people make mistakes. // We are *not* putting the image into the database; we are putting a reference to the file's location on the server $sql = "insert into images (member_id, image_cartegory, image_date, image) values ('{$_SESSION['id']}', 'main', NOW(), '" . $image['name'] . "')"; $result = mysql_query($sql) or die ("Could not insert data into DB: " . mysql_error()); header("Location: images.php"); echo "File uploaded"; exit; } else { // A common cause of file moving failures is because of bad permissions on the directory attempting to be written to // Make sure you chmod the directory to be writeable $_SESSION['error'] = "Could not upload file. Check read/write persmissions on the directory"; header("Location: member.php"); exit; } } //End of if session variable id is not present. ?> The script seems to work fine because I managed to upload a picture which was successfully inserted into my images folder and into the database. Now the problem is, I can't figure out exactly how to write the script that displays the image on an html page. I used the following script which didn't work. Code: [Select] //authenticate user //Start session session_start(); //Connect to database require ('config.php'); $sql = mysql_query("SELECT* FROM images WHERE member_id = '".$_SESSION['id']."' AND image_cartegory = 'main' "); $row = mysql_fetch_assoc($sql); $imagebytes = $row['image']; header("Content-type: image/jpeg"); print $imagebytes; Seems to me like I need to alter some variables to match the variables used in the insert script, just can't figure out which. Can anyone help?? Hello friends if i've this $text = "i love adult sites" then i wanna clean it by elminate words like adult - bad - kill - die so it be $clean = "i love sites" as you can see it eliminated the word adult how it could be which code can i use ? thanks in advance Hey guys I have a lot of inputs from my form. Is there a way I can do like a for each or something instead of of having to write $myusername = stripslashes($_POST['name'); $mypassword = stripslashes($_POST['pass']); $myusername = mysql_real_escape_string($myusername); $mypassword = mysql_real_escape_string($mypassword); .... for all 16 fields? thanks What is the cleanest way to write this if? Code: [Select] <div <?php if (!empty($otherfans)) {echo "class=\"newstext\"";} else {echo "class=\"newstext pts\""; } ?>> My code here Code: [Select] setcookie('hide_div', $_COOKIE['hide_div'].','.$_GET['hide'],time()+32000000); When I set it, it works wonderful, but then when I see view it in my cookie it shows this code between my $_GET['hide'] values Code: [Select] %2C how do I clean it up and just make it show my " , "? Thank you Hi, Here is a tiny bit of my code $r = mysql_query ($query); while ($row = mysql_fetch_array ($r, MYSQL_ASSOC)) { echo "<tr> <td align=\"center\">"; echo date("M-d-Y", mktime(0, 0, 0, $row['month'], $row['day'], $row['year'])); echo "</td> "; if ($row['presenter1status'] == '0') { echo "<td align=\"center\"><font color=\"#FF9900\">{$row['presenter1']}</font></td>"; } elseif ($row['presenter1status'] == '1') { echo "<td align=\"center\"><font color=\"green\">{$row['presenter1']}</font></td>"; } elseif ($row['presenter1status'] == '2') { echo "<td align=\"center\"><font color=\"red\">{$row['presenter1']}</font></td>"; } if ($row['presenter2status'] == '0') { echo "<td align=\"center\"><font color=\"#FF9900\">{$row['presenter2']}</font></td>"; } elseif ($row['presenter2status'] == '1') { echo "<td align=\"center\"><font color=\"green\">{$row['presenter2']}</font></td>"; } elseif ($row['presenter2status'] == '2') { echo "<td align=\"center\"><font color=\"red\">{$row['presenter2']}</font></td>"; } if ($row['engineerstatus'] == '0') { echo "<td align=\"center\"><font color=\"#FF9900\">{$row['engineer']}</font></td>"; } elseif ($row['engineerstatus'] == '1') { echo "<td align=\"center\"><font color=\"green\">{$row['engineer']}</font></td>"; } elseif ($row['engineerstatus'] == '2') { echo "<td align=\"center\"><font color=\"red\">{$row['engineer']}</font></td>"; } echo "</tr> I was wondering if there is a better way of doing this as I think including this many ifesle statements in a while loop that could go round about 40 times might be very heavy on the resources. Thanks I've been messing around with clean urls in php and I've been having some trouble. I'm working on a private messaging system and when I go to "sitename.com/mail/view.php?page=inbox" it correctly displays the users inbox messages but when I put "sitename.com/mail/view/inbox/" it just displays the page like the GET value isn't set. Why is that? this is the HTACCESS file for clean url RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME}\.php -f RewriteRule ^view/([a-z]) view.php?page=$1 [NC,L]
Hi, I am creating a website but am having trouble with the following: I have an include file(db.inc) which contains the following: function clean($input, $maxlength) { $input = substr($input, 0, $maxlength); $input = EscapeShellCmd($input); return ($input); } The file I am having the problem with is my view.php file. I get the following error: Notice: Undefined variable: file in C:\wamp\www\fermpix\view.php on line 4 Line 4 contains the following: $file = clean($file, 4); My view.php does have the line: include 'db.inc'; Does anybody have any idea what the issue might be? Cheers Paul Hey all, I would like to some how clean up an if statement to be a little cleaner. Code: [Select] if(condition && condition && (condition && condition) || (condition && condition) || (condition && condition)) How can I do that? I created a new homepage for my network of sites I run. I tried to go for a simple and clean look. The pages themselves function how I'd like, but I'm unsure if the menu system is easy to understand or not.
I even added a time limited message on the front page of the site indicating that the logo was clickable, but purposely left it off of other sub-pages to try and keep the design as clean as possible.
Check it out, let me know what you think.
http://ctenetwork.com
if any body copy and paste anything from word to editor some unwanted css also coming with that pasting. when we request the data same css also coming with that. so how to clean data when we request $desc =$_request['contents']; how to solve this issue. please help me. I'm new to OOP and trying to write tiny classes to get practice. The next class I want to create will be used to clean HTTP data from when a form gets submitted. I'm embarrassed to say, but I'm scratching my head trying to figure out what types of things I should do as far as "sterilizing" POST and GET data?! Can someone get me started here? Thanks, TomTees I currently use the following function to clean form inputs to prevent MySql injection, Does this function do enough to prevent MySql injection? is there anything i have missed? <?php //Function to sanitize values received from the form. Prevents SQL injection function clean($str) { $str = @trim($str); if(get_magic_quotes_gpc()) { $str = stripslashes($str); } return mysql_real_escape_string($str); } ?> I am currently using my own error handler so i can log all mysql errors into a mysql table: mysql_query('select data from table')or trigger_error(mysql_error(),256); If there's an error i need to know the line number where the mysql_query function was called. Is it possible to instead use this code: mysql_query('select data from table'); but somehow transparently log a mysql error and the line number if there is an error in the query? thanks I've been using clean URLs and it's been giving my PHP sessions for my user system some trouble. I display the logged in users username on every page via a header.php file that I require on every page. Sometimes when I click a link to navigate to a page with a clean URL, the session information "disappears" and asks the user to login but if I navigate to another page from the clean url that the session "disappeared" on, the logged in users username is displayed at the top of the page like normal. Any idea certain pages cause the session to "disappear"
Header.php where the user info is displayed. $_SESSION['username'] is set on the login page <? session_start(); if (isset($_SESSION['username'])) { echo "Welcome back, " . $_SESSION['username']; } ?> Edited June 13, 2020 by Nematode128 i have my form with 5 fields that are not required to be filled out. Code: [Select] <input class="inputbg" type="text" name="MAC[]" /> <input class="inputbg" type="text" name="MAC[]" /> <input class="inputbg" type="text" name="MAC[]" /> <input class="inputbg" type="text" name="MAC[]" /> <input class="inputbg" type="text" name="MAC[]" /> On my process page I want to remove all of the empty index's that did not have data entered. I am new to sending array data via POST do I need to do anything else other then what is below? Code: [Select] foreach($MAC as $key => $value) { if($value == "") { unset($MAC[$key]); } } $mac_addresses = array_values($MAC); if(empty($mac_addresses)){ $error = "You did not enter any MAC adresses."; $valid = "false"; } Hello everybody,
I am honestly quite a newb when it comes to mod_rewrite.
We run a small social media page with different areas and I would like to change the URLs to something more clean and professional.
User profiles look like this:
http://www.sky-mp3.com/index.php?action=cm&siteid=59&wahl=artists&tat=details&keyid=477siteid 59 is the artists list and the keyid at the end is the ID of the artist but should be like: http://www.sky-mp3.com/mischuraor in worse case like: http://www.sky-mp3.com/user/mischuraCMS pages look like this: http://www.sky-mp3.com/index.php?siteid=106but should be like: http://www.sky-mp3.com/charts(page name instead of siteid) What I know so far: - I have to add something to the .htaccess file - I need to change something in the code (but I don`t know where) Im good he? What would be the first step on the path to clean URLs for me? I found alot of infos here and there but found nothing yet for this specific case. Kind regards from and thx in advance from Cologne Hi Guys, I am trying to disguise my php script as a .gif file for the purposes of a shopping cart. Basically, you click on the .gif which is placed on the page and it takes you to an image unless it has variables attached to it. Google use this for their checkout so I know this is possible. Does anyone have any ideas how I would go about doing this? Thanks in advance, Jacob. hello i have this php code to show the last image upload in a folder and the result is this http://theplattan.com/screenlive2/Screenshooter2.php this code must be in the same image folder to work. I need add a php code from my joomla website but i dont know where i can change the path image folder on this script. i have tried by my self to change rad 33 $files = dirList('.'); to $files = dirList('screenlive2'); but this show only the list and not the image, somebody have any idea? Code: [Select] 1: <html> 2: <head> 3: <style type="text/css"> 4: #files { float: left; font-size: small; } 5: #image { float: right; } 6: .links { float: right; left: -40px; position: relative; } 7: .selected { font-weight: bold; } 8: </style> 9: </head> 10: <body> 11: 12: <?php 13: 14: function dirList ($directory) 15: { 16: $results = array(); 17: $handler = opendir($directory); 18: while ($file = readdir($handler)) { 19: if ($file != '.' && $file != '..') 20: { 21: $suffix = "gif"; 22: 23: if( substr($file, -3) == $suffix ) 24: { 25: $results[] = $file; 26: } 27: } 28: } 29: closedir($handler); 30: return $results; 31: } 32: 33: $files = dirList('.'); 34: 35: arsort($files); 36: 37: //$files = array_reverse( $files ); 38: 39: echo "<div id='files'><ul>"; 40: 41: $prev = ""; 42: $next = ""; 43: $last = ""; 44: 45: foreach( $files as $entry ) 46: { 47: if( isset($_GET['sel']) == false ) 48: { 49: $_GET['sel'] = $entry; 50: } 51: 52: $style = ""; 53: 54: if( $_GET['sel'] == $entry ) 55: { 56: $style="selected"; 57: $selected = $entry; 58: $prev = $last; 59: } 60: 61: echo "<li class='".$style."'><a href='i.php?sel=".$entry."'>"; 62: print_r( $entry ); 63: echo "</a></li>"; 64: 65: if( ($last == $selected) && ($last != "") ) 66: { 67: $next = $entry; 68: } 69: 70: $last = $entry; 71: } 72: 73: echo "</ul></div>"; 74: 75: echo "<div id='image'><img src='".$selected."' alt=''/></div>"; 76: 77: echo "<p class='links'>"; 78: 79: if( $prev != "" ) echo "<a href='i.php?sel=".$prev."'>Up</a>"; 80: echo "<br/>"; 81: if( $next != "" ) echo "<a href='i.php?sel=".$next."'>Down</a>"; 82: 83: echo "</p>"; 84: 85: ?> 86: 87: </body> 88: </html> I have 23 products in a db, all with a unique ID that Im creating a user site for. Some of the items have 1 or 2 images and others have as many as 30 images. In my db I have a field called "pics". All the images are saved as a .jpg, according to their id on the server. So if item 1 has 10 images the files a "1_001.jpg," "1_002.jpg," ect. How do I create a script that will display the correct amount of images based on the number in the db. I was going to do something like this: Code: [Select] if ($row['pics'] =="1") { $pictures = '<img src="pictures/'.$id.'_001.jpg" width="200" alt="" />'; } elseif ($row['pics'] =="2") { $pictures = '<img src="pictures/'.$id.'_001.jpg" width="200" alt="" /><img src="pictures/'.$id.'_002.jpg" width="200" alt="" />'; } elseif ($row['pics'] =="3") { $pictures = '<img src="pictures/'.$id.'_001.jpg" width="200" alt="" /><img src="pictures/'.$id.'_002.jpg" width="200" alt="" /><img src="pictures/'.$id.'_003.jpg" width="200" alt="" />'; } but that would just be retarded... Thank you! |