Ver Mensaje Individual
  #2 (permalink)  
Antiguo 04/08/2003, 10:33
Avatar de macabro
macabro
 
Fecha de Ingreso: enero-2003
Ubicación: venus >> ((_\
Mensajes: 254
Antigüedad: 21 años, 3 meses
Puntos: 1
Hola esto lo realize junto con un libro que tenia, y lo
modifique solo para las imagenes pero tambien
sirve para guardar archivos y luego descargar y eliminar
el mismo,

primero creas la tabla:

CREATE TABLE filestore (
ID INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
FileName VARCHAR(255) NOT NULL,
MimeType VARCHAR(50) NOT NULL,
Description VARCHAR (255) NOT NULL,
FileData MEDIUMBLOB
);

//Toma nota en el mediumblob puedes cambiarlo a blob o tinyblob
TINYBLOB=255B
BLOB=65KB
MEDIUMBLOB=16.7MB

Despues creamos el codigo php
prueba.php
Código PHP:

<?php

$dbcnx 
mysql_connect("localhost""usuario""password");
mysql_select_db("nombre_base _de_datos");

if ((
$action == "view" or $action == "dnld") and $id != "") {
    
    
// User is retrieving a file
    
$sql "SELECT FileName, MimeType, FileData
        FROM filestore WHERE ID = '$id'"
;
    
$result = @mysql_query($sql);
    if (!
$result) die("Database error: " mysql_error());
    
    
$filename mysql_result($result,0,"FileName");
    
$mimetype mysql_result($result,0,"MimeType");
    
$filedata mysql_result($result,0,"FileData");
    
    if (
$action == "dnld") {
        if (
strpos($HTTP_USER_AGENT,"MSIE"))
            
$mimetype "application/x-download";
        else
            
$mimetype "application/octet-stream";
    }

    
header("Content-disposition: filename=$filename");
    
header("Content-Type: $mimetype");
    
header("Content-Length: " strlen($filedata));

    echo(
$filedata);
    exit();
    
} elseif (
$action == "del" and $id != "") {
    
    
// User is deleting a file
    
$sql "DELETE FROM filestore WHERE ID = '$id'";
    
$ok = @mysql_query($sql);
    if (!
$ok) die("Database error: " mysql_error());

    
header("Location: $PHP_SELF");
    exit();

} elseif (
$action == "ulfile" and $uploadfile != "") {

    
// Bail out if the file isn’t really an upload.
    
if (!is_uploaded_file($uploadfile))
        die(
"$uploadfile is not an uploaded file!");
        
    
// Open file for binary reading ("rb")
    
$tempfile fopen($uploadfile,"rb");

    
// Read the entire file into memory using PHP's
    // filesize function to get the file size.
    
$filedata fread($tempfile,filesize($uploadfile));

    
// Prepare for database insert by adding backslashes
    // before special characters.
    
$filedata addslashes($filedata);

    
// Create the SQL query.
    
$sql "INSERT INTO filestore SET
    FileName = '$uploadfile_name',
    MimeType = '$uploadfile_type',
    Description = '$desc',
    FileData = '$filedata'"
;

    
// Perform the insert.
    
$ok = @mysql_query($sql);
    if (!
$ok) die("Database error storing file: " mysql_error());

    
header("Location: $PHP_SELF");
    exit();
}

// Default page view: lists stored files

$sql "SELECT ID, FileName, MimeType, Description
    FROM filestore"
;
$filelist = @mysql_query($sql)
    or die(
"Database error: " mysql_error());
?>
<html>
<head>
<title> Subir fotos y guardando</title>
</head>
<body>

<h1>Depósito De los Archivos </h1>
<font color="#FF0000">No se subira los archivos que no contenga imagenes</font>

<form action="<?=$PHP_SELF?>?action=ulfile" method="post"
    enctype="multipart/form-data">
<p>Elegir imagen:<br />
<input type="file" name="uploadfile" /></p>
<p>Descripcion:<br />
<input type="text" name="desc" maxlength="255" /></p>
<p><input type="submit" name="go" value="subir" /></p>
</form>

<p>Los siguientes archivos estan guardados en la base de datos:</p>
<table width="600" border="1" cellpadding="2" cellspacing="0">
<tr>
    <th align="center" cellpadding="2">Archivo</th>
    <th align="center" width="100%">Informacion</th>
</tr>
<?php

if (mysql_num_rows($filelist) > 0) {
    while (
$f mysql_fetch_array($filelist)) {
        
?>

<tr valign="top" >
    <td nowrap align="center">
        <a href="<?=$PHP_SELF?>?action=view&id=<?=$f['ID']?>">
         <img src="?action=view&id=<?=$f['ID']?>" height="100" width="100" border="0"> </a> <br> <?=$f['FileName']?>
    </td>
    <td nowrap width="100%"><b>Tipo</b> <?=$f['MimeType']?> <br>
    <b>Descripcion</b> <?=$f['Description']?><br><br><br>
    <b>Opciones:</b><br>
        [<a href="<?=$PHP_SELF?>?action=dnld&id=<?=$f['ID']?>">Descargar</a> |
        <a href="<?=$PHP_SELF?>?action=del&id=<?=$f['ID']?>"
            onClick="return confirm('Delete this file?');">Eliminar</a> |
<a href="<?=$PHP_SELF?>?action=view&id=<?=$f['ID']?>">Ver en grande</a>]
    </td>
</tr>

        <?php
    
}
} else {
    
?>
    <tr><td colspan="4" align="center">No hay Archivos!</td></tr>
    <?php
}
?>
</table>
</body>
</html>
Espero haberte ayudado en algo
saludos

Última edición por macabro; 04/08/2003 a las 10:37