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