uploading images with php

Joined
Aug 12, 2006
Messages
60
I've been experimenting with this for awhile now, and i've always run into a dead end.

I'm trying to upload a file, jpg, gif, png, etc to a directory on my web server, then be able to have it appear on the page that i want. I've been able to upload a file without any trouble...The tough part is getting the image to appear on say my index page. This is my structure of what i've done so far.

used phpMySQL to create a record with these values:

table name frontend
id - INT - Auto Increment - Primary Key
picture_file - text

this works just fine...i can log onto my sql database and see that it actually records the jpg name.(not the image itself) i can also find the directory on the web server and see that it uploads the image just fine. This Code works just fine...no problem here.

Code:
<?php require_once('Connections/curb.php'); ?>
<?php
//initialize the session
if (!isset($_SESSION)) {
  session_start();
}

// ** Logout the current user. **
$logoutAction = $_SERVER['PHP_SELF']."?doLogout=true";
if ((isset($_SERVER['QUERY_STRING'])) && ($_SERVER['QUERY_STRING'] != "")){
  $logoutAction .="&". htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset($_GET['doLogout'])) &&($_GET['doLogout']=="true")){
  //to fully log out a visitor we need to clear the session varialbles
  $_SESSION['MM_Username'] = NULL;
  $_SESSION['MM_UserGroup'] = NULL;
  $_SESSION['PrevUrl'] = NULL;
  unset($_SESSION['MM_Username']);
  unset($_SESSION['MM_UserGroup']);
  unset($_SESSION['PrevUrl']);
	
  $logoutGoTo = "index.php";
  if ($logoutGoTo) {
    header("Location: $logoutGoTo");
    exit;
  }
}
?><?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
  $insertSQL = sprintf("INSERT INTO frontend (picture_file) VALUES (%s)",
                       GetSQLValueString($_POST['picture_file'], "text"));

  mysql_select_db($database_curb, $curb);
  $Result1 = mysql_query($insertSQL, $curb) or die(mysql_error());

  $insertGoTo = "adminimages.php";
  if (isset($_SERVER['QUERY_STRING'])) {
    $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
    $insertGoTo .= $_SERVER['QUERY_STRING'];
  }
  header(sprintf("Location: %s", $insertGoTo));
}

mysql_select_db($database_curb, $curb);
$query_Recordset2 = "SELECT * FROM frontend";
$Recordset2 = mysql_query($query_Recordset2, $curb) or die(mysql_error());
$row_Recordset2 = mysql_fetch_assoc($Recordset2);
$totalRows_Recordset2 = mysql_num_rows($Recordset2);
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>::Lightbox Demo::</title>
<style type="text/css">
<!--
body {
	background-color: #333333;
}
.style1 {color: #FFFFFF}
a:link {
	color: #FFFFFF;
	text-decoration: none;
}
a:visited {
	text-decoration: none;
	color: #FFFFFF;
}
a:hover {
	text-decoration: none;
	color: #FFFFFF;
}
a:active {
	text-decoration: none;
	color: #FFFFFF;
}
-->
</style></head>

<body>
<table width="600" border="0" align="center" cellpadding="0" cellspacing="0" class="style1">
  <tr>
    <td><div align="center" class="style1">Lightbox Demo Admin</div></td>
  </tr>
  <tr>  </tr>
  <tr>
    <td><form action="<?php echo $editFormAction; ?>" method="post" enctype="multipart/form-data" name="form1">
      <table width="100%" align="center">
          <tr valign="baseline">
            <td nowrap align="right">Picture_file:</td>
            <td><input type="text" name="picture_file" value="" size="45"></td>
          </tr>
          <tr valign="baseline">
            <td nowrap align="right">&nbsp;</td>
            <td><input name="userjpg" type="file" id="userjpg" size="45" />
            <?php if (@is_uploaded_file($_FILES["userjpg"]["tmp_name"])) { 
copy($_FILES["userjpg"]["tmp_name"], "/home/curbyour/public_html/images/" . $_FILES["userjpg"]["name"]); 
echo "<p>File uploaded successfully.</p>"; 
} 
?></td>
          </tr>
          <tr valign="baseline">
            <td nowrap align="right">&nbsp;</td>
            <td><input type="submit" value="Insert record"></td>
          </tr>
        </table>
        <input type="hidden" name="MM_insert" value="form1">
      </form>
    <p>&nbsp;</p></td>
  </tr>
  <tr>  </tr>
  <tr>
    <td><table width="100%" border="0" cellpadding="3" cellspacing="3" class="style1">
      <?php do { ?>
        <tr>
            <td><div align="center"><a href="editimages.php?recordID=<?php echo $row_Recordset2['id']; ?>">edit</a></div></td>
          <td><div align="center"><a href="deleteimages.php?recordID=<?php echo $row_Recordset2['id']; ?>">delete</a></div></td>
          <td><?php echo $row_Recordset2['picture_file']; ?></td>
        </tr>
        <?php } while ($row_Recordset2 = mysql_fetch_assoc($Recordset2)); ?>
    </table></td>
  </tr>
  <tr>
    <td><div align="center" class="style1"><a href="<?php echo $logoutAction ?>">logout</a></div></td>
  </tr>
</table>
</body>
</html>
<?php
mysql_free_result($Recordset2);
?>

This is where i run into a problem. Here is the code i use to try and dynamically call the image:

Code:
<?php require_once('Connections/curb.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

mysql_select_db($database_curb, $curb);
$query_Recordset2 = "SELECT * FROM frontend";
$Recordset2 = mysql_query($query_Recordset2, $curb) or die(mysql_error());
$row_Recordset2 = mysql_fetch_assoc($Recordset2);
$totalRows_Recordset2 = mysql_num_rows($Recordset2);
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<script type="text/javascript" src="lightbox/js/prototype.js"></script>
<script type="text/javascript" src="lightbox/js/scriptaculous.js?load=effects"></script>
<script type="text/javascript" src="lightbox/js/lightbox.js"></script>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>::lightbox::</title><style type="text/css">
<!--
body {
	background-color: #333333;
}
.style1 {color: #FFFFFF}
a:link {
	color: #FFFFFF;
}
a:visited {
	color: #FFFFFF;
}
a:hover {
	color: #FFFFFF;
}
a:active {
	color: #FFFFFF;
}
-->
</style></head>

<body>
<table width="100" border="0" align="center" cellpadding="3" cellspacing="3" class="style1">
  <tr>
    <td><div align="center">Demo</div></td>
  </tr>
  <?php do { ?>
    <tr>
      <td><img src="images/?<?php echo $row_Recordset2['picture_file']; ?>" width="100" height="100" /></td>
    </tr>
    <?php } while ($row_Recordset2 = mysql_fetch_assoc($Recordset2)); ?>
<tr>
    <td><a href="adminimages.php">upload image</a></td>
  </tr>
</table>
</body>
</html>
<?php
mysql_free_result($Recordset2);
?>

nothing happens. A couple of days ago i thought i had it all figured out, the image even came up, however, i want to try to get lightbox to work with the images that i uploaded and i messed something up and it won't work anymore.
 
Is the filepath for the <IMG> tag correct with respect to the file? That's the only problem area I can think of if the image is correct on the db and server.
 
Back
Top