ecko04 Posted January 28, 2009 Share Posted January 28, 2009 So i'm a php n00b and working on a script here. My first problem is, i'm trying to read a txt file into an array but the file I am trying to read is not in the same directory as the php file but the only way I can get it to work is to have the file in the same directory as the php file. Here is an example of what I have: $fcontents = file("./Applications.txt"); The file I want it to read is not in that directory though but the only way I can get it to work is to drop it in that directory, if someone can give me the correct syntax I would be grateful. My next problem is trying to take a user selected option in an html file and use it in my php file. For example I have something like this for my html file: <form method=post action="http://xxxxx/upload.php"> <hr> <p> Select the file delimiter<br> <select name=delimiter> <option value="," selected>Comma <option value="\t" selected>Tab <option value=";" selected>Semi-Colon </select><br><br> <form enctype="multipart/form-data" action="http://xxxxxx/forms.php" method="post"> <input type="hidden" name="MAX_FILE_SIZE" value="1000"> Send this file: <input name="userfile" type="file"> <input type="submit" value="Send File"> </form> </form> Notice I am trying to have my user upload a file as well as specify the file delimiter. I don't know if this is the correct syntax to do both things but I am trying. Now on to the PHP $delimiter = $_POST['delimiter']; echo $delimiter; for($i=1; $i<sizeof($fcontents); $i++) { $line = trim($fcontents[$i]); $arr = explode("\t", $line); #if your data is comma separate # instead of tab separated, # change the '\t' above to ',' $sql = "insert into Application values ('". implode("','", $arr) ."')"; mysql_query($sql); echo $sql ."<br>\n"; if(mysql_error()) { echo mysql_error() ."<br>\n"; Notice initially I am trying to take the info that the user selected from the drop down menu and read it into an array called $delimiter Next I believe I would want to change this particular line in the code: $arr = explode("\t", $line); To read: $arr = explode($delimiter, $line); I believe what that would do is no matter what the user selects as the delimiter it would put their selection there and parse the file accordingly. Then the next segment would write the output to a mysql database. At least that is what I am understanding it should do. If someone would please help a n00b out I would be greatly appreciative. Quote Link to comment https://forums.phpfreaks.com/topic/142713-solved-php-n00b-has-a-few-questions/ Share on other sites More sharing options...
MadTechie Posted January 28, 2009 Share Posted January 28, 2009 Okay where is the text file in relation to the php file ? ie index.php <--file to open text file +---Data +---TextFiles +---Applications.txt <---File to open code $fcontents = file("Data/TextFiles/Applications.txt"); Next.. the forms your closing the first form after the 2nd form this is a no no also they are going to different php files.. with only 1 submit. also a no no.. so let join these first you can include the upload.php in the forms.php if you wish (it may work) depends on the coding.. <form enctype="multipart/form-data" action="http://xxxxxx/forms.php" method="post"> <hr> <p> Select the file delimiter<br> <select name=delimiter> <option value="," selected>Comma <option value="\t" selected>Tab <option value=";" selected>Semi-Colon </select><br><br> <input type="hidden" name="MAX_FILE_SIZE" value="1000"> Send this file: <input name="userfile" type="file"> <input type="submit" value="Send File"> </form> php code you posted looks okay (your need to test) Next Sounds okay.. you seam to have a good idea what your plaing to do (tests required) Any problems please post back Quote Link to comment https://forums.phpfreaks.com/topic/142713-solved-php-n00b-has-a-few-questions/#findComment-748098 Share on other sites More sharing options...
ecko04 Posted January 28, 2009 Author Share Posted January 28, 2009 Thanks for the speedy response!! So you're saying include the upload.php in the forms.php I'm not quite sure how that would look Here is my upload.php. It was working earlier but now it keeps defaulting to my else statement <html><head><title>PHP Test</title></head> <body> Upload started.<br> <?php if (is_uploaded_file($_FILES['userfile']['tmp_name'])) { move_uploaded_file($_FILES['userfile']['tmp_name'], "DUMMY.txt"); print "The file " . $_FILES['userfile']['name'] . " has been uploaded.<br>"; } else { print "Possible file upload attack. Filename: " . $_FILES['userfile']['tmp_name']; } ?> </body></html> Quote Link to comment https://forums.phpfreaks.com/topic/142713-solved-php-n00b-has-a-few-questions/#findComment-748111 Share on other sites More sharing options...
ecko04 Posted January 28, 2009 Author Share Posted January 28, 2009 On a good note my upload.php decided it wants to work again now Quote Link to comment https://forums.phpfreaks.com/topic/142713-solved-php-n00b-has-a-few-questions/#findComment-748116 Share on other sites More sharing options...
MadTechie Posted January 28, 2009 Share Posted January 28, 2009 you could just add, the following code (from the upload.php) to the forms.php <?php if (is_uploaded_file($_FILES['userfile']['tmp_name'])) { move_uploaded_file($_FILES['userfile']['tmp_name'], "DUMMY.txt"); print "The file " . $_FILES['userfile']['name'] . " has been uploaded.<br>"; } else { print "Possible file upload attack. Filename: " . $_FILES['userfile']['tmp_name']; } ?> basically if you need to have 2 forms they will post their OWN data to wherever you point them but you can't point 1 form to 2 places Hows the reading the text file comming along ? Quote Link to comment https://forums.phpfreaks.com/topic/142713-solved-php-n00b-has-a-few-questions/#findComment-748121 Share on other sites More sharing options...
ecko04 Posted January 28, 2009 Author Share Posted January 28, 2009 Hows the reading the text file comming along ? This may the one spot where I am having a few difficulties. For example, the file uploads successfully. The variable for the delimiter is successfully saved. Now here is the full code from my forms.php <?php include 'upload.php'; /* connect */ $link = mysql_connect('xxxxxxxxx', 'USER', 'PASS') or die('Could not connect: ' . mysql_error()); mysql_select_db('DB_NAME', $link) or die('Could not select database'); mysql_query("DROP TABLE Appication"); mysql_query("CREATE TABLE Application (Name VARCHAR(255), Genre VARCHAR(255), Price VARCHAR(10), PRIMARY KEY (Name, Genre))"); $fcontents = file("/DUMMY.txt"); # expects the csv file to be in the same dir as this script $delimiter = $_POST['delimiter']; for($i=1; $i<sizeof($fcontents); $i++) { $line = trim($fcontents[$i]); $arr = explode($delimiter, $line); #if your data is comma separate # instead of tab separated, # change the '\t' above to ',' $sql = "insert into Application values ('". implode("','", $arr) ."')"; mysql_query($sql); echo $sql ."<br>\n"; if(mysql_error()) { echo mysql_error() ."<br>\n"; } } ?> Now, currently the file being uploaded is uploaded to the same directory as the PHP file. The code successfully creates the table correctly. From that point it seems to die as no data is entered into the table. I know the $delimiter is correct because if I echo it I get the result that I selected when I uploaded the file. Quote Link to comment https://forums.phpfreaks.com/topic/142713-solved-php-n00b-has-a-few-questions/#findComment-748125 Share on other sites More sharing options...
ecko04 Posted January 28, 2009 Author Share Posted January 28, 2009 I figured that out, I had that slash in there where it shouldn't have been. i.e. $fcontents = file("/DUMMY.txt"); <-----WRONG $fcontents = file("DUMMY.txt"); <------DING DING....WE HAVE A WINNER Quote Link to comment https://forums.phpfreaks.com/topic/142713-solved-php-n00b-has-a-few-questions/#findComment-748129 Share on other sites More sharing options...
MadTechie Posted January 28, 2009 Share Posted January 28, 2009 Great. Next problem (if any) ? Quote Link to comment https://forums.phpfreaks.com/topic/142713-solved-php-n00b-has-a-few-questions/#findComment-748131 Share on other sites More sharing options...
ecko04 Posted January 28, 2009 Author Share Posted January 28, 2009 Now, I fix one thing and create a different problem now on the MySQL side of things.... So MySQL is checking with the $delimiter array to see that the user selected It should be either a comma, semi-colon or tab When I change the value from $delimiter to '\t' it enters the data correctly into the database When I change back to $delimiter and select tab from when I upload the file I am getting a MySQL database stating Column count doesn't match value count at row 1 There is a syntax error going on somewhere Quote Link to comment https://forums.phpfreaks.com/topic/142713-solved-php-n00b-has-a-few-questions/#findComment-748135 Share on other sites More sharing options...
ecko04 Posted January 28, 2009 Author Share Posted January 28, 2009 When I echo the delimiter I get \\t Somewhere I have an extra slash ??? This only happens for the tab The comma and semi-colon echo correctly Quote Link to comment https://forums.phpfreaks.com/topic/142713-solved-php-n00b-has-a-few-questions/#findComment-748140 Share on other sites More sharing options...
MadTechie Posted January 28, 2009 Share Posted January 28, 2009 Ahhhhhh Magic Quote are on.. if you can turn them off in the PHP.INI file.. if not try this change $delimiter = $_POST['delimiter']; to if(get_magic_quotes_gpc()) { $delimiter = stripslashes($_POST['delimiter']); } else { $delimiter = $_POST['delimiter']; } Quote Link to comment https://forums.phpfreaks.com/topic/142713-solved-php-n00b-has-a-few-questions/#findComment-748146 Share on other sites More sharing options...
ecko04 Posted January 28, 2009 Author Share Posted January 28, 2009 That definitely got rid of that 2nd slash so not it echoes \t but it till is giving me some error about how Column count doesn't match value count at row 1 Quote Link to comment https://forums.phpfreaks.com/topic/142713-solved-php-n00b-has-a-few-questions/#findComment-748148 Share on other sites More sharing options...
MadTechie Posted January 28, 2009 Share Posted January 28, 2009 One thing i should point out is that if the data being imported has a single quote (') this is messing up the insert.. Try this $arr = array_map("mysql_real_escape_string",$arr); //<--Add $sql = "insert into Application values ('".implode("','", $arr) ."')"; //tidy mysql_query($sql) or die("$sql<br>".mysql_error()); //updated Quote Link to comment https://forums.phpfreaks.com/topic/142713-solved-php-n00b-has-a-few-questions/#findComment-748149 Share on other sites More sharing options...
ecko04 Posted January 28, 2009 Author Share Posted January 28, 2009 Sorry, I never even mentioned that part The data to be inserted looks like Gears of War Fiction 60 There are about 15 entries similar to that. These are tab delimited. When I added that last part $arr = array_map("mysql_real_escape_string",$arr); I am getting the error: insert into Application values ('Gears of War Fiction 60') Column count doesn't match value count at row 1 I have a death grip on my hair, the little that I do have,.....so close Quote Link to comment https://forums.phpfreaks.com/topic/142713-solved-php-n00b-has-a-few-questions/#findComment-748151 Share on other sites More sharing options...
MadTechie Posted January 28, 2009 Share Posted January 28, 2009 okay well it seams $arr = explode($delimiter, $line); isn't working so i'll assume its a $delimiter problem to test try $arr = explode($delimiter, $line); //existing code var_dump($arr); die("Wait"); Quote Link to comment https://forums.phpfreaks.com/topic/142713-solved-php-n00b-has-a-few-questions/#findComment-748156 Share on other sites More sharing options...
ecko04 Posted January 28, 2009 Author Share Posted January 28, 2009 Upload started. The file Application.txt has been uploaded. array(1) { [0]=> string(23) "Gears of War Fiction 60" } Wait Quote Link to comment https://forums.phpfreaks.com/topic/142713-solved-php-n00b-has-a-few-questions/#findComment-748159 Share on other sites More sharing options...
MadTechie Posted January 28, 2009 Share Posted January 28, 2009 Yeah one item.. not creating 3 as expected can you post a sample of the text file.. attach it or paste into a code tag Quote Link to comment https://forums.phpfreaks.com/topic/142713-solved-php-n00b-has-a-few-questions/#findComment-748161 Share on other sites More sharing options...
ecko04 Posted January 28, 2009 Author Share Posted January 28, 2009 I have tried using two different files, one being applications and one using an old class roster NAME GENRE PRICE Gears of War Fiction 60 NBA Live 09 Sports 60 Mortal Kombat vs DC Universe Fiction 60 Namco Museum Classic 30 Grand Theft Auto Fiction 40 Rock Band II Musical-Interactive 130 Fallout 3 Fiction 60 Call of Duty: World at War Historic 60 Halo 3 Fiction 60 Major League Basebal 2K9 Sports 60 STUDENT NAME Email Course Richard O user@testdomain.net ENG_7120 Curtis Clifford user@testdomain.net ENG_7120 Shanee Terese user@testdomain.net ENG_7120 Wanda user@testdomain.net ENG_7120 Jian user@testdomain.net ENG_7120 Michael G user@testdomain.net ENG_7120 Jeffrey Michael user@testdomain.net ENG_7120 Jaeho user@testdomain.net ENG_7120 Yueqin user@testdomain.net ENG_7120 Jerome L user@testdomain.net ENG_7120 Balapuwaduge J P user@testdomain.net ENG_7120 Justus Nyamweya Juser@testdomain.net ENG_7120 Uday Sankar user@testdomain.net ENG_7120 Gregory Demetrius user@testdomain.net ENG_7120 Damodar Purushottam user@testdomain.net ENG_7120 Kevin Bernard user@testdomain.net ENG_7120 Prathap user@testdomain.net ENG_7120 Yun user@testdomain.net ENG_7120 Yixian user@testdomain.net ENG_7120 Candice Harris user@testdomain.net ENG_7126 Karyn Lynn user@testdomain.net ENG_7126 Sheldon Oscar user@testdomain.net ENG_7126 Bobby R user@testdomain.net ENG_7126 Quote Link to comment https://forums.phpfreaks.com/topic/142713-solved-php-n00b-has-a-few-questions/#findComment-748172 Share on other sites More sharing options...
MadTechie Posted January 28, 2009 Share Posted January 28, 2009 Okay they are not tabs.. but 3 spaces add an extra option to the form <option value=" " selected>Triple Space Quote Link to comment https://forums.phpfreaks.com/topic/142713-solved-php-n00b-has-a-few-questions/#findComment-748175 Share on other sites More sharing options...
ecko04 Posted January 28, 2009 Author Share Posted January 28, 2009 I'm pretty sure they are tabbed but I added the new option to the form and I am gettings Upload started. The file WTF.txt has been uploaded. array(1) { [0]=> string(38) "Richard O user@testdomain.net ENG_7120" } Wait and Upload started. The file Applications.txt has been uploaded. array(1) { [0]=> string(23) "Gears of War Fiction 60" } Wait Quote Link to comment https://forums.phpfreaks.com/topic/142713-solved-php-n00b-has-a-few-questions/#findComment-748177 Share on other sites More sharing options...
MadTechie Posted January 28, 2009 Share Posted January 28, 2009 Can you attach one of the files so i can check, this form say they are 3 spaces but it could be be the parse to attach (click Additional Options...) bottom left than browse etc Quote Link to comment https://forums.phpfreaks.com/topic/142713-solved-php-n00b-has-a-few-questions/#findComment-748180 Share on other sites More sharing options...
ecko04 Posted January 28, 2009 Author Share Posted January 28, 2009 Can you attach one of the files so i can check, this form say they are 3 spaces but it could be be the parse to attach (click Additional Options...) bottom left than browse etc Sure [attachment deleted by admin] Quote Link to comment https://forums.phpfreaks.com/topic/142713-solved-php-n00b-has-a-few-questions/#findComment-748182 Share on other sites More sharing options...
MadTechie Posted January 28, 2009 Share Posted January 28, 2009 Okay you win.. its a tab Not sure why explode isn't working!! try $arr = explode("$delimiter", $line); if that fails use preg_split, that should work! $arr = preg_split("/$delimiter/", $line); Quote Link to comment https://forums.phpfreaks.com/topic/142713-solved-php-n00b-has-a-few-questions/#findComment-748187 Share on other sites More sharing options...
ecko04 Posted January 28, 2009 Author Share Posted January 28, 2009 Thanks!! The first option didn't work but the 2nd did! Last question and I think I may be done harassing you; how can we make it so that it will read and work for comma, tab as well as semi-colon depending on what the user selects? Right now it will read a tab delimited file but what about if we want to read the same file but at the comma or something like that? Quote Link to comment https://forums.phpfreaks.com/topic/142713-solved-php-n00b-has-a-few-questions/#findComment-748195 Share on other sites More sharing options...
MadTechie Posted January 28, 2009 Share Posted January 28, 2009 Same code, the only thing that would change is the $delimiter which is picked up from the form . Quote Link to comment https://forums.phpfreaks.com/topic/142713-solved-php-n00b-has-a-few-questions/#findComment-748208 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.