Jump to content

CSV remote server upload


dotkpay

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/242800-csv-remote-server-upload/
Share on other sites

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(); }
}

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.