Foros del Web » Programando para Internet » PHP »

insertar noticia y subir imagen....

Estas en el tema de insertar noticia y subir imagen.... en el foro de PHP en Foros del Web. Código PHP: <?php $MAX_SIZE  =  2000000 ;                                        $FILE_EXTS   = array( '.zip' , '.jpg' , '.png' , '.gif' , '.doc' );  $DELETABLE   =  true ;                                ...
  #1 (permalink)  
Antiguo 09/04/2007, 14:40
 
Fecha de Ingreso: junio-2003
Ubicación: Frente al monitor
Mensajes: 508
Antigüedad: 20 años, 10 meses
Puntos: 0
insertar noticia y subir imagen....

Código PHP:
<?php

$MAX_SIZE 
2000000;
                            
         
$FILE_EXTS  = array('.zip','.jpg','.png','.gif','.doc'); 


$DELETABLE  true;                               



$site_name $_SERVER['HTTP_HOST'];
$url_dir "http://".$_SERVER['HTTP_HOST'].dirname($_SERVER['PHP_SELF']);
$url_this =  "http://".$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'];

$upload_dir "files/";
$upload_url $url_dir."/files/";
$message ="";


if (!
is_dir("files")) {
  if (!
mkdir($upload_dir))
      die (
"upload_files directory doesn't exist and creation failed");
  if (!
chmod($upload_dir,0755))
      die (
"change permission to 755 failed.");
}


if (
$_REQUEST[del] && $DELETABLE)  {
  
$resource fopen("log.txt","a");
  
fwrite($resource,date("Ymd h:i:s")."DELETE - $_SERVER[REMOTE_ADDR]"."$_REQUEST[del]\n");
  
fclose($resource);
  
  if (
strpos($_REQUEST[del],"/.")>0);                  //possible hacking
  
else if (strpos($_REQUEST[del],$upload_dir) === false); //possible hacking
  
else if (substr($_REQUEST[del],0,6)==$upload_dir) {
    
unlink($_REQUEST[del]);
    print 
"<script>window.location.href='$url_this?message=deleted successfully'</script>";
  }
}
else if (
$_FILES['userfile']) {
  
$resource fopen("log.txt","a");
  
fwrite($resource,date("Ymd h:i:s")."UPLOAD - $_SERVER[REMOTE_ADDR]"
            
.$_FILES['userfile']['name']." "
            
.$_FILES['userfile']['type']."\n");
  
fclose($resource);

  
$file_type $_FILES['userfile']['type']; 
  
$file_name $_FILES['userfile']['name'];
  
$file_ext strtolower(substr($file_name,strrpos($file_name,".")));

  
//File Size Check
  
if ( $_FILES['userfile']['size'] > $MAX_SIZE
     
$message "The file size is over 2MB.";
  
//File Extension Check
  
else if (!in_array($file_ext$FILE_EXTS))
     
$message "Sorry, $file_name($file_type) is not allowed to be uploaded.";
  else
     
$message do_upload($upload_dir$upload_url);
  
  print 
"<script>window.location.href='$url_this?message=$message'</script>";
}
else if (!
$_FILES['userfile']);
else 
    
$message "Invalid File Specified.";


$handle=opendir($upload_dir);
$filelist "";
while (
$file readdir($handle)) {
   if(!
is_dir($file) && !is_link($file)) {
      
$filelist .= "<a href='$upload_dir$file'>".$file."</a> - URL: <b>$upload_url$file</b>";
      if (
$DELETABLE)
        
      
$filelist .= " Added at  ".date("d-m H:i"filemtime($upload_dir.$file))
                   .
"";
$filelist .= " <a style='text-decoration:none; font-weight:bold'  href='?del=$upload_dir".urlencode($file)."' title='delete'>x</a>";
      
$filelist .="<br>";
   }
}

function 
do_upload($upload_dir$upload_url) {

    
$temp_name $_FILES['userfile']['tmp_name'];
    
$file_name $_FILES['userfile']['name']; 
  
$file_name str_replace("\\","",$file_name);
  
$file_name str_replace("'","",$file_name);
    
$file_path $upload_dir.$file_name;

    
//File Name Check
  
if ( $file_name =="") { 
      
$message "Invalid File Name Specified";
      return 
$message;
  }

  
$result  =  move_uploaded_file($temp_name$file_path);
  if (!
chmod($file_path,0777))
       
$message "change permission to 777 failed.";
  else
    
$message = ($result)?"$file_name was uploaded successfully." :
               
"Something is wrong with uploading the file.";
  return 
$message;
}

?>

<font color=red><?=$_REQUEST[message]?></font>
<br>
<form name="upload" id="upload" ENCTYPE="multipart/form-data" method="post">
Upload File <input type="file" id="userfile" name="userfile">
<input type="submit" name="upload" value="Upload">
</form>



Esto es un upload. Lo pegue en otro formulario q lleva contenido con unos cmapos, quiero subir la imagen y q todo el contenido se me guarde en la base de datos, pero cuando envio el archivo me dice q el campo donde ira la imagen no puede estar vacio, o sea no hace en envio, como puedo arreglarlo para q me grabe y me suba el contenido a la base de datos?
__________________
Tengo una duda...
  #2 (permalink)  
Antiguo 09/04/2007, 14:51
Avatar de kaninox  
Fecha de Ingreso: septiembre-2005
Ubicación: In my House
Mensajes: 3.597
Antigüedad: 18 años, 7 meses
Puntos: 49
Re: insertar noticia y subir imagen....

muestrame tu insert :)
__________________
Gokuh Salvo al mundo. PUNTO!!!!
  #3 (permalink)  
Antiguo 10/04/2007, 07:33
 
Fecha de Ingreso: junio-2003
Ubicación: Frente al monitor
Mensajes: 508
Antigüedad: 20 años, 10 meses
Puntos: 0
Re: insertar noticia y subir imagen....

Código PHP:
<?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"] == "form1")) {
  
$insertSQL sprintf("INSERT INTO contenido (id, titulo, fecha, intro, foto, texto, categoria_id) VALUES (%s, %s, %s, %s, %s, %s, %s)",
                       
GetSQLValueString($_POST['id'], "int"),
                       
GetSQLValueString($_POST['titulo'], "text"),
                       
GetSQLValueString($_POST['fecha'], "date"),
                       
GetSQLValueString($_POST['intro'], "text"),
                       
GetSQLValueString($_POST['foto'], "text"),
                       
GetSQLValueString($_POST['texto'], "text"),
                       
GetSQLValueString($_POST['categoria_id'], "int"));

  
mysql_select_db($database_ctneditor$ctneditor);
  
$Result1 mysql_query($insertSQL$ctneditor) or die(mysql_error());

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

mysql_select_db($database_ctneditor$ctneditor);
$query_getcat "SELECT * FROM categorias";
$getcat mysql_query($query_getcat$ctneditor) or die(mysql_error());
$row_getcat mysql_fetch_assoc($getcat);
$totalRows_getcat mysql_num_rows($getcat);
?>[/B]

Este es el insert q tengo, o si algiuien me dice como inserta la foto y q sea la adecuada con el contenido...
__________________
Tengo una duda...
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 23:49.