garydt Posted February 28, 2007 Share Posted February 28, 2007 I'm trying to upload an image file into a mysql database and i got the following code off the net but the image isn't going into the database. <?php require_once('Connections/elvisdb.php'); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Untitled Document</title> </head> <body> <?php mysql_select_db($database_elvisdb, $elvisdb); if (!$_POST['uploaded']){ //If nothing has been uploaded display the form ?> <form action="<? echo $_SERVER['PHP_SELF']; ?>" method="post" ENCTYPE="multipart/form-data"> Upload:<br><br> <input type="file" name="image"><br><br> <input type="hidden" name="uploaded" value="1"> <input type="submit" value="Upload"> </form> <? }else{ $ip=$REMOTE_ADDR; //don't continue if an image hasn't been uploaded if (!empty($image)){ //copy the image to directory copy($image, "C:/Program Files/xampp/htdocs/epeople/".$ip.""); //open the copied image, ready to encode into text to go into the database $filename1 = "C:/Program Files/xampp/htdocs/epeople/".$REMOTE_ADDR; $fp1 = fopen($filename1, "r"); //record the image contents into a variable $contents1 = fread($fp1, filesize($filename1)); //close the file fclose($fp1); //encode the image into text $encoded = chunk_split(base64_encode($contents1)); //insert information into the database mysql_query("INSERT INTO images (img,data)"."VALUES ('NULL', '$encoded')"); //delete the temporary file we made } //end } ?> </body> </html> Ideally i want to load images into the same record that contains other fields of data. Can you tell me if this is possible? Thanks Link to comment https://forums.phpfreaks.com/topic/40559-upload-image-into-database/ Share on other sites More sharing options...
simcoweb Posted February 28, 2007 Share Posted February 28, 2007 Personally I think that code is a bit off. For example, I don't see where $image variable's value is even set. Basically a file upload consists of using the $_FILES array that PHP creates when a file is uploaded. It consists of a couple of important things: $_FILES['uploadfile']['tmp_name']; which is the temporary name that is assigned to the uploaded file. As a default, the image is uploaded to a temporary folder and assigned the temporary name. and $_FILES['uploadedfile']['name']; which is the actual name of the file. There's also $_FILES['uploadedfile']['size']; and $_FILES['uploadedfile']['type']; as well so you can check against size restrictions and file types. Here's an image upload snippet I use that also validates the extension and the size that you can use as a model: <?php // Upload File $eg_success_File1 = false; if(!empty($_FILES['photo']['name'])) { // Check file is not larger than specified maximum size $eg_allowUpload = $_FILES['photo']['size'] <= 100000 ? true : false; // Check file is of the specified type if($eg_allowUpload) $eg_allowUpload = preg_match('/\\.(gif|jpg|jpeg|png)$/i', $_FILES['photo']['name']) ? true : false; if($eg_allowUpload) { if(is_uploaded_file($_FILES['photo']['tmp_name'])) { $eg_uploaddir = $_SERVER['DOCUMENT_ROOT']."/images/photo/"; $eg_uploadFile1 = $eg_uploaddir.rawurlencode($_FILES['photo']['name']); // Create a unique filename for the uploaded file $eg_i = 1; while (file_exists($eg_uploadFile1)) { $eg_separated_filename = explode(".",$eg_uploadFile1); if (substr($eg_separated_filename[0],-1) == $eg_i) { $eg_separated_filename[0] = substr($eg_separated_filename[0], 0, (strlen($eg_separated_filename[0])-1)); $eg_i++; } $eg_separated_filename[0] = $eg_separated_filename[0] . "$eg_i"; $eg_uploadFile1 = implode(".",$eg_separated_filename); } $eg_success_File1 = move_uploaded_file($_FILES['photo']['tmp_name'], $eg_uploadFile1); } } } ?> For the mysql insert i'd add the value like this: '".substr(strrchr($eg_uploadFile1, "/"), 1)."' Link to comment https://forums.phpfreaks.com/topic/40559-upload-image-into-database/#findComment-196322 Share on other sites More sharing options...
garydt Posted March 1, 2007 Author Share Posted March 1, 2007 Thanks very much for that but I'm confused. Where and how do I put the code for insert it into a mysql database. Also do i just write a normal upload form to go with it in html? Thanks again. Link to comment https://forums.phpfreaks.com/topic/40559-upload-image-into-database/#findComment-196755 Share on other sites More sharing options...
garydt Posted March 1, 2007 Author Share Posted March 1, 2007 Can somebody help me please? Link to comment https://forums.phpfreaks.com/topic/40559-upload-image-into-database/#findComment-197038 Share on other sites More sharing options...
boo_lolly Posted March 1, 2007 Share Posted March 1, 2007 if you are confused as to how to handle forms with php, have a look at this. then, look at file uploads in php here. you can also refer to the manual. Link to comment https://forums.phpfreaks.com/topic/40559-upload-image-into-database/#findComment-197052 Share on other sites More sharing options...
calabiyau Posted March 1, 2007 Share Posted March 1, 2007 You could try just storing the name of the file in the database instead of storing the file itself, and use the upload information above to upload the file to a specific directory. then when you need to recall the file again later for display or whatever, all you have to do is query your database for the name and do something like echo '<img src="'.$row['img_name'].'"/>'; where row is one you have just queried previously. Not sure if that method is appropriate for what you want to do , but that is how I do it. Link to comment https://forums.phpfreaks.com/topic/40559-upload-image-into-database/#findComment-197057 Share on other sites More sharing options...
boo_lolly Posted March 1, 2007 Share Posted March 1, 2007 You could try just storing the name of the file in the database instead of storing the file itself, and use the upload information above to upload the file to a specific directory. then when you need to recall the file again later for display or whatever, all you have to do is query your database for the name and do something like echo '<img src="'.$row['img_name'].'"/>'; where row is one you have just queried previously. Not sure if that method is appropriate for what you want to do , but that is how I do it. calab is right. it is pretty much standard procedure to store the file's information into a database. for instance, upon upload, you'd store the filename into one field, the path to the file into another field, the file size in another field, the file type in another field, and so on... then, to call the file, you do what calab said and do something like: <?php echo "<img src=\"". $row['file_path'] . $row['file_name'] ."\">\n"; ?> Link to comment https://forums.phpfreaks.com/topic/40559-upload-image-into-database/#findComment-197061 Share on other sites More sharing options...
simcoweb Posted March 1, 2007 Share Posted March 1, 2007 You need an HTML form like the following: <html> <head> <title>Upload form</title> </head> <body> <form enctype="multipart/form-data" action="uploader.php" method="POST"> <input type="hidden" name="MAX_FILE_SIZE" value="100000" /> Choose a file to upload: <input name="uploadedfile" type="file" /><br /> <input type="submit" value="Upload File" /> </form> </body> </html> Link to comment https://forums.phpfreaks.com/topic/40559-upload-image-into-database/#findComment-197069 Share on other sites More sharing options...
garydt Posted March 1, 2007 Author Share Posted March 1, 2007 Thanks for that. I'll give it a go. Link to comment https://forums.phpfreaks.com/topic/40559-upload-image-into-database/#findComment-197136 Share on other sites More sharing options...
garydt Posted March 2, 2007 Author Share Posted March 2, 2007 I've been trying this and I'm getting error Parse error: parse error, unexpected '<' in C:\Program Files\xampp\htdocs\epeople\upload.php on line 47 The code- <?php require_once('Connections/elvisdb.php'); ?> <?php // Upload File $eg_success_File1 = false; if(!empty($_FILES['photo']['name'])) { // Check file is not larger than specified maximum size $eg_allowUpload = $_FILES['photo']['size'] <= 100000 ? true : false; // Check file is of the specified type if($eg_allowUpload) $eg_allowUpload = preg_match('/\\.(gif|jpg|jpeg|png)$/i', $_FILES['photo']['name']) ? true : false; if($eg_allowUpload) { if(is_uploaded_file($_FILES['photo']['tmp_name'])) { $eg_uploaddir = $_SERVER['DOCUMENT_ROOT']."/images/photo/"; $eg_uploadFile1 = $eg_uploaddir.rawurlencode($_FILES['photo']['name']); // Create a unique filename for the uploaded file $eg_i = 1; while (file_exists($eg_uploadFile1)) { $eg_separated_filename = explode(".",$eg_uploadFile1); if (substr($eg_separated_filename[0],-1) == $eg_i) { $eg_separated_filename[0] = substr($eg_separated_filename[0], 0, (strlen($eg_separated_filename[0])-1)); $eg_i++; } $eg_separated_filename[0] = $eg_separated_filename[0] . "$eg_i"; $eg_uploadFile1 = implode(".",$eg_separated_filename); } $eg_success_File1 = move_uploaded_file($_FILES['photo']['tmp_name'], $eg_uploadFile1); } } } $insertSQL = sprintf("INSERT INTO images (imageName) VALUES ('".substr(strrchr($eg_uploadFile1, "/"), 1)."'); mysql_select_db($database_elvisdb, $elvisdb); $Result1 = mysql_query($insertSQL, $elvisdb) or die(mysql_error()) ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Untitled Document</title> </head> <body> <form enctype="multipart/form-data" action="uploader.php" method="POST"> <input type="hidden" name="MAX_FILE_SIZE" value="100000" /> Choose a file to upload: <input name="photo" type="file" /><br /> <input type="submit" value="Upload File" /> </form> </body> </html> Have i written the code right to insert the image name variable into the database? I'm not sure which directory it's storing the actual image. Sorry to keep asking questions. Link to comment https://forums.phpfreaks.com/topic/40559-upload-image-into-database/#findComment-197816 Share on other sites More sharing options...
itsmeArry Posted March 2, 2007 Share Posted March 2, 2007 you are missing a ; here $Result1 = mysql_query($insertSQL, $elvisdb) or die(mysql_error()) use this $Result1 = mysql_query($insertSQL, $elvisdb) or die(mysql_error()); Link to comment https://forums.phpfreaks.com/topic/40559-upload-image-into-database/#findComment-197818 Share on other sites More sharing options...
garydt Posted March 2, 2007 Author Share Posted March 2, 2007 I did that and i'm still getting the same error. Any ideas? Link to comment https://forums.phpfreaks.com/topic/40559-upload-image-into-database/#findComment-197832 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.