title: How to save uploaded image in database
body: <p>I need help in writing this code to upload and save uploaded image in data base.</p>
  <p>File 1:</p>
  <pre><code>&lt;?php
  
        &lt;form action=&quot;upload.php&quot; method=&quot;post&quot; enctype=&quot;multipart/form-data&quot; target=&quot;upload_target&quot; onsubmit=&quot;startUpload();&quot; &gt;
                       
                  &lt;label&gt;File:  
                  &lt;input name=&quot;myfile&quot; type=&quot;file&quot; size=&quot;30&quot; /&gt;
                  &lt;/label&gt;
                         
                  &lt;input type=&quot;submit&quot; name=&quot;submitBtn&quot; class=&quot;sbtn&quot; value=&quot;Upload&quot; /&gt;
                         
                       
                    &lt;iframeid=&quot;upload_target&quot;name=&quot;upload_target&quot;src=&quot;#&quot;style=&quot;width:0;height:0;border:0px solid #fff;&quot;&gt;&lt;/iframe&gt;
                  &lt;/form&gt;
                  &lt;/div&gt;
  </code></pre>
  <p>File 2:</p>
  <pre><code>&lt;?php
  
  
  
  
     // Edit upload location here
     $destination_path = getcwd().DIRECTORY_SEPARATOR;
     $target_path=&quot;my/&quot;;
     $result = 0;
     $name=$_FILES['myfile']['name'];
     $target_path = $target_path . basename( $_FILES['myfile']['name']);
  
     if(@move_uploaded_file($_FILES['myfile']['tmp_name'], $target_path)) {
     list($width, $height, $type, $attr) = getimagesize($target_path);
  echo &quot;Image width &quot; .$width;
  echo &quot;&lt;BR&gt;&quot;;
  echo &quot;Image height &quot; .$height;
  echo &quot;&lt;BR&gt;&quot;;
  echo &quot;Image type &quot; .$type;
  echo &quot;&lt;BR&gt;&quot;;
  echo &quot;Attribute &quot; .$attr;
          
  
        $result = 1;
     }
     
    // sleep(1);
     
     
  
     
  $link=mysql_connect('localhost','root','');
  if(!$link)
  {die('you cannot connect to database...');}
  $db=mysql_select_db('final');
  if(!$db)die('smile');
  
  if (isset($_FILES['myfile']) &amp;&amp; $_FILES['myfile']['size'] &gt; 0) {
  
  // define the posted file into variables 
  $name = $_FILES['myfile']['name']; 
  $tmp_name = $_FILES['myfile']['tmp_name']; 
  $type = $_FILES['myfile']['type']; 
  $size = $_FILES['myfile']['size']; 
  
  // if your server has magic quotes turned off, add slashes manually 
  //if(!get_magic_quotes_gpc()){ 
  //$name = addslashes($name); 
  //} 
  
  // open up the file and extract the data/content from it 
  $extract = fopen($tmp_name, 'r'); 
  $content = fread($extract, filesize($tmp_name)); 
  $content = addslashes($content); 
  fclose($extract);  
  
  // connect to the database 
  
  
  // the query that will add this to the database 
  $s=mysql_query(&quot;SET NAMES 'utf8'&quot;);
  $sql = &quot;INSERT INTO `final`.`products` (`Productcode`, `Productname`, `Price`,`Descriptionofgood`, `image`) VALUES ('','','','','&quot;.$target_path.&quot;') WHERE `products`.`Productcode`='1371' &quot;;
  $results = mysql_query($sql);
  if(!$result)die('not');
  }
  
  ?&gt;
  </code></pre>