Jump to content

Possible Bug in search_array


ylkien

Recommended Posts

Hi guys,

 

I'm trying to create a normal site to upload stuff, and delete the file (which is up to the user). The code below is just a snippet of the code.

 

The problem that I encounter, is that, if I click the "delete" hyperlink more than once, the "array_search" goes crazy, and returns a value which is more than the array size.

 

Thanks!

 

<?php
error_reporting(E_ALL); //remove later

session_start();

//include 'Protected/Functions.php';		//This is where all of the functions are stored!
//include 'Protected/Database.php';			//This is where the database connections and variables are stored
$UPLOAD_TEMP = "Upload_Temp";
$UPLOAD_PERMANENT = "Files";


/*
if( !empty( $_REQUEST($FILE_UPLOAD_DISPLAY_HTML) ) )
$FILE_UPLOAD_DISPLAY_HTML = $_REQUEST($FILE_UPLOAD_DISPLAY_HTML);
else
$FILE_UPLOAD_DISPLAY_HTML = array();
*/	
if( isset( $_SESSION['Uploaded_Files'] ) || !session_is_registered('Uploaded_Files') )
{
$Uploaded_Files = array();
session_register("Uploaded_Files");
}

if( isset( $_SESSION['Uploaded_Files_Number'] ) || !session_is_registered('Uploaded_Files_Number') )
{
$Uploaded_Files = 0;
session_register("Uploaded_Files_Number");
}


if( isset( $_SESSION['Error'] ) || !session_is_registered('Error') )
{
$Error = 0;
session_register("Error");
}

/*
if( !$_SESSION['Auth'] || !$_SESSION['User'] )
{
echo "This is a private site. Please login to proceed...<BR>";
//header("Location: http://$_SERVER[sERVER_ADDR]/$LOGIN_PAGE");
session_destroy();
echo "<meta http-equiv=\"refresh\" content=\"3;URL=http://" . $_SERVER['SERVER_ADDR'] . "/" . $LOGIN_PAGE . "\">";
exit;
}
*/

// Upload files	********************************************************************************************************

//This function does the actual work of saving the file into the $Target_Path

function Save_Uploaded_File( $UPLOAD_TEMP )
{

$File_Upload_Error = array(
				"There is no error, the file uploaded with success",
				"The uploaded file exceeds the upload_max_filesize directive in php.ini",
				"The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form",
				"The uploaded file was only partially uploaded",
				"No file was uploaded",
				"Missing a temporary folder"
				);

$Target_Path = $_SERVER['DOCUMENT_ROOT'] . "/" . $UPLOAD_TEMP . "/";

$Target_Path = $Target_Path . basename( $_FILES['Picture']['name']); 

if( move_uploaded_file($_FILES['Picture']['tmp_name'], $Target_Path) )
{
	//echo "<BR>The file ".  basename( $_FILES['Picture']['name']). " has been uploaded";		//to be removed
	$_SESSION['Uploaded_Files'][$_SESSION['Uploaded_Files_Number']] = basename( $_FILES['Picture']['name'] );
} 
else
{	
	$_SESSION['Error'] .= "There was an error uploading the file!" . $File_Upload_Error[$_FILES['Picture']['error']] . ". Please try again<BR>";
}

$_SESSION['Uploaded_Files_Number']++;

}

//Checks whether the 'file submit' button is selected
if( empty($_SESSION['Error']) && isset($_REQUEST['_submit_check']) )
{

if( empty($_FILES['Picture']['name'] ) )
	$_SESSION['Error'] = "No file was selected to be uploaded";

//check if file has already been uploaded into the drive, and if so, will write to the $_SESSION['Error']
if( !empty($_FILES['Picture']['name']) && !empty($_SESSION['Uploaded_Files']) && empty($_SESSION['Error']) )
{
	foreach( $_SESSION['Uploaded_Files'] as $Temp_Array )
	{
		if( $Temp_Array == basename( $_FILES['Picture']['name'] ) )
		{	
			$_SESSION['Error'] .= "This file, <i>" . $Temp_Array . "</i>, has already been uploaded, and can't be uploaded again. <BR>";
			break;
		}

	}
}


//Does the actual action of uploading the file. Calls the function "$Save_Uploaded_File"
if( empty($_SESSION['Error']) ) // && ($_FILES['Picture']['name'] == $_SESSION['Uploaded_Files'][$_SESSION['Uploaded_Files_Number']]) )
{
	Save_Uploaded_File( $UPLOAD_TEMP );
}

}
//This deletes the file in the upload temp folder, if the user chooses so.
else if( empty($_SESSION['Error']) && isset( $_REQUEST['Action']) )
{	
if( empty($_REQUEST['File'] ) || $_REQUEST['Action'] != "Delete" )
	$_SESSION['Error'] = "No file selected to be deleted";
else if( !file_exists( $UPLOAD_TEMP . "/" . $_REQUEST['File'] ) )
{
	echo array_search( $_REQUEST['File'], $_SESSION['Uploaded_Files'] );
	$_SESSION['Error'] = "File not found.";
	array_splice( $_SESSION['Uploaded_Files'], array_search( $_REQUEST['File'], $_SESSION['Uploaded_Files'] ), 1 );
}
else
{
	echo "<BR>" . count( $_SESSION['Uploaded_Files']) . " - " . $_REQUEST['File'] . "<BR>";
	reset( $_SESSION['Uploaded_Files']) ;
	echo "<BR>Array Before array_search and array_slice<BR><BR>";
	$i = 0;		//to be removed later
	foreach( $_SESSION['Uploaded_Files'] as $Temp_Array )
	{
		echo $Temp_Array . $i . " has been uploaded <FONT>[<A HREF=\"" . $_SERVER['PHP_SELF'] . "?Action=Delete&File=" . $Temp_Array . "\">delete</A>]</FONT><BR>" ;
		$i++;
	}

	echo "Array Search found <B>" . $_REQUEST['File'] . "</B> at location <B>" . array_search( $_REQUEST['File'], $_SESSION['Uploaded_Files'] ) . "</B>";
	array_splice( $_SESSION['Uploaded_Files'], array_search( $_REQUEST['File'], $_SESSION['Uploaded_Files'] ), 1 );
	//echo array_search( $_REQUEST['File'], $_SESSION['Uploaded_Files'] );
	//array_splice( $_SESSION['Uploaded_Files'], array_search( $_REQUEST['File'], $_SESSION['Uploaded_Files'] ), 1 );
	unlink( $UPLOAD_TEMP . "/" . $_REQUEST['File'] );
}

}

$main_table = "

<FORM ENCTYPE=\"multipart/form-data\" ACTION=\"" . $_SERVER['PHP_SELF'] . "\" METHOD=POST >
<TABLE ALIGN=LEFT BORDER=0  CELLPADDING=2>
	<INPUT TYPE=hidden NAME=\"MAX_FILE_SIZE\" VALUE=\"10000000\">
	<TR><TD>Upload your files here (jpeg, doc, zip, etc)		</TD><TD>: <INPUT NAME=\"Picture\" SIZE=50 TYPE=file><br></TD></TR>

	<input type=hidden name=\"_submit_check\" value=\"UPLOAD1\"/> 
	<TR><TD><INPUT TYPE=submit VALUE=\"Upload File\"></TD></TR>
</FORM>
</TABLE>
		";

?>



<HTML>
<TITLE>File Upload</TITLE>
<BODY>

<P>

<?php

if( $_SESSION['Error'] )
echo "<FONT COLOR=RED><B>" . $_SESSION['Error'] . "</B></FONT>";
$_SESSION['Error'] = NULL;


echo $main_table;
//To display the files uploaded. Will add "delete" function in future!

echo "<BR CLEAR=LEFT><BR>";

//this is to display the files that has been uploaded previously In future, will add a 'delete' link here
if( !empty($_SESSION['Uploaded_Files']) )
{	
$i = 0;		//to be removed later
foreach( $_SESSION['Uploaded_Files'] as $Temp_Array )
{
	echo $Temp_Array . $i . " has been uploaded <FONT>[<A HREF=\"" . $_SERVER['PHP_SELF'] . "?Action=Delete&File=" . $Temp_Array . "\">delete</A>]</FONT><BR>" ;
	$i++;
}
}

?>

</P>

</BODY>
</HTML>

Link to comment
Share on other sites

Can you put the following code in:

 

		print "<pre>";var_dump($_SESSION['Uploaded_Files']);print"</pre>";
	echo array_search( $_REQUEST['File'], $_SESSION['Uploaded_Files'] );

 

to verify that the array is what you think it should be?  I'm assuming that's the call to array_search() you are interested in.

Link to comment
Share on other sites

Hi,

 

I have added it in already. The main part of the code that creates this problem is :

 

else

{

echo "<BR>Array has " . count( $_SESSION['Uploaded_Files']) . " values. File to be deleted is " . $_REQUEST['File'] . "<BR>";

reset( $_SESSION['Uploaded_Files']) ;

print "<pre>";var_dump($_SESSION['Uploaded_Files']);print"</pre>";

 

/*

echo "<BR>Array Before array_search and array_slice<BR><BR>";

$i = 0; //to be removed later

foreach( $_SESSION['Uploaded_Files'] as $Temp_Array )

{

echo $Temp_Array . $i . " has been uploaded <FONT>[<A HREF=\"" . $_SERVER['PHP_SELF'] . "?Action=Delete&File=" . $Temp_Array . "\">delete</A>]</FONT><BR>" ;

$i++;

}

*/

 

echo "<BR>Array_Search returned value : " . array_search( $_REQUEST['File'], $_SESSION['Uploaded_Files'] );

echo "<BR>Array Search found <B>" . $_REQUEST['File'] . "</B> at location <B>" . array_search( $_REQUEST['File'], $_SESSION['Uploaded_Files'] ) . "</B>";

array_splice( $_SESSION['Uploaded_Files'], array_search( $_REQUEST['File'], $_SESSION['Uploaded_Files'] ), 1 );

//echo array_search( $_REQUEST['File'], $_SESSION['Uploaded_Files'] );

//array_splice( $_SESSION['Uploaded_Files'], array_search( $_REQUEST['File'], $_SESSION['Uploaded_Files'] ), 1 );

unlink( $UPLOAD_TEMP . "/" . $_REQUEST['File'] );

 

 

 

<?php
error_reporting(E_ALL); //remove later

session_start();

//include 'Protected/Functions.php';		//This is where all of the functions are stored!
//include 'Protected/Database.php';			//This is where the database connections and variables are stored
$UPLOAD_TEMP = "Upload_Temp";
$UPLOAD_PERMANENT = "Files";


/*
if( !empty( $_REQUEST($FILE_UPLOAD_DISPLAY_HTML) ) )
$FILE_UPLOAD_DISPLAY_HTML = $_REQUEST($FILE_UPLOAD_DISPLAY_HTML);
else
$FILE_UPLOAD_DISPLAY_HTML = array();
*/	
if( isset( $_SESSION['Uploaded_Files'] ) || !session_is_registered('Uploaded_Files') )
{
$Uploaded_Files = array();
session_register("Uploaded_Files");
}

if( isset( $_SESSION['Uploaded_Files_Number'] ) || !session_is_registered('Uploaded_Files_Number') )
{
$Uploaded_Files = 0;
session_register("Uploaded_Files_Number");
}


if( isset( $_SESSION['Error'] ) || !session_is_registered('Error') )
{
$Error = 0;
session_register("Error");
}

/*
if( !$_SESSION['Auth'] || !$_SESSION['User'] )
{
echo "This is a private site. Please login to proceed...<BR>";
//header("Location: http://$_SERVER[sERVER_ADDR]/$LOGIN_PAGE");
session_destroy();
echo "<meta http-equiv=\"refresh\" content=\"3;URL=http://" . $_SERVER['SERVER_ADDR'] . "/" . $LOGIN_PAGE . "\">";
exit;
}
*/

// Upload files	********************************************************************************************************

//This function does the actual work of saving the file into the $Target_Path

function Save_Uploaded_File( $UPLOAD_TEMP )
{

$File_Upload_Error = array(
				"There is no error, the file uploaded with success",
				"The uploaded file exceeds the upload_max_filesize directive in php.ini",
				"The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form",
				"The uploaded file was only partially uploaded",
				"No file was uploaded",
				"Missing a temporary folder"
				);

$Target_Path = $_SERVER['DOCUMENT_ROOT'] . "/" . $UPLOAD_TEMP . "/";

$Target_Path = $Target_Path . basename( $_FILES['Picture']['name']); 

if( move_uploaded_file($_FILES['Picture']['tmp_name'], $Target_Path) )
{
	//echo "<BR>The file ".  basename( $_FILES['Picture']['name']). " has been uploaded";		//to be removed
	$_SESSION['Uploaded_Files'][$_SESSION['Uploaded_Files_Number']] = basename( $_FILES['Picture']['name'] );
} 
else
{	
	$_SESSION['Error'] .= "There was an error uploading the file!" . $File_Upload_Error[$_FILES['Picture']['error']] . ". Please try again<BR>";
}

$_SESSION['Uploaded_Files_Number']++;

}

//Checks whether the 'file submit' button is selected
if( empty($_SESSION['Error']) && isset($_REQUEST['_submit_check']) )
{

if( empty($_FILES['Picture']['name'] ) )
	$_SESSION['Error'] = "No file was selected to be uploaded";

//check if file has already been uploaded into the drive, and if so, will write to the $_SESSION['Error']
if( !empty($_FILES['Picture']['name']) && !empty($_SESSION['Uploaded_Files']) && empty($_SESSION['Error']) )
{
	foreach( $_SESSION['Uploaded_Files'] as $Temp_Array )
	{
		if( $Temp_Array == basename( $_FILES['Picture']['name'] ) )
		{	
			$_SESSION['Error'] .= "This file, <i>" . $Temp_Array . "</i>, has already been uploaded, and can't be uploaded again. <BR>";
			break;
		}

	}
}


//Does the actual action of uploading the file. Calls the function "$Save_Uploaded_File"
if( empty($_SESSION['Error']) ) // && ($_FILES['Picture']['name'] == $_SESSION['Uploaded_Files'][$_SESSION['Uploaded_Files_Number']]) )
{
	Save_Uploaded_File( $UPLOAD_TEMP );
}

}
//This deletes the file in the upload temp folder, if the user chooses so.
else if( empty($_SESSION['Error']) && isset( $_REQUEST['Action']) )
{	
if( empty($_REQUEST['File'] ) || $_REQUEST['Action'] != "Delete" )
	$_SESSION['Error'] = "No file selected to be deleted";
else if( !file_exists( $UPLOAD_TEMP . "/" . $_REQUEST['File'] ) )
{
	echo "<BR>Array_Search value when 'file not found' is : " . array_search( $_REQUEST['File'], $_SESSION['Uploaded_Files'] );
	$_SESSION['Error'] = "File not found.";
	array_splice( $_SESSION['Uploaded_Files'], array_search( $_REQUEST['File'], $_SESSION['Uploaded_Files'] ), 1 );
}
else
{
	echo "<BR>Array has " . count( $_SESSION['Uploaded_Files']) . " values. File to be deleted is " . $_REQUEST['File'] . "<BR>";
	reset( $_SESSION['Uploaded_Files']) ;
	print "<pre>";var_dump($_SESSION['Uploaded_Files']);print"</pre>";

	/*
	echo "<BR>Array Before array_search and array_slice<BR><BR>";
	$i = 0;		//to be removed later
	foreach( $_SESSION['Uploaded_Files'] as $Temp_Array )
	{
		echo $Temp_Array . $i . " has been uploaded <FONT>[<A HREF=\"" . $_SERVER['PHP_SELF'] . "?Action=Delete&File=" . $Temp_Array . "\">delete</A>]</FONT><BR>" ;
		$i++;
	}
	*/

	echo "<BR>Array_Search returned value : " . array_search( $_REQUEST['File'], $_SESSION['Uploaded_Files'] );
	echo "<BR>Array Search found <B>" . $_REQUEST['File'] . "</B> at location <B>" . array_search( $_REQUEST['File'], $_SESSION['Uploaded_Files'] ) . "</B>";
	array_splice( $_SESSION['Uploaded_Files'], array_search( $_REQUEST['File'], $_SESSION['Uploaded_Files'] ), 1 );
	//echo array_search( $_REQUEST['File'], $_SESSION['Uploaded_Files'] );
	//array_splice( $_SESSION['Uploaded_Files'], array_search( $_REQUEST['File'], $_SESSION['Uploaded_Files'] ), 1 );
	unlink( $UPLOAD_TEMP . "/" . $_REQUEST['File'] );
}

}

$main_table = "

<FORM ENCTYPE=\"multipart/form-data\" ACTION=\"" . $_SERVER['PHP_SELF'] . "\" METHOD=POST >
<TABLE ALIGN=LEFT BORDER=0  CELLPADDING=2>
	<INPUT TYPE=hidden NAME=\"MAX_FILE_SIZE\" VALUE=\"10000000\">
	<TR><TD>Upload your files here (jpeg, doc, zip, etc)		</TD><TD>: <INPUT NAME=\"Picture\" SIZE=50 TYPE=file><br></TD></TR>

	<input type=hidden name=\"_submit_check\" value=\"UPLOAD1\"/> 
	<TR><TD><INPUT TYPE=submit VALUE=\"Upload File\"></TD></TR>
</FORM>
</TABLE>
		";

?>



<HTML>
<TITLE>File Upload</TITLE>
<BODY>

<P>

<?php

if( $_SESSION['Error'] )
echo "<FONT COLOR=RED><B>" . $_SESSION['Error'] . "</B></FONT>";
$_SESSION['Error'] = NULL;


echo $main_table;
//To display the files uploaded. Will add "delete" function in future!

echo "<BR CLEAR=LEFT><BR>";

//this is to display the files that has been uploaded previously In future, will add a 'delete' link here
if( !empty($_SESSION['Uploaded_Files']) )
{	
$i = 0;		//to be removed later
foreach( $_SESSION['Uploaded_Files'] as $Temp_Array )
{
	echo $Temp_Array . $i . " has been uploaded <FONT>[<A HREF=\"" . $_SERVER['PHP_SELF'] . "?Action=Delete&File=" . $Temp_Array . "\">delete</A>]</FONT><BR>" ;
	$i++;
}
}

?>

</P>

</BODY>
</HTML>

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.