gunner_uk2000 Posted February 25, 2008 Share Posted February 25, 2008 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 More sharing options...
aschk Posted February 25, 2008 Share Posted February 25, 2008 Ok, so what's your error/problem? In answer to your question, yes you can have $_FILES[$var_here]. You don't need the double quotes because PHP will automatically make it a string'ed key association. Link to comment https://forums.phpfreaks.com/topic/92880-getting-items-out-of-the-_file-array/#findComment-475776 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.