![]() |
![]() |
||||||
|
|||||||
| Tags: |
![]() |
|
|||
|
Hi All, i'm currently doing a CSV file upload into a database application, and
I'm wondering if it is possible to pass the data from the file field to another php file which executes the command of inserting the data of the csv file into the database? Thanks!! Wanqi |
| Sponsored Links |
|
|||
|
Sure, although it's definitely easier to do it on the same page.
You could write the file data into a cookie, or pass it with a URL variable. -- Murray --- ICQ 71997575 Adobe Community Expert (If you *MUST* email me, don't LAUGH when you do so!) ================== http://www.projectseven.com/go - DW FAQs, Tutorials & Resources http://www.dwfaq.com - DW FAQs, Tutorials & Resources ================== "taywanqi" <webforumsuser@macromedia.com> wrote in message news:gal940$sj0$1@forums.macromedia.com... > Hi All, i'm currently doing a CSV file upload into a database application, > and > I'm wondering if it is possible to pass the data from the file field to > another > php file which executes the command of inserting the data of the csv file > into > the database? Thanks!! > > Wanqi > |
|
|||
|
Sure, although it's definitely easier to do it on the same page.
You could write the file data into a cookie, or pass it with a URL variable. -- Murray --- ICQ 71997575 Adobe Community Expert (If you *MUST* email me, don't LAUGH when you do so!) ================== http://www.projectseven.com/go - DW FAQs, Tutorials & Resources http://www.dwfaq.com - DW FAQs, Tutorials & Resources ================== "taywanqi" <webforumsuser@macromedia.com> wrote in message news:gal940$sj0$1@forums.macromedia.com... > Hi All, i'm currently doing a CSV file upload into a database application, > and > I'm wondering if it is possible to pass the data from the file field to > another > php file which executes the command of inserting the data of the csv file > into > the database? Thanks!! > > Wanqi > |
|
|||
|
Hi Murray *ACE*,
First off, thanks a million for your reply. Currently my method of importing the csv file into the database is to upload the file to a "uploads" folder that i created, and then storing the file path in a variable before passing the variable to the LOAD DATA INFILE query, which would then import the file into the database. However, i keep getting this error: ERROR:File 'c:\wamp\bin\mysql\mysql5.0.45\data\test\$target_p ath' not found (Errcode: 2). I have tried specifying the path to the exact uploads folder, but i still keep getting the same error. Could you show me an example or give me some pointers on how i should go about importing the file into the database? For example, is it better to use cookies to store the file path or would it be better to use a URL variable? Also, since I am using a LOAD DATA INFILE query command, do i have to store everything to an "uploads" folder, or can i just get the file straight from the file path itself? Many thanks, and I look forward to hearing from you soon. Regards, Wanqi |
|
|||
|
Hi Murray *ACE*,
First off, thanks a million for your reply. Currently my method of importing the csv file into the database is to upload the file to a "uploads" folder that i created, and then storing the file path in a variable before passing the variable to the LOAD DATA INFILE query, which would then import the file into the database. However, i keep getting this error: ERROR:File 'c:\wamp\bin\mysql\mysql5.0.45\data\test\$target_p ath' not found (Errcode: 2). I have tried specifying the path to the exact uploads folder, but i still keep getting the same error. Could you show me an example or give me some pointers on how i should go about importing the file into the database? For example, is it better to use cookies to store the file path or would it be better to use a URL variable? Also, since I am using a LOAD DATA INFILE query command, do i have to store everything to an "uploads" folder, or can i just get the file straight from the file path itself? Many thanks, and I look forward to hearing from you soon. Regards, Wanqi |
|
|||
|
Can you show us the code block for this process, please?
Also, you could pass the file data in a session variable if security is an issue.... -- Murray --- ICQ 71997575 Adobe Community Expert (If you *MUST* email me, don't LAUGH when you do so!) ================== http://www.projectseven.com/go - DW FAQs, Tutorials & Resources http://www.dwfaq.com - DW FAQs, Tutorials & Resources ================== "taywanqi" <webforumsuser@macromedia.com> wrote in message news:galrvt$ia9$1@forums.macromedia.com... > Hi Murray *ACE*, > > First off, thanks a million for your reply. Currently my method of > importing > the csv file into the database is to upload the file to a "uploads" folder > that > i created, and then storing the file path in a variable before passing the > variable to the LOAD DATA INFILE query, which would then import the file > into > the database. However, i keep getting this error: > > ERROR:File 'c:\wamp\bin\mysql\mysql5.0.45\data\test\$target_p ath' not > found > (Errcode: 2). > > I have tried specifying the path to the exact uploads folder, but i still > keep > getting the same error. > > Could you show me an example or give me some pointers on how i should go > about > importing the file into the database? For example, is it better to use > cookies > to store the file path or would it be better to use a URL variable? Also, > since > I am using a LOAD DATA INFILE query command, do i have to store everything > to > an "uploads" folder, or can i just get the file straight from the file > path > itself? > > Many thanks, and I look forward to hearing from you soon. > > Regards, > Wanqi > |
|
|||
|
Can you show us the code block for this process, please?
Also, you could pass the file data in a session variable if security is an issue.... -- Murray --- ICQ 71997575 Adobe Community Expert (If you *MUST* email me, don't LAUGH when you do so!) ================== http://www.projectseven.com/go - DW FAQs, Tutorials & Resources http://www.dwfaq.com - DW FAQs, Tutorials & Resources ================== "taywanqi" <webforumsuser@macromedia.com> wrote in message news:galrvt$ia9$1@forums.macromedia.com... > Hi Murray *ACE*, > > First off, thanks a million for your reply. Currently my method of > importing > the csv file into the database is to upload the file to a "uploads" folder > that > i created, and then storing the file path in a variable before passing the > variable to the LOAD DATA INFILE query, which would then import the file > into > the database. However, i keep getting this error: > > ERROR:File 'c:\wamp\bin\mysql\mysql5.0.45\data\test\$target_p ath' not > found > (Errcode: 2). > > I have tried specifying the path to the exact uploads folder, but i still > keep > getting the same error. > > Could you show me an example or give me some pointers on how i should go > about > importing the file into the database? For example, is it better to use > cookies > to store the file path or would it be better to use a URL variable? Also, > since > I am using a LOAD DATA INFILE query command, do i have to store everything > to > an "uploads" folder, or can i just get the file straight from the file > path > itself? > > Many thanks, and I look forward to hearing from you soon. > > Regards, > Wanqi > |
|
|||
|
Attached below is the code i used for my application:
form.php <?php session_start(); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Untitled Document</title> <style type="text/css"> <!-- .style13 { color: #FFFFFF; font-family: Georgia, "Times New Roman", Times, serif; font-size: 14px; } --> </style> </head> <body> <form id="form1" name="form1" enctype="multipart/form-data" method="post" action="test.php"> <p align="center"> <input type="file" name="fileupload" id="fileupload" /> <input type="hidden" name="csvfile" id="csvfile"/> </p> <p align="center"> <input type="submit" name="upload" id="upload" value="Submit" /> </p> </form> </body> </html> Test.php <?php session_start(); echo $name; ob_start(); ?> <?php require_once('connection.php'); ?> <?php //UPLOADING FILE if(isset($_POST['upload'])) { $uploaddir = 'uploads'; $uploadfile = $uploaddir . basename($_FILES['fileupload']['name']); $target_path = $_GET['uploadfile']; echo $target_path; //$target_path = ini_get('$uploadfile'); echo $file; echo '<pre>'; if (move_uploaded_file($_FILES['fileupload']['tmp_name'], $uploadfile)) { echo "File is valid, and was successfully uploaded.\n"; } else { echo "Possible file upload attack!\n"; } echo 'Here is some more debugging info:'; print_r($_FILES); print "</pre>"; } $fcontents = $_SESSION[csvfile]; for($i=0; $i<sizeof($fcontents); $i++) { $line = trim($fcontents[$i]); $arr = explode("\t", $line); echo $arr."<br>\n"; } //ATTRIBUTES //$fieldseparator = ','; //$lineseparator = '\n'; //READING OF FILE $csvfile = $uploaddir . basename($_FILES['fileupload']['name']); //TRUNCATE THE FILE BEFORE INSERTING IT INTO THE DATABASE mysql_select_db($database, $test); mysql_query("TRUNCATE TABLE timetable") or die("MySQL Error: " . mysql_error()); //Delete the existing rows //IMPORT CSV INTO DATABASE $lines = 0; $queries = ""; $linearray = array(); $fcontents = file($csvfile); for($i=0; $i<sizeof($fcontents); $i++) { $lines++; $line = trim($fcontents[$i]); $linearray = explode(',',$line); $linemysql = implode("','",$linearray); // convert the array to a string echo "$linemysql". "\n"; //$query = "INSERT INTO timetable values('$linemysql')";//Insert query to insert values into the database $query = 'LOAD DATA INFILE "$target_path" REPLACE INTO TABLE timetable FIELDS TERMINATED BY "," OPTIONALLY ENCLOSED BY """" LINES TERMINATED BY "\r\n"'; mysql_query($query, $test) or die('SQL ERROR:'.mysql_error()); //Insert in the new values into the database } ?> </head> <body> </body> </html> <?php ob_flush(); ?> |
|
|||
|
Attached below is the code i used for my application:
form.php <?php session_start(); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Untitled Document</title> <style type="text/css"> <!-- .style13 { color: #FFFFFF; font-family: Georgia, "Times New Roman", Times, serif; font-size: 14px; } --> </style> </head> <body> <form id="form1" name="form1" enctype="multipart/form-data" method="post" action="test.php"> <p align="center"> <input type="file" name="fileupload" id="fileupload" /> <input type="hidden" name="csvfile" id="csvfile"/> </p> <p align="center"> <input type="submit" name="upload" id="upload" value="Submit" /> </p> </form> </body> </html> Test.php <?php session_start(); echo $name; ob_start(); ?> <?php require_once('connection.php'); ?> <?php //UPLOADING FILE if(isset($_POST['upload'])) { $uploaddir = 'uploads'; $uploadfile = $uploaddir . basename($_FILES['fileupload']['name']); $target_path = $_GET['uploadfile']; echo $target_path; //$target_path = ini_get('$uploadfile'); echo $file; echo '<pre>'; if (move_uploaded_file($_FILES['fileupload']['tmp_name'], $uploadfile)) { echo "File is valid, and was successfully uploaded.\n"; } else { echo "Possible file upload attack!\n"; } echo 'Here is some more debugging info:'; print_r($_FILES); print "</pre>"; } $fcontents = $_SESSION[csvfile]; for($i=0; $i<sizeof($fcontents); $i++) { $line = trim($fcontents[$i]); $arr = explode("\t", $line); echo $arr."<br>\n"; } //ATTRIBUTES //$fieldseparator = ','; //$lineseparator = '\n'; //READING OF FILE $csvfile = $uploaddir . basename($_FILES['fileupload']['name']); //TRUNCATE THE FILE BEFORE INSERTING IT INTO THE DATABASE mysql_select_db($database, $test); mysql_query("TRUNCATE TABLE timetable") or die("MySQL Error: " . mysql_error()); //Delete the existing rows //IMPORT CSV INTO DATABASE $lines = 0; $queries = ""; $linearray = array(); $fcontents = file($csvfile); for($i=0; $i<sizeof($fcontents); $i++) { $lines++; $line = trim($fcontents[$i]); $linearray = explode(',',$line); $linemysql = implode("','",$linearray); // convert the array to a string echo "$linemysql". "\n"; //$query = "INSERT INTO timetable values('$linemysql')";//Insert query to insert values into the database $query = 'LOAD DATA INFILE "$target_path" REPLACE INTO TABLE timetable FIELDS TERMINATED BY "," OPTIONALLY ENCLOSED BY """" LINES TERMINATED BY "\r\n"'; mysql_query($query, $test) or die('SQL ERROR:'.mysql_error()); //Insert in the new values into the database } ?> </head> <body> </body> </html> <?php ob_flush(); ?> |
|
|||
|
You have this in test.php -
$target_path = $_GET['uploadfile']; That implies that the page calling test.php is passing 'uploadfile' as a URL variable. But it isn't. <form id="form1" name="form1" enctype="multipart/form-data" method="post" action="test.php"> Why wouldn't $target_path be the same as $uploadfile? -- Murray --- ICQ 71997575 Adobe Community Expert (If you *MUST* email me, don't LAUGH when you do so!) ================== http://www.projectseven.com/go - DW FAQs, Tutorials & Resources http://www.dwfaq.com - DW FAQs, Tutorials & Resources ================== "taywanqi" <webforumsuser@macromedia.com> wrote in message news:gam4sp$scm$1@forums.macromedia.com... > Attached below is the code i used for my application: > > form.php > > > > > > > > <?php > session_start(); > ?> > <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" > "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> > <html xmlns="http://www.w3.org/1999/xhtml"> > <head> > <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> > <title>Untitled Document</title> > <style type="text/css"> > <!-- > .style13 { color: #FFFFFF; > font-family: Georgia, "Times New Roman", Times, serif; > font-size: 14px; > } > --> > </style> > </head> > > <body> > <form id="form1" name="form1" enctype="multipart/form-data" method="post" > action="test.php"> > <p align="center"> > <input type="file" name="fileupload" id="fileupload" /> > <input type="hidden" name="csvfile" id="csvfile"/> > > </p> > <p align="center"> > <input type="submit" name="upload" id="upload" value="Submit" /> > </p> > </form> > </body> > </html> > > > Test.php > > <?php > session_start(); > echo $name; > ob_start(); > ?> > > <?php require_once('connection.php'); ?> > > <?php > > //UPLOADING FILE > if(isset($_POST['upload'])) > { > > $uploaddir = 'uploads'; > > $uploadfile = $uploaddir . basename($_FILES['fileupload']['name']); > > $target_path = $_GET['uploadfile']; > > echo $target_path; > > //$target_path = ini_get('$uploadfile'); > > echo $file; > > echo '<pre>'; > if (move_uploaded_file($_FILES['fileupload']['tmp_name'], $uploadfile)) { > > echo "File is valid, and was successfully uploaded.\n"; > > } else { > echo "Possible file upload attack!\n"; > } > > echo 'Here is some more debugging info:'; > print_r($_FILES); > > print "</pre>"; > } > > > $fcontents = $_SESSION[csvfile]; > > for($i=0; $i<sizeof($fcontents); $i++) { > > $line = trim($fcontents[$i]); > $arr = explode("\t", $line); > > echo $arr."<br>\n"; > > > } > > //ATTRIBUTES > //$fieldseparator = ','; > //$lineseparator = '\n'; > > //READING OF FILE > $csvfile = $uploaddir . basename($_FILES['fileupload']['name']); > > //TRUNCATE THE FILE BEFORE INSERTING IT INTO THE DATABASE > mysql_select_db($database, $test); > mysql_query("TRUNCATE TABLE timetable") or die("MySQL Error: " . > mysql_error()); //Delete the existing rows > > //IMPORT CSV INTO DATABASE > $lines = 0; > $queries = ""; > $linearray = array(); > $fcontents = file($csvfile); > > for($i=0; $i<sizeof($fcontents); $i++) { > > $lines++; > > $line = trim($fcontents[$i]); > > $linearray = explode(',',$line); > > $linemysql = implode("','",$linearray); // convert the array to a string > > echo "$linemysql". "\n"; > > //$query = "INSERT INTO timetable values('$linemysql')";//Insert query to > insert values into the database > $query = 'LOAD DATA INFILE "$target_path" REPLACE > INTO TABLE timetable > FIELDS TERMINATED BY "," > OPTIONALLY ENCLOSED BY """" > LINES TERMINATED BY "\r\n"'; > > mysql_query($query, $test) or die('SQL ERROR:'.mysql_error()); //Insert > in > the new values into the database > > } > > ?> > > </head> > <body> > </body> > </html> > <?php ob_flush(); ?> > |
![]() |
| Thread Tools | |
| Display Modes | |
|
|
- Contact Us
-|-
Adobe Dreamweaver Forums -|-
Archive -|-
Top -|-Rules/Disclaimer-|-Help/Support-|-Advertise