Page 1 of 1

Uploading Pictures to my MySQL db w/ PHP

Posted: Sat Aug 25, 2007 4:34 pm
by desktopdevil2000
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


What i want is for the form to upload an image and insert the name of the file in the MySQL database, this is what i have currently thank you:

Code: Select all

<?php
if (phpversion() > "4.0.6") {
	$HTTP_POST_FILES = &$_FILES;
}
define("MAX_SIZE",0);
define("DESTINATION_FOLDER", "/up_images/");
define("no_error", "");
define("yes_error", "");
$_accepted_extensions_ = "";
if(strlen($_accepted_extensions_) > 0){
	$_accepted_extensions_ = @explode(",",$_accepted_extensions_);
} else {
	$_accepted_extensions_ = array();
}
$_file_ = $HTTP_POST_FILES['picture'];
if(is_uploaded_file($_file_['tmp_name']) && $HTTP_POST_FILES['picture']['error'] == 0){
	$errStr = "";
	$_name_ = $_file_['name'];
	$_type_ = $_file_['type'];
	$_tmp_name_ = $_file_['tmp_name'];
	$_size_ = $_file_['size'];
	if($_size_ > MAX_SIZE && MAX_SIZE > 0){
		$errStr = "File troppo pesante";
	}
	$_ext_ = explode(".", $_name_);
	$_ext_ = strtolower($_ext_[count($_ext_)-1]);
	if(!in_array($_ext_, $_accepted_extensions_) && count($_accepted_extensions_) > 0){
		$errStr = "Estensione non valida";
	}
	if(!is_dir(DESTINATION_FOLDER) && is_writeable(DESTINATION_FOLDER)){
		$errStr = "Cartella di destinazione non valida";
	}
	if(empty($errStr)){
		if(@copy($_tmp_name_,DESTINATION_FOLDER . "/" . $_name_)){
			header("Location: " . no_error);
		} else {
			header("Location: " . yes_error);
		}
	} else {
		header("Location: " . yes_error);
	}
}
?>
<?php
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  $theValue = (!get_magic_quotes_gpc()) ? addslashes($theValue) : $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"] == "uploadpic")) {
  $insertSQL = sprintf("INSERT INTO picture (description, picture, parentid) VALUES (%s, %s, %s)",
                       GetSQLValueString($_POST['description'], "text"),
                       GetSQLValueString($_POST['$_name_'], "text"),
                       GetSQLValueString($_POST['parentid'], "int"));

  mysql_select_db($database_CleverWebDesigns, $CleverWebDesigns);
  $Result1 = mysql_query($insertSQL, $CleverWebDesigns) or die(mysql_error());

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

mysql_select_db($database_CleverWebDesigns, $CleverWebDesigns);
$query_rsPicture = "SELECT * FROM picture ORDER BY id DESC";
$rsPicture = mysql_query($query_rsPicture, $CleverWebDesigns) or die(mysql_error());
$row_rsPicture = mysql_fetch_assoc($rsPicture);
$totalRows_rsPicture = mysql_num_rows($rsPicture);
?>

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]