Jump to content

how to add image type to db??


runnerjp

Recommended Posts

i have my image upload and resize script all nicely working

 

<?php

session_start();
//load the config file
include("config.php");
require_once '../settings.php';


//if the for has submittedd
if (isset($_POST['upForm'])){

       $file_type = $_FILES['imgfile']['type'];
       $file_name = $_FILES['imgfile']['name'];
       $file_size = $_FILES['imgfile']['size'];
       $file_tmp = $_FILES['imgfile']['tmp_name'];

       //check if you have selected a file.
       if(!is_uploaded_file($file_tmp)){
          echo "Error: Please select a file to upload!. <br>--<a href=\"$_SERVER[php_SELF]\">back</a>";
          exit(); //exit the script and don't do anything else.
       }
       //check file extension
       $ext = strrchr($file_name,'.');
       $ext = strtolower($ext);
       if (($extlimit == "yes") && (!in_array($ext,$limitedext))) {
          echo "Wrong file extension.  <br>--<a href=\"$_SERVER[php_SELF]\">back</a>";
          exit();
       }
       //get the file extension.
       $getExt = explode ('.', $file_name);
       $file_ext = $getExt[count($getExt)-1];

//get users ID
    $id = $_SESSION['user_id'];
    
      
      //get the new width variable.
       $ThumbWidth = $img_thumb_width;

       //keep image type
       if($file_size){
          if($file_type == "image/pjpeg" || $file_type == "image/jpeg"){
               $new_img = imagecreatefromjpeg($file_tmp);
           }elseif($file_type == "image/x-png" || $file_type == "image/png"){
               $new_img = imagecreatefrompng($file_tmp);
           }elseif($file_type == "image/gif"){
               $new_img = imagecreatefromgif($file_tmp);
           }
           //list width and height and keep height ratio.
           list($width, $height) = getimagesize($file_tmp);
           $imgratio=$width/$height;
           if ($imgratio>1){
              $newwidth = $ThumbWidth;
              $newheight = $ThumbWidth/$imgratio;
           }else{
                 $newheight = $ThumbWidth;
                 $newwidth = $ThumbWidth*$imgratio;
           }
           //function for resize image.
           if (function_exists(imagecreatetruecolor)){
           $resized_img = imagecreatetruecolor($newwidth,$newheight);
           }else{
                 die("Error: Please make sure you have GD library ver 2+");
           }
           imagecopyresized($resized_img, $new_img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
           //save image
           ImageJpeg ($resized_img,"$path_thumbs/$id.$file_ext");
           ImageDestroy ($resized_img);
           ImageDestroy ($new_img);
           //print message
           echo "<br>Image Thumb: <a href=\"$path_thumbs/$id.$file_ext\">$path_thumbs/$id.$file_ext</a>";
        }

        //upload the big image
        move_uploaded_file ($file_tmp, "$path_big/$id.$file_ext");

        echo "<br>Image Big: <a href=\"$path_big/$id.$file_ext\">$path_big/$id.$file_ext</a>";

        echo "<br><br>--<a href=\"$_SERVER[php_SELF]\">back</a>";

}else{ //if the form hasn't been submitted.

      //print the form
      echo "<script>
      function view_img(img_name){
         document[img_name].src = upForm.imgfile.value;
            document[img_name].width = 150;
      }
      </script>\n\n
      <br><h3>:: Browse an Image to Upload:</h3>\n
      <form method=\"post\" name=\"upForm\" enctype=\"multipart/form-data\" action=\"$_SERVER[php_SELF]\">\n
      <input type=\"file\" name=\"imgfile\" onchange=\"javascript:view_img('img_vv');\"> <img src='' name='img_vv' width='0'><br>\n
      Image width will resize to <b>$img_thumb_width</b> with height ratio.
      <br><input type=\"Submit\" name=\"upForm\" value=\"Upload & Resize\">\n
      </form>
      <a href=\"view_gallery.php\">View Images</a>";
      }

?> 

 

 

how would i get the image exe typ and add it to my db with the users id

 

 

at the moment i have my table  user_images

 

and in the table i have  user_id / ext / created / modified

 

but i cant work out how to add all this data into it...

Link to comment
Share on other sites

This is what I use

<?php
include("settings.php3");
if ($img1_name != "") {
        
        @copy("$img1" , "$main_dir/$img1_name") 
               
                or die("Couldn't Upload Your File.");


} else {

echo "";
}
if ($img1_name == "") 
{$img1_name = '';}
if ($img1_name != "")
{$pic = "<img src=$img1_name height=100>";}
if ($img1_name == "")
{$pic = '';}
?>
<?php 
include("settings.php3");
mysql_connect ($dbhost, $dblogin,$dbpass);

mysql_select_db ($dbname);

mysql_query ("INSERT INTO services (company, address, city, state, phone, picture, keywords) 
                VALUES ('$company', '$address', '$city', '$state', '$phone', '$pic', '$keywords', '$description')
             ");

 

Just change it aroun a little to suit you.

Link to comment
Share on other sites

!Don't bump your own posts pls!

 

Use GD to change the format. I'm not going to provide some code for you because there's plenty on google ;)

Why bother changing the format though? Just use the image as it is (saves you processing time). And the uploaded file information gives you the extension. Or you could use one of one PHPs inbuilt functions filetype() .

Link to comment
Share on other sites

<?php

session_start();
//load the config file
include("config.php");
require_once '../settings.php';


//if the for has submittedd
if (isset($_POST['upForm'])){

       $file_type = $_FILES['imgfile']['type'];
       $file_name = $_FILES['imgfile']['name'];
       $file_size = $_FILES['imgfile']['size'];
       $file_tmp = $_FILES['imgfile']['tmp_name'];

       //check if you have selected a file.
       if(!is_uploaded_file($file_tmp)){
          echo "Error: Please select a file to upload!. <br>--<a href=\"$_SERVER[php_SELF]\">back</a>";
          exit(); //exit the script and don't do anything else.
       }
       //check file extension
       $ext = strrchr($file_name,'.');
       $ext = strtolower($ext);
       if (($extlimit == "yes") && (!in_array($ext,$limitedext))) {
          echo "Wrong file extension.  <br>--<a href=\"$_SERVER[php_SELF]\">back</a>";
          exit();
       }
       //get the file extension.
       $getExt = explode ('.', $file_name);
       $file_ext = $getExt[count($getExt)-1];

//get users ID
    $id = $_SESSION['user_id'];
    
      
      //get the new width variable.
       $ThumbWidth = $img_thumb_width;

       //keep image type
       if($file_size){
          if($file_type == "image/pjpeg" || $file_type == "image/jpeg"){
               $new_img = imagecreatefromjpeg($file_tmp);
           }elseif($file_type == "image/x-png" || $file_type == "image/png"){
               $new_img = imagecreatefrompng($file_tmp);
           }elseif($file_type == "image/gif"){
               $new_img = imagecreatefromgif($file_tmp);
           }
           //list width and height and keep height ratio.
           list($width, $height) = getimagesize($file_tmp);
           $imgratio=$width/$height;
           if ($imgratio>1){
              $newwidth = $ThumbWidth;
              $newheight = $ThumbWidth/$imgratio;
           }else{
                 $newheight = $ThumbWidth;
                 $newwidth = $ThumbWidth*$imgratio;
           }
           //function for resize image.
           if (function_exists(imagecreatetruecolor)){
           $resized_img = imagecreatetruecolor($newwidth,$newheight);
           }else{
                 die("Error: Please make sure you have GD library ver 2+");
           }
           imagecopyresized($resized_img, $new_img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
           //save image
           ImageJpeg ($resized_img,"$path_thumbs/$id.$file_ext");
           ImageDestroy ($resized_img);
           ImageDestroy ($new_img);
           //print message
           echo "<br>Image Thumb: <a href=\"$path_thumbs/$id.$file_ext\">$path_thumbs/$id.$file_ext</a>";
        }

        //upload the big image
        move_uploaded_file ($file_tmp, "$path_big/$id.$file_ext");

        echo "<br>Image Big: <a href=\"$path_big/$id.$file_ext\">$path_big/$id.$file_ext</a>";

        echo "<br><br>--<a href=\"$_SERVER[php_SELF]\">back</a>";

}else{ //if the form hasn't been submitted.

      //print the form
      echo "<script>
      function view_img(img_name){
         document[img_name].src = upForm.imgfile.value;
            document[img_name].width = 150;
      }
      </script>\n\n
      <br><h3>:: Browse an Image to Upload:</h3>\n
      <form method=\"post\" name=\"upForm\" enctype=\"multipart/form-data\" action=\"$_SERVER[php_SELF]\">\n
      <input type=\"file\" name=\"imgfile\" onchange=\"javascript:view_img('img_vv');\"> <img src='' name='img_vv' width='0'><br>\n
      Image width will resize to <b>$img_thumb_width</b> with height ratio.
      <br><input type=\"Submit\" name=\"upForm\" value=\"Upload & Resize\">\n
      </form>
      <a href=\"view_gallery.php\">View Images</a>";
      }
      $sql = "
INSERT INTO `user_images` 
              (`user_id`, `ext`)
            VALUES ('$id','$file_ext',            
            )
          ";
?>

 

i tried that and it does not insert the data into my db...any reaosn why?

Link to comment
Share on other sites

solvd by using

     $query = "INSERT INTO user_images (user_id, ext )
VALUES ('$id', '$file_ext')";

$results = mysql_query($query) or die 
("Could not execute query : $query." . mysql_error());

if ($results)
{
echo "Details added.";
}
mysql_close();
?>

 

but it also adds anouther two bits of data with id 0  like below.... how comes?

 

user_id ext

1           jpg

0

0

Link to comment
Share on other sites

Still seems to add more then one record.... as there is only 1 user id and ext it records 1 and then records anouther 2 with no data :S

humm i have been messing around with it and by only doing

$query = "INSERT INTO user_images (user_id, ext )
VALUES ('$id', '$file_ext')"; 

it does not add a single bit of data :S

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.