Jump to content

need help debugging this PHP script


natronp

Recommended Posts

I'm getting errors saying that there's a timeout on line 10 :

 

$lines = file($url_path.'/admin/lists/'.$new_file_name);

 

this is where we try to access the file that a user has uploaded.  I confirmed that the file is in fact uploaded and in the proper directory and has also been chmoded to 0777.  For some reason, i get the "failed to open stream: Connection timed out" error here and then, obviously, the rest of the code (the foreach loop) fails as well.

 

Not sure where to turn next for troubleshooting.  I'm assuming it's server related, but i only know enough php to be dangerous and wanted to see if anyone saw anything that could be a red flag...

 

here's the script in it's entirety... thanks for any feedback!

<? include $_SERVER['DOCUMENT_ROOT']."/includes/config.inc";?>
<? 
if ($action =='upload_members') {
$new_file_name = time() .'_'. basename($_FILES['member_list']['name']);
$uploadfile = $site_root .'/admin/lists/'.$new_file_name;
move_uploaded_file($_FILES['member_list']['tmp_name'], $uploadfile);
chmod($uploadfile, 0777);
$clear_table = mysql_query("truncate table accounts");

$lines = file($url_path.'/admin/lists/'.$new_file_name);
foreach($lines as $key=>$val) {
	//echo "$key -> $val ++<BR><BR>";
	$accountArray = explode(",", $val);
	$arraySize = sizeof($accountArray);
	//echo "$key -> $val -> $arraySize +++<BR>";
	$accountNumber = $accountArray[0];
	if ($arraySize == 2) $accountName = $accountArray[1];
	else {
		array_shift($accountArray);
		$accountName = join(",", $accountArray);
	}
	$accountInsert = "INSERT INTO accounts SET accountNumber = '$accountNumber', accountCompany = '".addslashes($accountName)."'";
	//echo "$accountInsert ++<BR><BR>";
	$accountResult = mysql_query($accountInsert);
}
echo "You list has been uploaded.";

}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> File upload </TITLE>
<link href="/css/styles.css" rel="stylesheet" type="text/css">
<link href="/css/stylesAdmin.css" rel="stylesheet" type="text/css">
</HEAD>
<BODY>
<div style="padding:10px">
Upload the export  as type .CSV here.<br />
Make sure the file only contains two columns<br />
Account Number and Account Name
<form method="POST" action='<?=$PHP_SELF;?> ' enctype="multipart/form-data">
<input type='file' name='member_list'><br>
<input type='submit' name='submit' value='upload'>
<input type='hidden' name='action' value='upload_members'>
</form>
</div>
</BODY>
</HTML>

 

 

Link to comment
Share on other sites

What is the contents of $url_path?  It sounds to me like you're trying to download the file via HTTP even though it might be on your local machine.  If that is the case, there may be some sort of DNS issue keeping you from accessing your own site locally via its URL and causing the timeout.  If you're trying to access a local file, you should never get a timeout error (unless maybe your hard drive is going bad).

Link to comment
Share on other sites

I see several issues:

 

1. The variable $url_path is not defined. Although it may be defined in the include file at the top - why are you using that it access the file? You used $uploadfile to copy the file to a new location/name, why not use that variable to access it? Assuming $url_path is a url it makes no sense to try and access the file via the web when you already have it in your accesible directory structure.

 

Plus, unless you have a bullet proof method of ensuring the user cannot upload any type of malicious content you are asking for bad things to happen by uploading user files to a web accessible directory.

 

I would change yur approach, but for now, try using $uploadfile on the line in error.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.