...

View Full Version : Forms and PHP



PutterPlace
03-31-2007, 04:12 AM
I have a question, but I'm not sure if it would be considered simple or complex. What I'm trying to do is help users of a certain account management software.

What I want to do is have a "multipart" form on my web site. This form will allow users to select a text file from their computer. The text file being selected will be numerous lines of text in the following format:


firstemail@domain.com:password
secondemail@domain.net:password
thirdemail@domain.info:password

Within this particular script, I would like to change all colons ":" to semicolons ";". After that I would like to convert every line to hexadecimal. So the above file would have the following output:


6669727374656D61696C40646F6D61696E2E636F6D3B70617373776F7264
7365636F6E64656D61696C40646F6D61696E2E6E65743B70617373776F7264
7468697264656D61696C40646F6D61696E2E696E666F3B70617373776F7264

After this is done, a randomly named folder needs to be created within a specified directory on my web site. (It's important that this folder is not named the same as another folder that already exists) Then the output file will save itself as "scalemode.txt" within the aforementioned randomly named folder.

Any help would be greatly appreciated. Thanks in advance for any help you all provide.



UPDATE:

I have put together a little something that I would like to use to create the random directory:


<?php
$randnum = rand(1, 1000000000);
$other = time();
$dirname = "$randnum$other";
$old_umask = umask(0);
mkdir("/path/to/my/server/FOLDER/".$dirname, 0777);
?>

That's just what I'm using to create the random directory in which to save the file to. :) I still need help with the rest though.

mlseim
03-31-2007, 04:25 AM
This sounds like a homework assignment ... is it?

PutterPlace
03-31-2007, 04:33 AM
No. Actually, there's an automated account management software that it'll be used for. The software allows you to input an unlimited number of accounts, but they all have to be entered manually, and one at a time. This software then saves the inputted accounts into a text file in hexadecimal format, separating the account name (represented with the email address) and password with a semicolon.

This form will allow users to input their accounts from a text file, and overwrite the file that this software creates. It will help those who have a lot of accounts to manage.

PutterPlace
03-31-2007, 06:40 AM
UPDATE:

I have put together a little something that I would like to use to create the random directory:


<?php
$randnum = rand(1, 1000000000);
$other = time();
$dirname = "$randnum$other";
$old_umask = umask(0);
mkdir("/path/to/my/server/FOLDER/".$dirname, 0777);
?>

I'm still working on the script as a whole. That's just what I'm using to create the random directory in which to save the file to. :)

Alex!
03-31-2007, 04:03 PM
To change the colons to semi-colons you could use preg_replace:

http://uk.php.net/manual/en/function.preg-replace.php

And to convert the strings to hex you obviously haven't bothered searching google :/

http://www.google.co.uk/search?hl=en&safe=off&q=converting+to+php+string+to+hexadecimal&btnG=Search&meta=

There's a function on the first link which seems to do what you want.

Alex

PutterPlace
03-31-2007, 05:07 PM
Thanks very much for your help. I will definitely work on this some more when I get home, and post my progress here.

By the way, when I searched google, I wrote ascii to hexadecimal. I didn't think of actually replacing "ascii" with "string". :P Thanks again.



-PutterPlace

mlseim
03-31-2007, 05:48 PM
One question ...

Once you create those random directories, how will you know
which one contains the "scalemode.txt" file that you're looking for?

I guess the direction I'm going is ...

Instead of creating a randomly-named directory and putting
"scalemode.txt" into it, why not just name "scalemode.txt"
something random (example: "3U3tgy36.txt" ), and save them
all on one .htaccess protected directory?

That way, you could have one file in that protected directory,
called something like, "index.txt" that keeps track of which ones
are which, like a log (a list of the IP's that were responsible for
saving the file, the random file name, the save date, number of
emails in that file, a non-cryptic way of identifying it, etc).


.

PutterPlace
03-31-2007, 11:23 PM
I want it to create random directories so that the text file can be named scalemode.txt. This file must be named this way, and it's easier to jst have it already named like that for the users using this form. That way they don't have to type in the file name when they're saving it, or rename it later. :)

UPDATE: I fixed that problem by changing "text/plain" to "plain/text". Maybe I was wrong, but I thought, "text/plain" was correct. lol Anyways, now I'm fixing my script to replace all colons with semicolons. :) I'll post my progress

Anyways, here's what I have so far. The only thing I need to add is the ascii to hex thing, but I'm trying to fix another problem right now. The only problem I'm having right now is validating the type of the uploaded file. It's set to "text/plain" which is what a TXT file should be. However, when selecting a file of that type, it returns the error that I set, "Error: You must select an account list in .txt format.". I'm not sure why it's doing this. I tried setting it to image/gif, just to make sure that it would work with a different mime type. After selecting a GIF file, it worked just fine. Any ideas why I'm having this problem?

NancyJ
04-01-2007, 12:08 AM
To change the colons to semi-colons you could use preg_replace:

http://uk.php.net/manual/en/function.preg-replace.php

And to convert the strings to hex you obviously haven't bothered searching google :/

http://www.google.co.uk/search?hl=en&safe=off&q=converting+to+php+string+to+hexadecimal&btnG=Search&meta=

There's a function on the first link which seems to do what you want.

Alex
Why preg_replace? Its just a simple colon to semi-colon straight swap, why bother with regular expressions? A simple str_replace would do the job

for the OP, what your asking for is really quite simple but I'm not going to do it all for you ;) But heres a little pseudo-code/process flow to help you along the way - you've pretty much been given all the functions you need anyway



