Post Reply 
 
Thread Rating:
  • 0 Votes - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Interesting request from HF...
08-23-2010, 08:15 PM (This post was last modified: 08-24-2010 02:43 AM by l3g1sl4tor.)
Post: #1
Interesting request from HF...
Quote:Well, I need some php coder to write me little script about importing
few details from txt file or manual paste.

Have database table with columns:
Full Name - Address - Country - Phone

TXT file with details in format:
John Smith | PO Box 49 | USA | 430347743743
Jessica Herrow | PO Box 12 | USA | 430347743743
First Column | Second Column | Third Column | Four Column

That script need to import content from txt file into database and sort it in colums.
Content/Column is separeted with | and that will decide where details will be imported.

I need two ways for importing:
Manual paste in textfield and Browse for TXT file.

My PHP solution :

Code:
<?php
$conn = mysql_connect("dbhost", "dbuser", "dbpass") or die(mysql_error());
mysql_select_db("dbname") or die(mysql_error());

if($_GET[mode]=="file"){
function upload () {
$target_path = "";

$target_path = $target_path . basename( $_FILES['uploadedfile']['name']);

if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
$fh = fopen($target_path, 'r');
$data = fread($fh, filesize($target_path));
fclose($fh);

$lines = explode("\n", $data);
foreach($lines as $line) {
    $piece = explode(" | ", $line);
    $fullname = $piece[0];
    $address = $piece[1];
    $country = $piece[2];
    $phone = $piece[3];
    mysql_query("INSERT INTO tablename (Full Name,Address,Country,Phone) VALUES('$fullname','$address','$country','$phone')") or die(mysql_error());
}
mysql_close($conn);
}
else{
    echo "There was an error uploading the file, please try again!";
}
} ?>
<form enctype="multipart/form-data" action= <?php echo upload(); ?>  method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="100000" />
Choose a .txt file:
<input name="uploadedfile" type="file" /><br />
<input type="submit" value="Upload File" />
</form>

<?php
}
if($_GET[mode]=="paste") {

function textfield() {
$data = $_POST['textarea'];
$lines = explode("\n", $data);
foreach($lines as $line) {
    $piece = explode(" | ", $line);
    $fullname = $piece[0];
    $address = $piece[1];
    $country = $piece[2];
    $phone = $piece[3];
    mysql_query("INSERT INTO tablename (Full Name,Address,Country,Phone) VALUES('$fullname','$address','$country','$phone')") or die(mysql_error());
}
mysql_close($conn);
}

?>
<form name="form1" method="post" action="<?php echo textfield(); ?>">
  <textarea name="textarea" cols="100" rows="20"></textarea>
  <input type="submit" value="OK" />
</form>
<?php } ?>

<?php

if(!$_GET[mode]=="file") || (!$_get[mode]=="paste") {
echo "what are you looking for?";
}
?>

I haven't tested it, but it should work the job! Smile This guy probably requested this for storing stolen passwords to a database Tongue

Read rules Smile
[Image: legislator.png]
Find all posts by this user
Quote this message in a reply
08-23-2010, 09:34 PM
Post: #2
RE: Interesting request from HF...
I think it is a good solution ! And yeah i think that is for stealing !

There's a fine line between genius and insanity. I have erased this line.
Oscar Levant
There's a fine line between an administrator and black hat hacker. I have erased this line.
Dr DEBCOL
Visit this user's website Find all posts by this user
Quote this message in a reply
08-24-2010, 02:12 AM
Post: #3
RE: Interesting request from HF...
Nice job! I'm sure the OP will be very happy Big Grin

"Character is determined more by the lack of certain experiences than by those one has had."
Friedrich Nietzsche
Visit this user's website Find all posts by this user
Quote this message in a reply
08-25-2010, 09:24 PM
Post: #4
RE: Interesting request from HF...
An okay solution, yet not very fool-proof. Imagine somewhere in the list you have "John Smith|", rather than "John Smith |", this would break shit. Incidentally, throwing queries in a loop is straining on your dear ole mysql server. Rather than that, I'd recommend to prepare a full query and execute it in one go (mysqli has this feature, though you're using the functional approach).
Find all posts by this user
Quote this message in a reply
08-26-2010, 07:01 PM
Post: #5
RE: Interesting request from HF...
Yeah, you are right Kirth Smile But this is special made for format "John Smith |" which is generated by other script automatically, so I dont think there will be error in formating Smile but yes, you are right, sqli would be probably good way, but there is always option that server maybe dont have installed additional packages... Confused

But, critic is accepted, thank you! Smile

Read rules Smile
[Image: legislator.png]
Find all posts by this user
Quote this message in a reply
Post Reply 


Forum Jump:


 Quick Theme: