Images and databases, big mess

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
Perfidus
Forum Contributor
Posts: 114
Joined: Sun Nov 02, 2003 9:54 pm

Images and databases, big mess

Post 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?
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Re: Images and databases, big mess

Post 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.
Perfidus
Forum Contributor
Posts: 114
Joined: Sun Nov 02, 2003 9:54 pm

Easy but do not work

Post 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
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post 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
Perfidus
Forum Contributor
Posts: 114
Joined: Sun Nov 02, 2003 9:54 pm

You are right!

Post 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.
Post Reply