Upload de foto con insertación en base de datos
Buenas,
he estado buscando y probando cosas y no he conseguido hacer nada decente, lo único tener errores sin cesar y sin poder solucionarlos y ya estoy harto.
Me gustaría saber si alguno de vosotros sabe un sitio o si puede poner por aqui un script o tutorial donde haya un formulario que recoja un campo texto, un campo para subir imagen y un campo de comentario (hasta aqui nada del otro mundo...), esos datos se insertarían en una tabla, es decir, el campo texto en un campo de la tabla, el nombre original de la imagen (imagen.jpg, imagen.gif, imagen.png) en otro campo y el comentario en otro campo.
No consigo que el nombre de la foto (con su extensión) se incluya en la tabla por más que lo veo, reveo y más, no dá fallo pero no inserta eso, el resto lo inserta, aunque a veces no inserta nada ni da error ni nada...
Tenía montado un script igualito a este que en un servidor me funciona maravillosamente y en este otro servidor da fallos, por eso pido si alguien sabe donde sacar un sistema parecido...
Gracias.
x-1
Más información.... con que haces el upload con php?
puedes poner el codigo?
lazyroy
Si el upload lo hago con PHP, he intentado poner el código pero me sale mal al ponerlo aquí, no sé por qué...
lazyroy
Este es el código PHP:
function getFileExtension($str) {
$i = strrpos($str,".");
if (!$i) { return ""; }
$l = strlen($str) - $i;
$ext = substr($str,$i+1,$l);
return $ext;
}
if ($REQUEST_METHOD == "POST")
{
$uploaddir = "/home/strato/www/go/www.goldamn.net/htdocs/web_goldamn/gd/img/img-team";
/*== get file extension (fn at bottom of script) ==*/
/*== checks to see if image file, if not do not allow upload ==*/
$pext = getFileExtension($imgfile_name);
$pext = strtolower($pext);
if (($pext != "jpg") && ($pext != "jpeg"))
{
print "<h1>ERROR</h1>Image Extension Unknown.<br>";
print "<p>Please upload only a JPEG image with the extension .jpg or .jpeg ONLY<br><br>";
print "The file you uploaded had the following extension: $pext</p>\n";
/*== delete uploaded file ==*/
unlink($imgfile);
exit();
}
//-- RE-SIZING UPLOADED IMAGE
/*== only resize if the image is larger than 200 x 150 ==*/
$imgsize = GetImageSize($imgfile);
/*== check size 0=width, 1=height ==*/
if (($imgsize[0] > 150) || ($imgsize[1] > 150))
{
$tmpimg = tempnam("/home/strato/www/go/www.goldamn.net/htdocs", "MKUP");
/*/kunden/web135
/*== RESIZE PROCESS
1. decompress jpeg image to pnm file (a raw image type)
2. scale pnm image
3. compress pnm file to jpeg image
==*/
/*== Step 1: djpeg decompresses jpeg to pnm ==*/
system("djpeg $imgfile >$tmpimg");
/*== Steps 2&3: scale image using pnmscale and then
pipe into cjpeg to output jpeg file ==*/
system("pnmscale -xy 150 150 $tmpimg | cjpeg -smoo 10 -qual 50 >$imgfile");
/*== remove temp image ==*/
unlink($tmpimg);
}
/*== setup final file location and name ==*/
/*== change spaces to underscores in filename ==*/
$final_filename = str_replace(" ", "_", $imgfile_name);
$newfile = $uploaddir . "/$final_filename";
/*== do extra security check to prevent malicious abuse==*/
if (is_uploaded_file($imgfile))
{
/*== move file to proper directory ==*/
if (!copy($imgfile,"$newfile"))
{
/*== if an error occurs the file could not
be written, read or possibly does not exist ==*/
print "Error Uploading File.";
exit();
}
}
/*== delete the temporary uploaded file ==*/
unlink($imgfile);
//ejecución de la sentencia SQL
$ssql = "INSERT INTO equipo (nombre,imgfile,comentario) VALUES ('$_POST[nombre]','$_POST[final_filename]','$_POST[comentario]' )";
if (mysql_query($ssql)){
//si se consiguio insertar la noticia
echo "<h1>MEMBER OF THE TEAM ADDED SUCCESSFULLY!!</h1>";
}else{
//no se insertó la noticia correctamente
echo "<h1>ERROR! - The member of the team has not been added. Please, try again.</h1>";
echo mysql_error();
}
//close the sql connection.
mysql_close($conn);
}
?>
Este es el código del form:
<form>
<strong>Name of the team member</strong> <br> <input><br><br>
<input>
ONLY <strong>.jpg</strong> or <strong>.jpeg</strong> images are allowed. <br>
DO NOT UPLOAD IMAGES LIKE <strong>.gif</strong>, <strong>.png</strong> or <strong>.bmp</strong> <br>
DO NOT UPLOAD IMAGES OF SIZE HIGHER THAN <strong>150Kb</strong>.<br>
<strong>Upload Image</strong><br>
<input><br><br>
<strong>Description / Text</strong> <br> <textarea></textarea><br>
<input>
</form>
No sé, a ver si hay algo raro, pero...
zigotica
¿no será que le falta poner enctype="multipart/form-data" al form?
x-1
donde especificas el valor de la variable: $imgfile_name ?
lazyroy
zigotica
¿no será que le falta poner enctype="multipart/form-data" al form?
Eso lo tengo puesto pero no sé por que coño no ha salido... joder...