Fires of Heaven Guild Message Board  

Go Back   Fires of Heaven Guild Message Board > General forums > Development
User Name
Password
Or, use your gamerDNA username: (more...)
ForumSpy Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
Old 07-05-2008, 11:52 AM   #1 (permalink)
Believe
Whiskey Tango Foxtrot
 
Believe's Avatar
 
Join Date: Aug 2006
Posts: 2,123
-5 Internets
PHP script question

Anyone familiar with PHP that could help me with a script? I need a script that can delete a file via FTP, that can be ran with a CronJob.

Any help would be appreciated
__________________
Believe is offline   Reply With Quote
Old 07-06-2008, 12:28 AM   #2 (permalink)
Sabolin
Registered User
 
Join Date: May 2003
Posts: 313
+9 Internets
This should be pretty easy to do. Get started here: PHP: FTP - Manual .. basically you will use ftp_connect, ftp_login, ftp_chdir if necessary, then ftp_delete, in that order.
Sabolin is offline   Reply With Quote
Old 07-06-2008, 12:33 AM   #3 (permalink)
Sabolin
Registered User
 
Join Date: May 2003
Posts: 313
+9 Internets
Some further help.. simplified but this should get you started.

$conn = ftp_connect('your ftp server') or die('Failed to connect.');
$login = ftp_login($conn, 'username','password') or die('Failed to login.');
//if needed:
//$chgdir = ftp_chdir($conn, 'directory_name') or die('Failed to change directory');
//can also just type in the path to the file in ftp_delete instead of changing directory
$delete = ftp_delete($conn, 'path_to_file') or die('Failed to delete.');
Sabolin is offline   Reply With Quote
Old 07-07-2008, 07:55 AM   #4 (permalink)
Moontayle
Romo is a manwhore
 
Moontayle's Avatar
 
Join Date: Oct 2003
Location: Dallas, TX
Posts: 2,952
Send a message via AIM to Moontayle
Hmm, this looks like a good thread for my question.

I'm currently attempting a project which requires a lot of database calls across what is likely to be a lot of different .php files. Rather than have each script individually call the database (mysqli_connect()), would it be prudent for me to create a class which does this and simply call the class when necessary?
__________________
Moontayle is offline   Reply With Quote
Old 07-07-2008, 09:51 AM   #5 (permalink)
Sabolin
Registered User
 
Join Date: May 2003
Posts: 313
+9 Internets
Yes, the way I do it is have a class called something like opendb.php that contains the mysql_connect call with the user/pass and anything else you want (ie, log functions, any specific routine that must be ran before or after connecting to the db, etc.) then in the php files that will use this, you can use require or require_once to run the opendb.php or whatever you name it.

Generally, if on a webserver, the opendb.php would be in a folder that is hidden from everyone but owner (permissions would be 700 for unix webserver).
Sabolin is offline   Reply With Quote
Old 07-07-2008, 05:47 PM   #6 (permalink)
Moontayle
Romo is a manwhore
 
Moontayle's Avatar
 
Join Date: Oct 2003
Location: Dallas, TX
Posts: 2,952
Send a message via AIM to Moontayle
Thanks. It's a couple of additional lines in the code but now I don't have to type out that damned mysqli_connect function for god knows how many files this thing will end up using. Plus, when I change the user from Root to something a bit more secure, I don't need to tear my hair out on the updating.
__________________
Moontayle is offline   Reply With Quote
Old 07-07-2008, 06:56 PM   #7 (permalink)
Zippygoose
Math Enthusiast/Badass MC
 
Zippygoose's Avatar
 
Join Date: Jun 2002
Location: Seattle
Posts: 650
+0 Internets
Send a message via AIM to Zippygoose
Yeah, if you are ever having to make the same change in multiple places it is time to step back and re-think how you've architected it
Zippygoose is online now   Reply With Quote
Old 07-08-2008, 04:49 PM   #8 (permalink)
Slide
Limey Bastard
 
Join Date: Mar 2004
Location: London innit
Posts: 744
-3 Internets
Arrow

Code:
db.php <?php $link_id = mysql_pconnect($server, $username, $password); mysql_select_db($link_id, $db); ?> page.php <?php require_once("db.php"); $results = mysql_query($sql, $link_id); ?>
Quick and dirty, you should make a db class to do this, and build in input cleaning functionality etc into it.

Edit: At work now, here's my db class. Username etc are hard coded, easy enough to change that.

