jeppers Posted May 19, 2007 Share Posted May 19, 2007 i only know how to upload the images names manually, i want to automate it so when an image is uploaded to a file directory it will automatically insert the data about the image to the database. i hope this is simple to do because i just have 2 days to fin this project off so allot of pressure i just hope some one can help thanks Quote Link to comment https://forums.phpfreaks.com/topic/52111-solved-i-have-created-a-upload-function-but-how-do-i-upload-the-file-name-to-a-database/ Share on other sites More sharing options...
jeppers Posted May 19, 2007 Author Share Posted May 19, 2007 this code uploads and this is where i want to upload the information about the images to the database. <?php if (! @include('mysql\conn_mysql.inc.php')) { echo 'Sorry, page unavailable'; exit; } // define a constant for the maximum upload size define ('MAX_FILE_SIZE', 51200); if (array_key_exists('upload', $_POST)) { // define constant for upload folder define('UPLOAD_DIR', 'C:/test/images/'); // replace any spaces in original filename with underscores // at the same time, assign to a simpler variable $file = str_replace(' ', '_', $_FILES['image']['name']); // convert the maximum size to KB $max = number_format(MAX_FILE_SIZE/1024, 1).'KB'; // create an array of permitted MIME types $permitted = array('image/gif', 'image/jpeg', 'image/pjpeg', 'image/png'); // begin by assuming the file is unacceptable $sizeOK = false; $typeOK = false; // check that file is within the permitted size if ($_FILES['image']['size'] > 0 && $_FILES['image']['size'] <= MAX_FILE_SIZE) { $sizeOK = true; } // check that file is of an permitted MIME type foreach ($permitted as $type) { if ($type == $_FILES['image']['type']) { $typeOK = true; break; } } if ($sizeOK && $typeOK) { switch($_FILES['image']['error']) { case 0: include('includes/create_thumb.php'); break; case 3: $result = "Error uploading $file. Please try again."; default: $result = "System error uploading $file. Contact webmaster."; } } elseif ($_FILES['image']['error'] == 4) { $result = 'No file selected'; } else { $result = "$file cannot be uploaded. Maximum size: $max. Acceptable file types: gif, jpg, png."; } } ?><!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>File upload</title> </head> <body> <?php // if the form has been submitted, display result if (isset($result)) { echo "<p><strong>$result</strong></p>"; } ?> <form action="" method="post" enctype="multipart/form-data" name="uploadImage" id="uploadImage"> <p> <label for="image">Upload image:</label> <input type="hidden" name="MAX_FILE_SIZE" value="<?php echo MAX_FILE_SIZE; ?>" /> <input type="file" name="image" id="image" /> </p> <p> <input type="submit" name="upload" id="upload" value="Upload" /> </p> </form> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/52111-solved-i-have-created-a-upload-function-but-how-do-i-upload-the-file-name-to-a-database/#findComment-256971 Share on other sites More sharing options...
chronister Posted May 19, 2007 Share Posted May 19, 2007 Does this code actually move the file from the temp location to your actual directory where you want the files to go? I am not seeing a move_uploaded_file line in the code. That is generally what actually moves the file because php uses a temp location by default. Here is a script I use for uploading, moving and adding file information to the database. Take a look at the comments and hopefully this should give you an idea on how to adapt this to your code. <?php if($_FILES['file_upload']['tmp_name']) { $file=$_FILES['file_upload']['tmp_name']; if(is_uploaded_file($file)) { $name=$_FILES['file_upload']['name']; // get the real name of the file $size=fsize_convert($_FILES['file_upload']['size']); // file size convert function results // the next 2 lines define where the file is to be moved to with file name $uploaddir = $_SERVER['DOCUMENT_ROOT']."/members/docs/"; $uploaddir.=$name; if(move_uploaded_file($_FILES['file_upload']['tmp_name'], $uploaddir)) { // if the file was successfully moved, // set the message, and get time stamp. $message='<b>'.$name .'</b> Was Successfully Uploaded'; $time=time(); connectdb(); // connect to the database $query="INSERT INTO docs (title ,description ,filename ,filesize ,level,modified) VALUES ('$title', '$description', '$name', '$size', '$level','$time')"; $result=mysql_query($query); // run insert query } else { $message="There was a problem when uploading the new file"; } } ?> Hope this helps. Nate Quote Link to comment https://forums.phpfreaks.com/topic/52111-solved-i-have-created-a-upload-function-but-how-do-i-upload-the-file-name-to-a-database/#findComment-256986 Share on other sites More sharing options...
jeppers Posted May 19, 2007 Author Share Posted May 19, 2007 <?php define('THUMBS_DIR', 'c:/test/images/thumbs/'); define('MAX_WIDTH', 200); define('MAX_HEIGHT', 150); // process the uploaded image if (is_uploaded_file($_FILES['image']['tmp_name'])) { $original = $_FILES['image']['tmp_name']; // begin by getting the details of the origin<a href="create_thumb.php">Create thumbnail image</a>al list($width, $height, $type) = getimagesize($original); // calculate the scaling ratio if ($width <= MAX_WIDTH && $height <= MAX_HEIGHT) { $ratio = 1; } elseif ($width > $height) { $ratio = MAX_WIDTH/$width; } else { $ratio = MAX_HEIGHT/$height; } // strip the extension off the image filename $imagetypes = array('/\.gif$/', '/\.jpg$/', '/\.jpeg$/', '/\.png$/'); $name = preg_replace($imagetypes, '', basename($_FILES['image']['name'])); // move the temporary file to the upload folder $moved = move_uploaded_file($original, UPLOAD_DIR.$_FILES['image']['name']); if ($moved) { $result = $_FILES['image']['name'].' successfully uploaded; '; $original = UPLOAD_DIR.$_FILES['image']['name']; } else { $result = 'Problem uploading '.$_FILES['image']['name'].'; '; } // create an image resource for the original switch($type) { case 1: $source = @ imagecreatefromgif($original); if (!$source) { $result = 'Cannot process GIF files. Please use JPEG or PNG.'; } break; case 2: $source = imagecreatefromjpeg($original); break; case 3: $source = imagecreatefrompng($original); break; default: $source = NULL; $result = 'Cannot identify file type.'; } // make sure the image resource is OK if (!$source) { $result = 'Problem copying original'; } else { // calculate the dimensions of the thumbnail $thumb_width = round($width * $ratio); $thumb_height = round($height * $ratio); // create an image resource for the thumbnail $thumb = imagecreatetruecolor($thumb_width, $thumb_height); // create the resized copy imagecopyresampled($thumb, $source, 0, 0, 0, 0, $thumb_width, $thumb_height, $width, $height); // save the resized copy switch($type) { case 1: if (function_exists('imagegif')) { $success = imagegif($thumb, THUMBS_DIR.$name.'_thb.gif'); $thumb_name = $name.'_thb.gif'; } else { $success = imagejpeg($thumb, THUMBS_DIR.$name.'_thb.jpg', 50); $thumb_name = $name.'_thb.jpg'; } break; case 2: $success = imagejpeg($thumb, THUMBS_DIR.$name.'_thb.jpg', 100); $thumb_name = $name.'_thb.jpg'; break; case 3: $success = imagepng($thumb, THUMBS_DIR.$name.'_thb.png'); $thumb_name = $name.'_thb.png'; } if ($success) { $result .= "$thumb_name created"; mysql_query($sql = "INSERT into images SET filename='".$_FILES['image']['name']."'") or die(mysql_error(). ": ".$sql ); } else { $result .= 'Problem creating thumbnail'; } // remove the image resources from memory imagedestroy($source); imagedestroy($thumb); } } ?> this the code that moves into the directory and i am able to update to the database now.... it was because i did not have an auto increment in there and i was not calling the database.. the next problem is that now that it updates for some reason the image dose not show as a thumb nail just text saying thumb.... (i think its because of the size of of the image but cant be sure) also if the same image when uploaded twice it will add it to the database twice is there something i can do to stop that... thanks for the help Quote Link to comment https://forums.phpfreaks.com/topic/52111-solved-i-have-created-a-upload-function-but-how-do-i-upload-the-file-name-to-a-database/#findComment-257074 Share on other sites More sharing options...
chronister Posted May 19, 2007 Share Posted May 19, 2007 also if the same image when uploaded twice it will add it to the database twice is there something i can do to stop that... The 2 images would have to have the same name. You can run a query before inserting it to see if the name of the file already exists in the database. If you have 2 identical images and their names are different, then I don't think there is any way to analyze the images themselves and determine that it already exists. Can I assume that the alt tag for the image is thumb? If so then you may want to check that the path the database is reporting is the correct path to find that particular image. Echo the path and hit the address bar and see if you can pull up the image that way. I would say that chances are it is either a path issue, or you may have forgot to include the $ in the variable, so that instead of $thumb, you have thumb. Hope this helps. Quote Link to comment https://forums.phpfreaks.com/topic/52111-solved-i-have-created-a-upload-function-but-how-do-i-upload-the-file-name-to-a-database/#findComment-257085 Share on other sites More sharing options...
jeppers Posted May 19, 2007 Author Share Posted May 19, 2007 Echo the path and hit the address bar and see if you can pull up the image that way i am not sure on how to do this is there any chance you could explain it to me like a beginner i am thanks but my database works well now thanks Quote Link to comment https://forums.phpfreaks.com/topic/52111-solved-i-have-created-a-upload-function-but-how-do-i-upload-the-file-name-to-a-database/#findComment-257130 Share on other sites More sharing options...
jeppers Posted May 19, 2007 Author Share Posted May 19, 2007 i also did not fine any error with syntax of $thumb and that path directory.... any ideas any one Quote Link to comment https://forums.phpfreaks.com/topic/52111-solved-i-have-created-a-upload-function-but-how-do-i-upload-the-file-name-to-a-database/#findComment-257143 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.