Foros del Web » Programando para Internet » PHP »

Problema con upload img

Estas en el tema de Problema con upload img en el foro de PHP en Foros del Web. Tengo el siguiente código para hacer upload de una imagen, guardarla en una carpeta y guardar el nombre en una bd para luego mostrar la ...
  #1 (permalink)  
Antiguo 21/01/2012, 17:52
Avatar de cuervo699  
Fecha de Ingreso: febrero-2011
Mensajes: 104
Antigüedad: 13 años, 2 meses
Puntos: 7
Problema con upload img

Tengo el siguiente código para hacer upload de una imagen, guardarla en una carpeta y guardar el nombre en una bd para luego mostrar la imagen

Código PHP:
<?php require_once('../Connections/demo1.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")) {
  
  
////////////// Parte añadida 1 ////////////// 
  //array de archivos disponibles
  
$archivos_disp_ar = array('jpg''jpeg''gif''png');
  
//carpteta donde vamos a guardar la imagen
  
$carpeta 'imagenes/';
  
//recibimos el campo de imagen
  
$imagen $FILES['imagen']['tmp_name'];
  
//guardamos el nombre original de la imagen en una variable
  
$nombrebre_orig $FILES['imagen']['name'];
  
//el proximo codigo es para ver que extension es la imagen
  
$array_nombre explode('.',$nombrebre_orig);
  
$cuenta_arr_nombre count($array_nombre);
  
$extension strtolower($array_nombre[--$cuenta_arr_nombre]);
  
  
//validamos la extension
  
if(!in_array($extension$archivos_disp_ar)) $error "Este tipo de archivo no es permitido";
  
  if(empty(
$error)){
  
      
//creamos nuevo nombre para que tenga nombre unico
      
$nombre_nuevo time().'_'.rand(0,100).'.'.$extension;
      
//nombre nuevo con la carpeta
      
$nombre_nuevo_con_carpeta $carpeta.$nombre_nuevo;
      
//por fin movemos el archivo a la carpeta de imagenes
      
$mover_archivos move_uploaded_file($imagen $nombre_nuevo_con_carpeta);
      
//de damos permisos 777
      
chmod($nombre_nuevo_con_carpeta,0777);
      
      
///////////////////////////////////////////
      
       // se agrega "archivo_archivos, extension_archivos y la fecha" a la consulta y dos extra %s separados por comas
      
$insertSQL sprintf("INSERT INTO archivos (tipo_archivos, nombre_archivos, archivo_archivos, extension_archivos, fecha_archivos) VALUES (%s, %s, %s, %s, NOW())",
                           
GetSQLValueString($_POST['tipo'], "text"),
                           
GetSQLValueString($_POST['nombre'], "text"),
                           
////////////// Parte añadida ////////////// 
                           
GetSQLValueString($nombre_nuevo"text"),
                           
GetSQLValueString($extension"text"))
                           
///////////////////////////////////////////
                           
;
    
      
mysql_select_db($database_demo1$demo1);
      
$Result1 mysql_query($insertSQL$demo1) or die(mysql_error());
    }
}


$maxRows_Recordset1 1;
$pageNum_Recordset1 0;
if (isset(
$_GET['pageNum_Recordset1'])) {
  
$pageNum_Recordset1 $_GET['pageNum_Recordset1'];
}
$startRow_Recordset1 $pageNum_Recordset1 $maxRows_Recordset1;

mysql_select_db($database_demo1$demo1);
$query_Recordset1 "SELECT * FROM archivos WHERE tipo_archivos = 'general' ORDER BY fecha_archivos DESC";
$query_limit_Recordset1 sprintf("%s LIMIT %d, %d"$query_Recordset1$startRow_Recordset1$maxRows_Recordset1);
$Recordset1 mysql_query($query_limit_Recordset1$demo1) or die(mysql_error());
$row_Recordset1 mysql_fetch_assoc($Recordset1);

