[Tool/Web/Other] [PHP] help needed..
#1

Hey,
I'm bad at PHP (but understand a little), and I've got this code (explanation what I want inside the code):
PHP код:
$conn ftp_connect("FTP SERVER") or die("Sorry!<br /> Database is unavailable at the moment!"); // Connecting to remote FTP
        
ftp_login($conn,"LOGIN","PASS");
        
ftp_get($conn,"file.sav","directory/name.sav",FTP_ASCII); //Taking file from the server
        
$file "file.sav";
/*
           file.sav would look like this:
             --------------------------
             parameter1=12345
             parameter2=1234
             parameter3=13456
             parameter4=134894
             parameter5=123
             
             (Numbers are random here)
*/

$handle fopen($file'r+'); //( r+ - Read/Write. Starts at the beginning of the file)
        
$data fread($handlefilesize($file)); // Reads the data (hope this works)
        
if($data)
        {
            if(
stristr($data,"parameter3") != FALSE// Don't know if this is right
            
{
                
//I need to get value of parameter3, and change it to 999 or something..
            
}            
        }
        
//And then it should save the file and upload it to remote FTP server..
        
ftp_close($conn);//Closing connection
        
*/ 
Reply
#2

What are you trying to do with this?
Reply
#3

Quote:
Originally Posted by Chris - X-Host
What are you trying to do with this?
I think I've explained everything well..
But if you didn't understood:
I want to get file from the remote FTP server, change a value in it, and upload it back to remote FTP server..
Reply
#4

That's a bit of a waste of Bandwidth...
You could(/should?) use a SSH script instead, ofcourse making sure it's linux. Just run a simple command then.

Example:
Код:
mv [old filename] [new filename]
Reply
#5

gimme a sec, i'll write you a get and set function
Reply
#6

Quote:
Originally Posted by darkrider366
That's a bit of a waste of Bandwidth...
You could(/should?) use a SSH script instead, ofcourse making sure it's linux. Just run a simple command then.

Example:
Код:
mv [old filename] [new filename]
Why? If I use that once a week?

Quote:
Originally Posted by iLinx
gimme a sec, i'll write you a get and set function
Ok, thanks..
Reply
#7

okay, took a little longer then i thought it would, but thats alright.
there's probably a more efficent method to do this, but i suppose this will do for now

PHP код:
function set($data$key$value) {
    
$regex "/^$key+.+\n/";
    
$data preg_replace($regex"$key=$value\r\n"$data);
    return 
$data;    
}

function 
get($data$key) {
    
$data explode("\n"$data);
    
    
$regex "/$key+=+.+$/";
    
$f "";
    for(
$i 0$i count($data); $i++) {
        if(
preg_match($regex$data[$i])) {
            
$f $data[$i];
            break;
        }
    }
    
    if(
is_array($f))
        return 
false;
    else {
        
$regex "/$key+=/";
        
$f preg_replace($regex''$f); //comment this line if you would like it to return KEY=VALUE instead of just VALUE
        
return $f;
    }

let me give you a quick rundown of how it works.
set requires 3 inputs, the current data in your file, you have it defined as $data, it requires a key and a value ( like dini ). the key is what it searches for, the value is what it updates it with. When it's done it returns an updated version of the whole file, or whatever data was.

Heres an example on how to use it

PHP код:
if($data)
{
    
$data set($data"parameter1"22);
    
$data set($data"parameter3"62);
    
fwrite($handle$data);

get requires 2 values, data and the key. It will search for the key and return its corresponding VALUE, or false if it can't find it.

example
PHP код:
if($data
{
    echo 
get($data"parameter1");

hope it helps, and let me know if something doesnt work, i checked it a couple of times but im tired as shit today so i may have left an error out somewhere
Reply
#8

Thank u soo much man..
+10 rep..

And one thing:
How could I upload it back to the remote FTP server?
Should I use ftp_put or what?
Reply
#9

yeah
Quote:
Originally Posted by SiJ
Thank u soo much man..
+10 rep..

And one thing:
How could I upload it back to the remote FTP server?
Should I use ftp_put or what?
yep
Reply
#10

yeah that would be the easiest way
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)