how can image be uploaded

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • anuragpj
    New Member
    • Jan 2007
    • 32

    how can image be uploaded

    I want that a user can upload his image .
    How can his image be stored in the database and can be retrieved from database?
  • millertime90
    New Member
    • Feb 2007
    • 3

    #2
    Originally posted by anuragpj
    I want that a user can upload his image .
    How can his image be stored in the database and can be retrieved from database?
    check out post http://www.thescripts.com/forum/thread614464.html should have the solution your looking for.

    Comment

    • ronverdonk
      Recognized Expert Specialist
      • Jul 2006
      • 4259

      #3
      Look at this reply to a similar question I just posted http://www.thescripts.com/forum/post2424480-4.html

      Ronald :cool:

      Comment

      • DavidPr
        New Member
        • Mar 2007
        • 155

        #4
        I read that thread and downloaded the zip files. The script works great, but how do I display the pictures?

        Comment

        • anandaraman
          New Member
          • Feb 2007
          • 8

          #5
          Main.php

          <title> File Upload and Download (MYSQL Database)</title>

          <h2 align="center"> File Upload and Download (MYSQL Database)</h2>
          <table width="200" border="0" align="center" >
          <tr id="trr">
          <td align="center"> <a href="upload.ph p?up=yes"><stro ng>
          Upload File
          </strong> </a> </td>
          </tr>
          <tr id="trr" >
          <td align="center"> <a href="download. php?dw=yes"><st rong>
          Download File
          </strong> </a></td>
          </tr>
          </table>

          upload.php

          <title>Upload Files to MYSQL Database</title>

          <h2 align="center"> Upload Files to MYSQL Database</h2>
          <?php
          if($_GET['up']=="yes")
          {
          print '<form name="uplform" action="" method="post" enctype="multip art/form-data" >
          <table width="450" border="0" align="center" ><stron>
          <tr id="trr">
          <th width="207">Fil e Upload </th>

          <td width="233"><in put type="file" name="uplfile" value="sss"></td></tr>

          <tr id="trr">
          <td>&nbsp;</td>
          <td>&nbsp;</td>
          </tr>
          <tr id="trr">
          <td align="right">< input type="submit" name="Submit" value="Submit"> </td>
          <td><input type="reset" name="Reset" value="Reset"></td>
          </tr></strong>
          </table></form>';
          }
          ?>

          <?php
          if(isset($_POST['Submit']) && $_POST['Submit']=="Submit")
          {

          $flag=0;
          if(isset($HTTP_ POST_FILES['uplfile']))
          {
          if(is_uploaded_ file($HTTP_POST _FILES['uplfile']['tmp_name']))

          if(!file_exists ($HTTP_POST_FIL ES['uplfile']['name']))

          if(@rename($HTT P_POST_FILES['uplfile']['tmp_name'],$HTTP_POST_FIL ES['uplfile']['name']))
          $flag=1;
          else
          $flag=0;

          }

          if($flag==1)
          {

          $file_u=$HTTP_P OST_FILES['uplfile']['name'];
          $da=date("Y-m-d ",time() + (34200)).date(" H:i:s",time() + (34200));

          mysql_connect(" localhost","roo t","") or die("Sever connection error");

          mysql_select_db ("file") or die ("Database error");
          $val = fread(fopen($fi le_u, "r+"), filesize($file_ u)+1);
          if (!empty($val))
          {

          $val = '0x' . bin2hex($val);
          $ext=substr($HT TP_POST_FILES['uplfile']['name'],strlen($HTTP_P OST_FILES['uplfile']['name'])-3,3);
          $q1="INSERT INTO `file_tb` ( `id` , `file_name` , `file` , `ext` , `date_time` )
          VALUES ( NULL , '$file_u', $val, '$ext', '$da')";

          if(@mysql_query ($q1))
          print '<h1 align="center"> File Successfully Uploaded</h1>';
          else
          print '<h1 align="center"> File is too Large</h1>';


          }

          $do=unlink ($HTTP_POST_FIL ES['uplfile']['name']);

          }
          }
          ?>


          download.php

          <title>Downlo ad Files From MYSQL Database</title>

          <h2 align="center"> Download Files From MYSQL Database</h2>
          <?php
          if($_GET['dw']=="yes")
          {
          print '
          <table border="0" align="center" width="500">
          <tr id="thr">
          <th>File ID</th>
          <th>File Name</th>
          <th>File Ext</th>
          <th>Date & Time</th>

          </tr>';
          mysql_connect(" localhost","roo t","") or die("Sever connection error");

          mysql_select_db ("file") or die ("Database error");
          $sel=mysql_quer y("select * from file_tb") or die("Table Error");
          while($r=mysql_ fetch_array($se l))
          {
          print '<tr id="trr">';
          print '<td>'.$r['id'].'</td>';
          print '<td><a href="download. php?action=view &amp;id='.$r['id'].'">'.$r['file_name'].'</a></td>';
          print '<td>'.$r['ext'].'</td>';
          print '<td>'.$r['date_time'].'</td>';
          print '</tr>';
          }
          print '</table>';
          }
          ?>



          <?php
          if (isset($_GET['action']) && ($_GET['action'] == "view"))
          {
          $fid=$_GET['id'];
          mysql_connect(" localhost","roo t","") or die("Sever connection error");
          mysql_select_db ("file") or die ("Database error");
          $sel=mysql_quer y("select * from file_tb") or die("Table Error");
          while($r=mysql_ fetch_array($se l))
          {
          if($r['id']==$fid)
          {
          $fi=$r['file'];

          $ab="files/".$r['file_name'];
          $fp=fopen($ab," w+") or die("file error");
          fwrite($fp,$fi) ;

          print '<img src="';
          print $ab;
          print '" >';
          print '<h3 align="center"> File Downloaded in <a href="files">Fi les</a> Directory</h3>';
          }
          }

          }
          ?>


          DATABASE.php

          <title>MYSQL Table Installation</title>


          <?php
          print '<h2 align="center"> MYSQL Table Installation</h2>';
          mysql_connect(" localhost","roo t","") or die("Sever connection error");
          mysql_query("CR EATE DATABASE IF NOT EXISTS file") or die("Database Already Exists");
          mysql_select_db ("file");
          $q="CREATE TABLE IF NOT EXISTS `file_tb` (
          `id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
          `file_name` VARCHAR( 20 ) NOT NULL ,
          `file` LONGBLOB NOT NULL ,
          `ext` VARCHAR( 5 ) NOT NULL ,
          `date_time` DATETIME NOT NULL
          ) ENGINE = MYISAM
          ";
          mysql_query($q) or die("Table Error");
          print '<h3 align="center"> Installation Complete</h3>';

          ?>

          first run database.php, then run main.php.

          Comment

          Working...