if (isset(
$_GET['totalRows_Recordset1'])) {
  
$totalRows_Recordset1 $_GET['totalRows_Recordset1'];
} else {
  
$all_Recordset1 mysql_query($query_Recordset1);
  
$totalRows_Recordset1 mysql_num_rows($all_Recordset1);
}
$totalPages_Recordset1 ceil($totalRows_Recordset1/$maxRows_Recordset1)-1;
$query_Recordset1 "SELECT * FROM archivos WHERE tipo_archivos = 'general' ORDER BY fecha_archivos DESC";
$Recordset1 mysql_query($query_Recordset1$demo1) or die(mysql_error());
$row_Recordset1 mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 mysql_num_rows($Recordset1);
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Subir imágenes al servidor con Dreamweaver</title>
<link rel="stylesheet" type="text/css" href="css/general.css">
</head>

<body>
<h1>Subir im&aacute;genes al servidor con Dreamweaver </h1>

<?php if(!empty($error)) echo $error?>
<form action="<?php echo $editFormAction?>" method="POST" enctype="multipart/form-data" name="form1" id="form1">
  <label for="imagen">nombre</label>
  <input type="text" name="nombre" id="nombre">
  <label for="imagen">imagen</label>
  <input type="file" name="imagen" id="imagen">
  <span class="enviar"><input type="submit" name="enviar" id="enviar" value="Enviar"></span>
  <input name="tipo" type="hidden" id="tipo" value="general">
  <input type="hidden" name="MM_insert" value="form1">
</form>
<p>&nbsp;</p>
<?php if ($totalRows_Recordset1 0) { // Show if recordset not empty ?>
  <img name="<?php echo $row_Recordset1['nombre_archivos']; ?>" src="imagenes/<?php echo $row_Recordset1['archivo_archivos']; ?>" width="300" height="300" alt="">
  <?php // Show if recordset not empty ?>
</body>
</html>
<?php
mysql_free_result
($Recordset1);
?>
Lo hice con este tutorial http://juaniquillo.com/blog/2010/07/...r-php-y-mysql/

El codigo lo tenia hecho hace aprox. 2 meses y funcionaba perfect! pero ahora lo pruebo en wampserver y cuando intento subir la imagen, me aparece un error 500 "Internal Server Error" me pasa tambien en el servidor remoto! es super raro porq antes funcionaba bien :S

Agradeceria si me pudieran decir si el tuto funciona bien o si en verdad ya no funciona o si mi codigo esta mal....

Gracias!
__________________
Quieres aprender a crear sitios web dinamicos, no te pierdas, usa Dreamweaver CS5
  #2 (permalink)  
Antiguo 21/01/2012, 19:59
Avatar de maycolalvarez
Colaborador
 
Fecha de Ingreso: julio-2008
Ubicación: Caracas
Mensajes: 12.120
Antigüedad: 15 años, 9 meses
Puntos: 1532
Respuesta: Problema con upload img

Verifique que el directorio de subida tenga permisos de escritura
Verifique si existe algún archivo .htaccess que pueda provocar el error en apache
__________________
¡Por favor!: usa el highlight para mostrar código
El que busca, encuentra...
  #3 (permalink)  
Antiguo 22/01/2012, 19:43
Avatar de cuervo699  
Fecha de Ingreso: febrero-2011
Mensajes: 104
Antigüedad: 13 años, 2 meses
Puntos: 7
Respuesta: Problema con upload img

Cita:
Iniciado por maycolalvarez Ver Mensaje
Verifique que el directorio de subida tenga permisos de escritura
Verifique si existe algún archivo .htaccess que pueda provocar el error en apache
El directorio si tiene permisos de subida :D y no se si el .htaccess pueda provocar el problema, como compruebo eso, Ademas ningun Upload ya sea de imagen o archivo, funcionan en mi localhost ni en el servidor :S
__________________
Quieres aprender a crear sitios web dinamicos, no te pierdas, usa Dreamweaver CS5

Etiquetas: html, imagenes, img, mysql, sql, upload, variables
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 02:49.