upload the file
create directory
create new file - scalemode.txt in directory (*function hint* fopen)
for each line of uploaded file (*function hint* file)
replace colons with semi colons (*function hint* str_replace)
convert to hex (see google results)
write line to new file (*function hint* fwrite)
close uploaded file (*function hint* fclose)

PutterPlace
04-01-2007, 12:39 AM
Below is what I have so far. Now I need a little more help. I've already put in fopen and fclose a few lines down from the part labeled "UPLOAD PROCESS" within basic.php (I wasn't sure what parameter should be used, so I just used "r" for now. Should I be using "w" since the file will be overwritten with the new data once it's ready?). I'm not completely sure how I would go about processing each line of the opened file. Any suggestions?

Thanks for everyone's help. :) I really do appreciate it.

index.php:

Select your account list below.<br><br>

<form method="post" enctype="multipart/form-data" action="basic.php">
<input type="file" name="userfile" size="27">
<br><br>
<input type="submit" value="Upload" name="upload">
</form>

config.php:

<?
$domain = "domain.com/FOLDER";
?>

basic.php:

<?
include ('config.php');
?>

<div class="text">
<div align="center">

<?
// CHECK IF FILE HAS BEEN SELECTED
if (!isset($_FILES['userfile']))
{
echo "<font color=\"white\" face=\"Geneva, Arial, Helvetica, sans-serif\">Error: You must select a file first.</font><br><br>\n";
echo "<center><a href=\"./index.php\">Back</a></center><br><br><br>";
echo "</div></div>";
exit;
}

if (is_uploaded_file($_FILES['userfile']['tmp_name'])) {

if (($_FILES['userfile']['type']=="plain/text"))
{

// CREATE RANDOM DIRECTORY
$randnum = rand(1, 1000000000);
$other = time();
$dirname = $randnum.$other;
$old_umask = umask(0);
mkdir("/path/to/my/server/FOLDER/".$dirname, 0777);

// UPLOAD PROCESS
$newfilename = "scalemode.txt";
$file = $_FILES['userfile']['name'];
$res = move_uploaded_file($_FILES['userfile']['tmp_name'], "./" . $dirname . "/" . $newfilename);

$openfile = fopen("./" . $dirname . "/scalemode.txt", "r");
fclose($openfile);

if (!$res)
{
echo "<font color=\"white\" face=\"Geneva, Arial, Helvetica, sans-serif\">Unknown Error: Please try again.</font><br><br>\n";
echo "<center><a href=\"./index.php\">Back</a></center><br><br><br>";
echo "</div></div>";
exit;
}

else
{
?>
<br>
<?

// SET URL VARIABLES
$urlf = 'http://' .$domain .'/' .$dirname . '/' . $newfilename;

?>
<script type="text/javascript">
<!--
function highlight(field)
{
field.focus();
field.select();
}
//-->
</script>
<table border="0" width="100%">
<tr><td class="imagehosting"><font color="white">DIRECT URL:</font></td><td><input type="text" name="thetext" onClick='highlight(this);' size="40" value="<? echo $urlf; ?>"></td></tr>
</table><table border='0' width='100%'><tr><td><center><font color="white"><a href='./index.php'>Back</a></font></td></tr></table><br><br>

<?
}
}

else
{
echo "<font color=\"white\" face=\"Geneva, Arial, Helvetica, sans-serif\">Error: You must select an account list in .txt format.</font><br><br>\n";
echo "<center><a href=\"./index.php\">Back</a></center><br><br><br>";
echo "</div></div>";
exit;
}

}
?>


</div>
</div>

PutterPlace
04-01-2007, 07:09 AM
I think I may have gotten something. Here is what I cam up with for the fopen/fclose from basic.php above.


$openfile = fopen("./" . $dirname . "/scalemode.txt", "r");
while (!feof($openfile))
{
$buffer = fgets($openfile);
$buffer_replace = str_replace(":", ";", $buffer);
echo $buffer_replace;
}
fclose($openfile);

I used the echo command to make sure that I'm doing it right, and the text shows up correctly. This processes each line and replaces colons with semicolons. Did I at least do that much correctly?

Also, when using fopen, which parameter should I be using for opening "scalemode.txt"? I want to read the file, then overwrite it with the new content.

Thanks again.

UPDATE: Is there an easier way of doing this so that I don't have to move the uploaded file directly to the random directory, and name it as "scalemode.txt"? For example:

In basic.php, I have it do this after the form is submitted:

// UPLOAD PROCESS
$newfilename = "scalemode.txt";
$file = $_FILES['userfile']['name'];
$res = move_uploaded_file($_FILES['userfile']['tmp_name'], "./" . $dirname . "/" . $newfilename);

How could I go about processing this file (replace all colons, and convert to hexadecimal), and then using fopen to create "scalemode.txt"? Or is there just a better way to set it up in general?

PutterPlace
04-01-2007, 09:57 PM
I found a way to process the file, and then save the processed file to scalemode.txt using fopen. Now I'm working on the ascii to hex conversion. :) I'll post back if I have anymore problems.

UPDATE: I've successfully finished this script. Thanks for all the help that everyone provided. This is DEFINITELY my favorite coding forum.

aedrin
04-02-2007, 03:15 PM
If this is your software you are creating a script for, then I think you need to fix the problem (function to create multiple users), not the symptom.

Is this legacy software? Font tags? I thought we ditched those when 2000 passed.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum