Page 1 of 1

Images and databases, big mess

Posted: Mon Nov 03, 2003 9:27 am
by Perfidus
I would like to create a file upload button that only accepts JPG, those images have to go to a folder and be renamed, the same name have to be stored on my database to be able to call them.
I do not have any idea on how to begin with this, can somebody give any hint on were should I start to search ?
Any tutorials?

Re: Images and databases, big mess

Posted: Mon Nov 03, 2003 9:31 am
by JAM
http://se.php.net/features.file-upload

[php_man]substr[/php_man] can be used to see if the file ends with .jpg or not (easy and safe enough perhaps).

If you can manage to get this working, inserting the name into the database would be solved along the road.

Hope that helps some.

Easy but do not work

Posted: Mon Nov 03, 2003 10:33 am
by Perfidus
This way seems to be a good beginning, I will make it more complicated.

Code: Select all

<?php
$path = "http://www.calabaza.com/fotos/";
$max_size = 200000;
if (!isset($HTTP_POST_FILES['userfile'])) exit;
if (is_uploaded_file($HTTP_POST_FILES['userfile']['tmp_name'])) {
if ($HTTP_POST_FILES['userfile']['size']>$max_size) { echo "La foto es demasiado grande\n"; 
exit; }
if (($HTTP_POST_FILES['userfile']['type']=="image/gif") || ($HTTP_POST_FILES['userfile']['type']=="image/pjpeg") || ($HTTP_POST_FILES['userfile']['type']=="image/jpeg")) {
if (file_exists($path . $HTTP_POST_FILES['userfile']['name'])) { echo "Este nombre de archivo ya existe\n"; exit; }
$res = copy($HTTP_POST_FILES['userfile']['tmp_name'], $path .
$HTTP_POST_FILES['userfile']['name']);
if (!$res) { echo "Envio de imagen fallido<br>\n"; exit; } else { echo "La imagen ha sido enviada<br>\n"; }
echo "Nombre de la foto: ".$HTTP_POST_FILES['userfile']['name']."\n";
echo "Tamaño de la foto: ".$HTTP_POST_FILES['userfile']['size']." bytes\n";
echo "Tipo de archivo: ".$HTTP_POST_FILES['userfile']['type']."\n";
} else { echo "Este archivo no es un archivo JPG o GIF\n"; 
exit; }
}
?>
The problem is that I get this message:
Warning: copy(http://www.calabaza.com/fotos/DSCN1762.JPG): failed to open stream: HTTP wrapper does not support writeable connections. in /chs/p1/costa4seasons.com/home/html/imageupload.php on line 14
Envio de imagen fallido

Posted: Mon Nov 03, 2003 10:49 am
by JAM
Note: As of PHP 4.3.0, both source and dest may be URLs if the "fopen wrappers" have been enabled. See fopen() for more details.
...and...
http://se.php.net/manual/en/wrappers.php#wrappers.http

You are right!

Posted: Mon Nov 03, 2003 11:21 am
by Perfidus
Yeah! I have substitute the absolute address by a local one and it works, server do not accepted absolute routes.

Code: Select all

<?php
$path = "fotos/";
$max_size = 200000;
if (!isset($HTTP_POST_FILES['userfile'])) exit;
if (is_uploaded_file($HTTP_POST_FILES['userfile']['tmp_name'])) {
if ($HTTP_POST_FILES['userfile']['size']>$max_size) { echo "La foto es demasiado grande\n"; 
exit; }
if (($HTTP_POST_FILES['userfile']['type']=="image/gif") || ($HTTP_POST_FILES['userfile']['type']=="image/pjpeg") || ($HTTP_POST_FILES['userfile']['type']=="image/jpeg")) {
if (file_exists($path . $HTTP_POST_FILES['userfile']['name'])) { echo "Este nombre de archivo ya existe\n"; exit; }
$res = copy($HTTP_POST_FILES['userfile']['tmp_name'], $path .
$HTTP_POST_FILES['userfile']['name']);
if (!$res) { echo "Envio de imagen fallido<br>\n"; exit; } else { echo "La imagen ha sido enviada<br>\n"; }
echo "Nombre de la foto: ".$HTTP_POST_FILES['userfile']['name']."\n";
echo "Tamaño de la foto: ".$HTTP_POST_FILES['userfile']['size']." bytes\n";
echo "Tipo de archivo: ".$HTTP_POST_FILES['userfile']['type']."\n";
} else { echo "Este archivo no es un archivo JPG o GIF\n"; 
exit; }
}
?>
Now I still have to find a way to rename the picture the way I want and to place name on database. I think I have some clues, will post.