Link to home
Start Free TrialLog in
Avatar of Robert Teetzel
Robert Teetzel

asked on

We are tring to gather data from all our rental equipment and send it back to our server

Currently we have many rental equipment out in the world and each one has a MySQL database that gathers data.  We have a small program to dump the data back to the server each hour so we can bill the customer based on use.  Currently we use Telus stick to allow the equipment to transmit data to our server and that works fine until 24 hours goes by then they block some of the ports that stop us from connecting to the server database.  We then have the turn off the router and start all over again.  Since we can not do this all the time we though to use a web program to copy the data through that port which is always open.  We are just testing this right now but  We have a php program that we will place on each machine to create a xml file from the database then copy the xml file to the server and check when done then if successful delete the data in the database.  The php program to create the xml file works great but the file transfer is where we are having issues.  Our networker doesn't want to create a ftp on the Lennox server so we wrote this code

WebUpdate.html
<!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>Administration - Upload new Files</title>
</head>

<body>
<h1>Upload new news files</h1>
<form enctype="multipart/form-data" action="upload.php" method="post">
<input type="hidden" name="MAX_FILE_SIZE" value="1000" />
upload this file: <input name="userfile" type="file" />
<input type="submit" value="Send File"
</form>
</body>
</html>

Open in new window


This program then calls upload.php
<!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>Uploading...</title>
</head>

<body>
<h1>Uploading file...</h1>
<?php
if (!isset($_SESSION))
{
	session_start();	  
}
	
if ($userfile=="none") 
{
	echo "Problem: not file uploaded";
	exit;
}

if ($userfile_size==0)
{
	echo "Problem: upload file is zero length";
	exit;
}

if ($userfile_type != "text/plain")
{
	echo "Problem: file is not plain text";
	exit;
}

if (!is_uploaded_file($userfile))
{
	echo "Problem: possible file upload attack";
	exit;
}

$upfile = some path.$userfile_name;
if (!copy($userfile, $upfile))
{
	echo "Problem: Could not move file into directory";
	exit;
}

echo "File upload successfully<br><br>";
$fp = open($upfile. "r");
$contents = fread ($fp, filesize ($upfile));
fclose($fp);

$contents = strip_tags($contents);
$fp = fopen($upfile, "w");
fwrite($fp, $contents);
fclose($ftp);

echo "Preview of upload file contents:<br><hr>";
echo $contents;
echo "<br><hr>";

?>
</body>
</html>
<?php
//This function is from the php manual.
// is_upload_file is built into php4.0.3.
// Prior to that, we can use this code.

function is_upload_file($filename) {
	if (!$tmp_file = get_cfg_var('upload_tmp_dir')) {
		$tmp_file = dirname(tempnam('', ''));
	}
	$tmp_file .= '/'.basename($filename);
	/* User might have traiing slash in php.ini.....*/
	return (ereg_replace('/+','/',$tmp_file) == $filename);
}
?>

Open in new window


The php book says this should work but nothing is getting passed to the upload.php program any help or suggestions would be greatly appreciated.
Avatar of Dave Baldwin
Dave Baldwin
Flag of United States of America image

I don't see that you are using sessions anywhere but session_start()  Must go at the top of the file before any content is output.  It simply will not work as you have written it.  It also should never be put in a 'conditional' statement that may interfere with it's operation.

Furthermore, your book is probably too old.  It looks like your code is depending on Register Globals which went from ON to OFF in PHP » 4.2.0 and were removed as of PHP 5.4.0.  http://php.net/manual/en/security.globals.php   Here is the current page about file uploads: http://php.net/manual/en/features.file-upload.php .
ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Robert Teetzel
Robert Teetzel

ASKER

I copy down your code and gave it a try and it worked great with only a few changes to the code.  Thank you for your help