Code:
class db { private $linkId; private $preparedQuery; private $resultId; function __construct() { $this->linkId = mysql_pconnect("localhost", "support", "password"); mysql_selectdb("support", $this->linkId); $this->preparedQuery = ""; } private function clean_input($string) { return mysql_real_escape_string($string, $this->linkId); } function prepare($sql_query, $values = "") { if (is_array($values)) { foreach ($values as $key => $value) { $safe_value = $this->clean_input($value); $sql_query = str_replace($key, $safe_value, $sql_query); } $this->preparedQuery = $sql_query; } else { $this->preparedQuery = $sql_query; } } function execute($debug = FALSE) { if ($this->preparedQuery) { if ($debug == TRUE) { echo $this->preparedQuery; } $this->resultId = mysql_query($this->preparedQuery, $this->linkId); if ($this->resultId) { return $this->resultId; } else { return false; } } else { return false; } } function error() { echo mysql_error($this->linkId); } function num_rows($resultId = "") { if ($resultId) { return mysql_num_rows($resultId); } else { return mysql_num_rows($this->resultId); } } function fetch_assoc($resultId = "") { if ($resultId) { return mysql_fetch_assoc($resultId); } else { return mysql_fetch_assoc($this->resultId); } } } $db = new db(); $sql_query = "SELECT * FROM foo WHERE var=&param"; $sql_param = array("&param"=>"blah"); $db->prepare($sql_query, $sql_param); $result = $db->execute();

Last edited by Slide : 07-09-2008 at 03:13 AM.
Slide is offline   Reply With Quote
Old 07-09-2008, 08:15 AM   #9 (permalink)
Moontayle
Romo is a manwhore
 
Moontayle's Avatar
 
Join Date: Oct 2003
Location: Dallas, TX
Posts: 2,952
Send a message via AIM to Moontayle
Heh, I'm just starting out with learning PHP though it's not really a complex language so that's not too hard to follow. My problem is that I learned procedural and am just now beginning to grasp the basics of OOP.

I threw this together the other day:

Code:
class opendb { function opendb() { $this->mysqli = mysqli_connect("blah", "blahblah", "meh", "mehmeh"); return $this->mysqli; } }
And then just added this to my script:
Code:
require_once("opendb.php"); $db = new opendb; $mysqli = $db->opendb();
It serves my purposes at the moment.
__________________
Moontayle is offline   Reply With Quote
Old 07-09-2008, 11:45 AM   #10 (permalink)
Sabolin
Registered User
 
Join Date: May 2003
Posts: 313
+9 Internets
You don't even have to bother with a class or function. If you just put:
Quote:
$conn = mysqli_connect("blah", "blahblah", "meh", "mehmeh");
into the opendb.php, when you call require_once("opendb.php") it will open the connection. Turns 6 lines of code in your example into 2.

Now if you're going to be opening multiple concurrent connections to the same db for whatever reason, then your method is fine.
Sabolin is offline   Reply With Quote
Old 07-09-2008, 12:27 PM   #11 (permalink)
Slide
Limey Bastard
 
Join Date: Mar 2004
Location: London innit
Posts: 744
-3 Internets
Quote:
Originally Posted by Sabolin View Post
You don't even have to bother with a class or function. If you just put:

into the opendb.php, when you call require_once("opendb.php") it will open the connection. Turns 6 lines of code in your example into 2.

Now if you're going to be opening multiple concurrent connections to the same db for whatever reason, then your method is fine.
the simple method is fine, but it fucks up when someone calls foo.php?user=;DELETE FROM blah;
Slide is offline   Reply With Quote
Old 07-09-2008, 12:34 PM   #12 (permalink)
Sabolin
Registered User
 
Join Date: May 2003
Posts: 313
+9 Internets
Quote:
Originally Posted by Slide View Post
the simple method is fine, but it fucks up when someone calls foo.php?user=;DELETE FROM blah;
Explain further, because we aren't using any GET variables or running any queries with the code we are discussing.
Sabolin is offline   Reply With Quote
Old 07-09-2008, 01:45 PM   #13 (permalink)
Slide
Limey Bastard
 
Join Date: Mar 2004
Location: London innit
Posts: 744
-3 Internets
Quote:
Hmm, this looks like a good thread for my question.

I'm currently attempting a project which requires a lot of database calls across what is likely to be a lot of different .php files. Rather than have each script individually call the database (mysqli_connect()), would it be prudent for me to create a class which does this and simply call the class when necessary?
__________________
Dont see any mention of taking or not taking user input in this question. Always err on the side of caution...
Slide is offline   Reply With Quote
Old 07-09-2008, 03:39 PM   #14 (permalink)
Moontayle
Romo is a manwhore
 
Moontayle's Avatar
 
Join Date: Oct 2003
Location: Dallas, TX
Posts: 2,952
Send a message via AIM to Moontayle
I don't foresee a situation where I would be taking user input in terms of the database call. Then again, I just started in on this so that may or may not change between now and the actual end result.
__________________
Moontayle is offline   Reply With Quote
Old 07-10-2008, 02:32 PM   #15 (permalink)
Slide
Limey Bastard
 
Join Date: Mar 2004
Location: London innit
Posts: 744
-3 Internets
Quote:
Originally Posted by Moontayle View Post
I don't foresee a situation where I would be taking user input in terms of the database call. Then again, I just started in on this so that may or may not change between now and the actual end result.
Yeah you never know, abstracting it also has the benefit of isolating you to some exent if there's a requirement to use another database. Been hacking PHP for 6-7 years, just sharing my experience
Slide is offline   Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is On
Trackbacks are On
Pingbacks are On
Refbacks are On
uberguilds network



All times are GMT -7. The time now is 01:30 AM.


Powered by vBulletin® Version 3.6.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.0.0 RC6