yo hecho subir imagenes y mysql todos bien por fin
Código PHP:
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
if (PHP_VERSION < 6) {
$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")) {
$tipo_prod = $_POST["lstTipo"];
//Guardar imagen
$uploads_dir = '/imagenes';
foreach ($_FILES["imagenes"]["error"] as $key => $error) {
if ($error == UPLOAD_ERR_OK) {
$tmp_name = $_FILES["imagenes"]["tmp_name"][$key];
$name = $_FILES["imagenes"]["name"][$key];
move_uploaded_file($tmp_name, "$uploads_dir/$name");
}
}
$insertSQL = sprintf("INSERT INTO wp_productos (nombre, des, precio, referencia, imagenes, imagenes2, imagenes3, imagenes4) VALUES (%s, %s, %s, %s, %s, %s, %s, %s)",
GetSQLValueString($_POST['nombre'], "text"),
GetSQLValueString($_POST['des'], "text"),
GetSQLValueString($_POST['precio'], "double"),
GetSQLValueString($_POST['referencia'], "text"),
GetSQLValueString($_FILES['imagen']['name'], "text"),
GetSQLValueString($_POST['imagenes2'], "text"),
GetSQLValueString($_POST['imagenes3'], "text"),
GetSQLValueString($_POST['imagenes4'], "text"));
if ($_FILES["imagen"]["error"] > 0){
echo "ha ocurrido un error";
} else {
//ahora vamos a verificar si el tipo de archivo es un tipo de imagen permitido.
//y que el tamano del archivo no exceda los 100kb
$permitidos = array("image/jpg", "image/jpeg", "image/gif", "image/png");
$limite_kb = 2000;
if (in_array($_FILES['imagen']['type'], $permitidos) && $_FILES['imagen']['size'] <= $limite_kb * 1024){
//esta es la ruta donde copiaremos la imagen
//recuerden que deben crear un directorio con este mismo nombre
//en el mismo lugar donde se encuentra el archivo subir.php
$ruta = "imagenes/" . $_FILES['imagen']['name'];
//comprobamos si este archivo existe para no volverlo a copiar.
//pero si quieren pueden obviar esto si no es necesario.
//o pueden darle otro nombre para que no sobreescriba el actual.
if (!file_exists($ruta)){
//aqui movemos el archivo desde la ruta temporal a nuestra ruta
//usamos la variable $resultado para almacenar el resultado del proceso de mover el archivo
//almacenara true o false
$resultado = @move_uploaded_file($_FILES["imagen"]["tmp_name"], $ruta);
if ($resultado){
$nombre = $_FILES['imagen']['name'];
echo "el archivo ha sido movido exitosamente";
} else {
echo "ocurrio un error al mover el archivo.";
}
} else {
echo $_FILES['imagen']['name'] . ", este archivo existe";
}
} else {
echo "archivo no permitido, es tipo de archivo prohibido o excede el tamano de $limite_kb Kilobytes";
}
}
mysql_select_db($database_wordpress, $wordpress);
$Result1 = mysql_query($insertSQL, $wordpress) or die(mysql_error());
$insertGoTo = "index.php?page_id=13";
if (isset($_SERVER['QUERY_STRING'])) {
$insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
$insertGoTo .= $_SERVER['QUERY_STRING'];
}
header(sprintf("Location: %s", $insertGoTo));
}
?>