Hej jeg ønsker at følgendene script akal overføre billeder til database i stedet for mappe
så jeg udlæset base fra mysql:
- // Uploads recipe picture..
- function uploadPicture($temp,$name,$size,$newID) {
- if (is_uploaded_file($temp) && is_writeable($this->settings->server_path.'templates/images/recipes')) {
- move_uploaded_file($temp,$this->settings->server_path.'templates/images/recipes/'.$name);
- if (file_exists($this->settings->server_path.'templates/images/recipes/'.$name)) {
- mysql_query("INSERT INTO ".$this->prefix."pictures (
- recipe,picPath
- ) VALUES (
- '$newID','$name'
- )") or die(mysql_error());
- // Required by some servers to make image viewable and accessible via FTP..
- @chmod($this->settings->server_path.'templates/images/recipes/'.$name,0644);
- // Does image need resizing..
- if ($this->settings->autoResize!=0) {
- // Comma delimited string?..
- if (strpos($this->settings->autoResize,',')!==FALSE) {
- // Assign current and new sizes to arrays..
- $imgSizes = @getimagesize($this->settings->server_path.'templates/images/recipes/'.$name);
- $newSizes = array_map('trim',explode(',',$this->settings->autoResize));
- // If all is set and resize is required, continue..
- if (isset($newSizes[0]) && isset($newSizes[1]) && isset($imgSizes[0]) && isset($imgSizes[1]) && $newSizes[0]>0 && $newSizes[1]>0) {
- if ($imgSizes[0]>$newSizes[0] && $imgSizes[1]>$newSizes[1]) {
- $this->resizeUploadPicture($name,$newSizes);
- }
- }
- }
- }
- }
- // Check temp file was cleared..
- if (file_exists($temp)) {
- @unlink($temp);
- }
- }
- }
-
- // Resizes uploaded jpeg..
- function resizeUploadPicture($name,$nsizes) {
- $path = $this->settings->server_path.'templates/images/recipes/'.$name;
- switch (substr(strtolower($name),strpos($name,'.')+1,strlen($name))) {
- // Just jpg/jpeg supported at the moment..
- case 'jpg':
- case 'jpeg':
- if (function_exists('imagecreatefromjpeg')) {
- $img = imagecreatefromjpeg($path);
- if ($img) {
- $i_width = imagesx($img);
- $i_height = imagesy($img);
- $scale = min($nsizes[0]/$i_width, $nsizes[1]/$i_height);
- // For thumbnail, maintain aspect ratio of original image
- // If image is smaller or equal to new sizes, no resize is necessary
- if ($scale<1) {
- $new_width = floor($scale * $i_width);
- $new_height = floor($scale * $i_height);
- $tmp_img = imagecreatetruecolor($new_width,$new_height);
- imagecopyresampled($tmp_img,$img,0,0,0,0,$new_width,$new_height,$i_width,$i_height);
- imagejpeg($tmp_img, $path);
- imagedestroy($img);
- imagedestroy($tmp_img);
- } else {
- imagedestroy($img);
- }
- }
- }
- break;
- }
- }
Jeg har følgendene script som virker som jeg onsker er der nogen der kan sætte den ind i funktion?
- if ( !preg_match( '/gif|png|x-png|jpeg/', $_FILES['userFile']['type']) ) {
- die('<p>Only browser compatible images allowed</p></body></html>');
- } else if ( strlen($_POST['altText']) < 9 ) {
- die('<p>Please provide meaningful alternate text</p></body></html>');
- } else if ( $_FILES['userFile']['size'] > 16384 ) {
- die('<p>Sorry file too large</p></body></html>');
- // Connect to database
- } else if ( !($link=mysql_connect($database['host'], $database['username'], $database['password'])) ) {
- die('<p>Error connecting to database</p></body></html>');
- } else if ( !(mysql_select_db($database['database'])) ) {
- die('<p>Error selecting database</p></body></html>');
- // Copy image file into a variable
- } else if ( !($handle = fopen ($_FILES['userFile']['tmp_name'], "r")) ) {
- die('<p>Error opening temp file</p></body></html>');
- } else if ( !($image = fread ($handle, filesize($_FILES['userFile']['tmp_name']))) ) {
- die('<p>Error reading temp file</p></body></html>');
- } else {
- fclose ($handle);
- // Commit image to the database
- $image = mysql_real_escape_string($image);
- $alt = htmlentities($_POST['altText']);
- $query = 'INSERT INTO mr_images (type,name,alt,img,recipe) VALUES ("' . $_FILES['userFile']['type'] . '","' . $_FILES['userFile']['name'] . '","' . $alt . '","' . $image . '","' . $newID . '")';
- if ( !(mysql_query($query,$link)) ) {
- die('<p>Error writing image to database</p></body></html>');
- } else {
- die('<p>Image successfully copied to database</p></body></html>');
- }
- }
- ?>
Indlæg senest redigeret d. 21.10.2012 21:41 af Bruger #12519