NYCPHP Meetup

NYPHP.org

[nycphp-talk] images into database

Alfonso Sánchez-Paus Díaz alfonso at ruboskizo.com
Thu Jun 12 07:03:38 EDT 2003


Oki dokim so i am going to post here to very nice filers that i have used
for a long time for uploading, saving and storing the images in the
database.
Hope it works out for you...

First one: do_upload.php. This one puts the file on your server:

//--------------------------------------------------------------------------
-------------------------------------------
<?
  $dir = "temp/";



  function handleupload() {
   global $HTTP_POST_FILES;
   global $dir;

   if (is_uploaded_file($HTTP_POST_FILES['img1']['tmp_name'])) {
    $filename = $HTTP_POST_FILES['img1']['tmp_name'];
    $realname = $HTTP_POST_FILES['img1']['name'];
    $type = $HTTP_POST_FILES['img1']['type'];
    $filesize = $HTTP_POST_FILES['img1']['size'];
    if($filesize > 496000)
      return "<span class=\\"form\\">Error</span><P
class=\\"texto\\"><b>Atención: </b>Debes seleccionar un fichero menor de
496Kb.</p><p><a href=\\"javascript:history.go(-1);\\">volver</a></p>";
    else{
      //$realname = str_replace("\\ ","_",$realname);
      $realname = str_replace(" ","_",$realname);
      copy($HTTP_POST_FILES['img1']['tmp_name'],$dir.$realname);
      $tipo_foto = GetImageSize($dir.$realname);
      if( $tipo_foto[2] != 2){
         unlink($dir.$realname);
         return "<span class=\\"form\\">Error</span><P
class=\\"texto\\"><b>Warning: </b>You must select a picture in JPEG
format.</p><p><a href=\\"javascript:history.go(-1);\\">volver</a></p>";
      }else{
         @chmod($dir.$realname,0777);
         return "<span class=\\"form\\">Success</span><P
class=\\"texto\\"><b>You sent: </b>".$realname.", with".$filesize."
bytes.</p><p><a href=\\"javascript:window.close()\\">Close window</a></p>";
      }
    }
   } else {
    return "<span class=\\"form\\">Error</span><P class=\\"texto\\"><b>Warning:
</b>You must select a file.</p><p><a
href=\\"javascript:history.go(-1);\\">back</a></p>";
   }
  }

?>

<html>
<head>
<title>Fotos Enviadas</title>
<link rel="stylesheet" href="images/estilos.css" type="text/css">
<meta http-equiv="Content-Type" content="text/html;
charset=iso-8859-1"><body bgcolor="#d1d4d6">
<table width="300" border="0" cellpadding="0" cellspacing="0"
bordercolor="4a4242">
  <tr bgcolor="#000000" valign="middle" align="center">
    <td class="text" colspan="4"> <p><font color="#FFFFFF">SELECT FILE
        </font></p></td>
  </tr>
  <tr>
    <td class="text" colspan="4" align="center" bgcolor="4a4242"> <div
align="left">
        <table width="303" border="0" align="center" cellpadding="2"
cellspacing="0" bgcolor="4a4242">
          <tr bgcolor="#CCFFCC">
            <td colspan="5" class="text" bgcolor="4a4242" height="94">
              <?=handleupload();?>
            </td>
          </tr>
        </table>
      </div></td>
  </tr>
</table>
</body>
</head>
</html>


//--------------------------------------------------------------------------
-------------------------------------------

Second file that you need:  select_file.php (I use it as a pop-up window)
//--------------------------------------------------------------------------
-------------------------------------------


<html>
<head>
<title>Upload your poictures</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

</head>
<script>

function send2(position){
  str= document.f.img1.value;
  str2='';
  length= document.f.img1.value.length;
  if (navigator.platform=="Win32") {
    situacion = strlastIndexOf('\\\\') +1;
  } else {
    situacion = str.lastIndexOf('/') +1;
  }

  for (i=situacion; i<=length; i++){
    str2 = str2 +  cadena.charAt(i);
  }

  eval('window.opener.document.f.elements[position].value = str2');
  document.f.submit();
}
</script>
<body bgcolor="#FFFFFF">
<table width="300" border="0" cellspacing="0" cellpadding="3">
  <tr bgcolor="#000000" valign="middle" align="center">
    <td class="text" colspan="4">
      <p class="form">SELECT FILE</p>
    </td>
  </tr>
  <tr>
    <td class="text" colspan="4" align="center" bgcolor="#FFFFFF">
      <div align="left">
        <p><br>
        </p>
        <table width="303" border="0" cellspacing="0" cellpadding="2"
align="center">
          <tr bgcolor="#CCFFCC">
            <td colspan="5" class="text" bgcolor="#FFFFFF" height="94">
              <table width="300" border="0" cellspacing="0" cellpadding="5"
bordercolor="#CCCCCC">
  <form enctype="multipart/form-data" method="post"
action="do_upload.php?where=<?=$where?>" name="f">
                <tr>
                  <td>
                      <input type="file" size="25" name="img1"
class="formC">
                  </td>
                </tr>
  </form>
                <tr>
                  <td colspan="2">
                    <div align="center"> </div>
                    <p><a class="formC"
href="javascript:send2(<?=$position?>)">Send</a></p>
                    <hr>
                    <div align="center"> </div>
                  </td>
                </tr>
              </table>
            </td>
          </tr>
        </table>

      </div>
      </td>
  </tr>
</table>
</body>
</html>
//--------------------------------------------------------------------------
-------------------------------------------

So here is how it works:
You pass the position on a form of the text input where you are going to
load the image (the select_file.php will fill the form in that position with
the name of the uploaded file).
You can have a button next to the text input saying "Send file...", this
should have an onClick event that would open the select_file.php window.
Remember to pass the position of the picture input text where you want the
name of the picture to be filled.
You can look at how this works in :
http://www.fotosprivadas.com/formulario.php It is spanish sorry, but you can
see the fundamentals of this.
Once you upload the files, you can save the data on your database when you
submit your form.
Regards, Alfie








More information about the talk mailing list