Jump to content

Getting Items out of the $_File Array....


gunner_uk2000

Recommended Posts

I'm trying to put images into a database, and I have the following code which will work if I just have it accessing the $_File array directly:

 

I'm trying to make it into a function, where it can be passed the name of the file form item and it will put it into the database, as I would like to be able to use this code in other forms.

 

I'm usually a Java/C# programmer, and the php arrays are really quite confusing.

 

What I would like to do is access the array by programtically putting a value between the ' ' which surrounds the key, ether that or get a reference to the of the file array.

 

So instead of isset($_FILES['fileImage1']) I can have $_FILES["$aFileName"], where it would get the value for the key (which is the value in the $aFileName vairable.

 

Here is my code:

 

$result1 = ValidateAndInsertImage($itemNo, "fileImage1");
$result2 = ValidateAndInsertImage($itemNo, "fileImage2");
$result3 = ValidateAndInsertImage($itemNo, "fileImage3");
$result4 = ValidateAndInsertImage($itemNo, "fileImage4");

 

 

function ValidateAndInsertImage($itemNo, $image){

if(!isset($_FILES['fileImage1'])) {
        	echo '<p>Please select a file</p>';
    	}
    	else
        {
        	try {
            	upload($itemNo, $image);
            	// give praise and thanks to the php gods
           	 	echo '<p>Thank you for submitting</p>';
        	}
        	catch(Exception $e) {
            	echo $e->getMessage();
            	echo 'Sorry, could not upload file';
       	 	}
    	}	

}

      function upload($itemNo, $file){

	if(is_uploaded_file($_FILES["$file"]['tmp_name'])) {

		// check the file is less than the maximum file size
		if($_FILES["$file"]['size'] < $maxsize)
			{
		// prepare the image for insertion
		$imgData =addslashes (file_get_contents($_FILES["$file"]['tmp_name']));
		// $imgData = addslashes($_FILES['userfile']);

		// get the image info..
		  $size = getimagesize($_FILES["$file"]['tmp_name']);

		// put the image in the db...
		  // database connection
		  mysql_connect("localhost", "$username", "$password") OR DIE (mysql_error());

		  // select the db
		  mysql_select_db ("$dbname") OR DIE ("Unable to select db".mysql_error());

		// our sql query
		$sql = "INSERT INTO image
				(itemNo, image, imageSize)
				VALUES
				('$itemNo', '{$imgData}', '{$size[3]}' )";


		// insert the image
		if(!mysql_query($sql)) {
			echo 'Unable to upload file';
			}
		}
	}
	else {
		 // if the file is not less than the maximum allowed, print an error
		 echo
		  '<div>File exceeds the Maximum File limit</div>
		  <div>Maximum File limit is '.$maxsize.'</div>
		  <div>File '.$_FILES['userfile']['name'].' is '.$_FILES['userfile']['size'].' bytes</div>
		  <hr />';
		 }
    }

EDITED BY WILDTEEN88: Please wrap code within code tags (


) when posting code.

Link to comment
https://forums.phpfreaks.com/topic/92880-getting-items-out-of-the-_file-array/
Share on other sites

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.