dotkpay Posted July 25, 2011 Share Posted July 25, 2011 Hello, I have a script that imports data from csv to mysql with this form header <form action="importer.php" method="post" enctype="multipart/form-data"> The importer.php works fine on localhost but it cannot upload data from csv to mysql once its hosted on a remote server. In otherwords it only seems to work when the csv file is on the same computer as importer.php Could someone please show me how to upload csv data to a remote mysql database. Thanks in advance Quote Link to comment https://forums.phpfreaks.com/topic/242800-csv-remote-server-upload/ Share on other sites More sharing options...
teynon Posted July 25, 2011 Share Posted July 25, 2011 Can we see importer.php? Is it uploading the file to the database or the file to a folder on your server? If it's a folder, it's most likely a chmod problem. You need to add permissions to the upload directory. Quote Link to comment https://forums.phpfreaks.com/topic/242800-csv-remote-server-upload/#findComment-1247027 Share on other sites More sharing options...
dotkpay Posted July 26, 2011 Author Share Posted July 26, 2011 Below is importer.php, it alwats manages to echo the success message but no data is saved in the database <?php require("connect.php"); // Database Connector class check { function error() { header("location:index.php?error=Invalid"); // Form validation function. Redirects back to page with error in url if criteria is not met exit(); } } $obj = new check(); if(!isset($_POST['submit'])){ $obj->error(); } if($_FILES["ifile"]["error"]>0){ $obj->error(); } else{ $fname = $_FILES["ifile"]["name"]; $chk_ext = explode(".",$fname); if(count($chk_ext)!=2){ $obj->error(); } else { if(strtolower($chk_ext[1]) == "csv" || strtolower($chk_ext[1]) == "txt") // Imports either csv or txt files { $filename = $_FILES["ifile"]["tmp_name"]; $handle = fopen($filename, "r"); while(($pc = fgetcsv($handle, 1000, ",")) !== FALSE) { $id = $pc[0]; $name = $pc[1]; $phone = $pc[2]; $id = mysql_real_escape_string(stripslashes($id)); $name = mysql_real_escape_string(stripslashes($name)); $phone = mysql_real_escape_string(stripslashes($phone)); mysql_query("INSERT INTO profiles (id, name, phone) VALUES('$id','$name','$phone')"); } } fclose($handle); { echo "Data was successfully imported"; // This message is always echoed by the remote server but no data is saved in the database } } else{ $obj->error(); } } Quote Link to comment https://forums.phpfreaks.com/topic/242800-csv-remote-server-upload/#findComment-1247124 Share on other sites More sharing options...
teynon Posted July 26, 2011 Share Posted July 26, 2011 There are a lot of problems with your code. 1) You are looping your mysql query. You can insert multiple rows in one query like this: INSERT INTO profiles (id, name, phone) VALUES ('ID', 'NAME', 'PHONE'), ('ID', 'NAME', 'PHONE'), ('ID', 'NAME', 'PHONE') 2) This makes no sense: fclose($handle); { echo "Data was successfully imported"; // This message is always echoed by the remote server but no data is saved in the database } 3) Try something like this: <?php class check { function error() { header("location:index.php?error=Invalid"); // Form validation function. Redirects back to page with error in url if criteria is not met exit(); } } $obj = new check(); if(!isset($_POST['submit'])){ $obj->error(); } if($_FILES["ifile"]["error"]>0){ $obj->error(); } else{ $fname = $_FILES["ifile"]["name"]; $chk_ext = explode(".",$fname); if(count($chk_ext)!=2){ $obj->error(); } else { if(strtolower($chk_ext[1]) == "csv" || strtolower($chk_ext[1]) == "txt") // Imports either csv or txt files { $filename = $_FILES["ifile"]["tmp_name"]; $handle = fopen($filename, "r"); while(($pc = fgetcsv($handle, 1000, ",")) !== FALSE) { $id = $pc[0]; $name = $pc[1]; $phone = $pc[2]; $id = mysql_real_escape_string(stripslashes($id)); $name = mysql_real_escape_string(stripslashes($name)); $phone = mysql_real_escape_string(stripslashes($phone)); if (mysql_query("INSERT INTO profiles (id, name, phone) VALUES('$id','$name','$phone')")) { echo "Data was successfully imported"; // This message is always echoed by the remote server but no data is saved in the database } } } } fclose($handle); } ?> It's still bad, but I don't have time to rework all of your code. Quote Link to comment https://forums.phpfreaks.com/topic/242800-csv-remote-server-upload/#findComment-1247245 Share on other sites More sharing options...
dotkpay Posted July 26, 2011 Author Share Posted July 26, 2011 Am really stuck on this issue. Could this be something to do with permissions since importer.php has to create a temporary location for the csv data before it is finally imported into sql? Quote Link to comment https://forums.phpfreaks.com/topic/242800-csv-remote-server-upload/#findComment-1247410 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.