Click to See Complete Forum and Search --> : I'm getting crazy with uploading images


Perfidus
11-04-2003, 01:14 PM
I'm working in a real state project, I would like them to upload pictures and description of apartment.
The description comes with a form plenty of questions, those question are stored in a DB, the DB itself generates a number for every upload. This is nwhat I have reach by the moment.
What I need is a form like the one I attach, able to rename the pictures an store them in a directory and able also to store this new name in one of the cells related to the apartment in a why that I can call the picture afterwords.
For example:

A user is uploading data about an apartment, the DB gives this apartment number 5678, when finished with the data wants to upload 3 images, (maximun 5) those images are renamed
5678-1.jpg
5678-2.jpg
5678-3.jpg
and those names are also stored in the table Photos in the database, in the cells photo1, photo2, photo3.
Clear enough?
Maybe a little messy, but I'm getting realy crazy!

<?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; }
}
?>

pyro
11-04-2003, 02:06 PM
What part specifically is giving you trouble?

Perfidus
11-04-2003, 02:10 PM
I know how to upload to directory, but not how to rename and neither how to store this new name in database.:(

pyro
11-04-2003, 02:27 PM
The renaming will be done in the copy function, take a look at what the manual says about how copy works:

From http://us2.php.net/manual/en/function.copy.php
bool copy ( string source, string dest)

From that, we see that the first parameter is the source, and then next is the destination. By changing the part in bold below, you can change the name:

$res = copy($HTTP_POST_FILES['userfile']['tmp_name'], $path .
$HTTP_POST_FILES['userfile']['name']);

As far as writing this name to the database, what kind of database are you using? MySQL?

Perfidus
11-04-2003, 02:29 PM
Yes, I'm using a mysql database.

pyro
11-04-2003, 02:34 PM
Then something like this should work to write it in:

$db = @mysql_connect('localhost','username','password') or die("Could not connect because: ".mysql_error());
mysql_select_db('dbname');

$query = "INSERT INTO `tablename` VALUES ('','imagename')"; //first left blank for id field
mysql_query($query);

Perfidus
11-04-2003, 02:48 PM
I have tried this stupid step to change the name, firts I was asking how many rows there still on the table, whan I know I add 5001, then it will start over 5000 and also add 1 because this will be the row X+1 when added.
With this number stored in a variable I was tryng to change the name using the bold part of the code you told me, but I'm afraid I did it very stupidly.
Of course: It doesn't work...

<?php
$link = mysql_connect('mysql.gestionar.info', 'aa1126', 'herrdoctor7');
mysql_select_db("aa3968",$link);
$result = mysql_query("SELECT * FROM DatosInmueble", $link);
$num_rows = mysql_num_rows($result);
$refnum = ("$num_rows"+5001);
$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']['$refnum']);
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; }
}
?>

pyro
11-04-2003, 03:09 PM
This part:

$HTTP_POST_FILES['userfile']['$refnum']should just be:

$refnumAlso, $refnum is going to equal 5001 more than the number of rows in the DB. Is that what you wanted?

Perfidus
11-04-2003, 03:15 PM
Something like this?
This causes an error

$res = copy($HTTP_POST_FILES['userfile']['tmp_name'], $path . '$refnum';

pyro
11-04-2003, 03:24 PM
Try it like this:

$res = copy($HTTP_POST_FILES['userfile']['tmp_name'], $path.$refnum);Also, if you are using PHP 4.2.0 or greater, you can use the $_FILES (http://us4.php.net/manual/en/reserved.variables.php#reserved.variables.files) superglobal, rather than the depreciated $HTTP_POST_VARS.

Perfidus
11-04-2003, 07:13 PM
I'm afraid it doesn't work, this is the error I get.

Parse error: parse error in /chs/p1/costa4seasons.com/home/html/imageupload3.php on line 16

<?php
$link = mysql_connect('mysql.gestionar.info', 'tomate', 'pepino');
mysql_select_db("aa3968",$link);
$result = mysql_query("SELECT * FROM DatosInmueble", $link);
$num_rows = mysql_num_rows($result);
$refnum = ("$num_rows"+5001);
$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.$refnum);
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; }
}
?>