class.upload.php is a powerful and mature PHP class to manage uploaded files, and manipulate images in many ways. The script is available under a GPL license.
My code is like this, but how to get the $imagem_nome that is the image name saved?
$x = mysql_query("INSERT INTO foto_galeria (id_galeria, foto, comentario) VALUES ('$id_galeria', '$imagem_nome', '$comentario')");
$handle->Process('./fotos_galeria/');
if ($handle->processed) {
// everything was fine !
echo ' file uploaded with success';
echo round(filesize($handle->file_dst_pathname)/256)/4 . 'KB';
echo ' link to the file just uploaded: file_dst_name';
}
$handle->Process('./fotos_galeria/');
if ($handle->processed) {
// everything was fine !
echo ' file uploaded with success';
echo round(filesize($handle->file_dst_pathname)/256)/4 . 'KB';
echo ' link to the file just uploaded: file_dst_name';
$x = mysql_query("INSERT INTO foto_galeria (id_galeria, foto, comentario) VALUES ('$id_galeria', '".$handle->file_dst_name."', '$comentario')");
}
This is a bit beyond the scope of this forum, but you can do like this for instance:
include('class.upload.php');
$files = array();
foreach ($_FILES['userfile'] as $k => $l) {
foreach ($l as $i => $v) {
if (!array_key_exists($i, $files))
$files[$i] = array();
$files[$i][$k] = $v;
}
}
// create an array here to hold file names
$uploaded = array();
foreach ($files as $file) {
$handle = new Upload($file);
if ($handle->uploaded) {
// create a little array for this set of pictures
$this_upload = array();
$handle->file_name_body_add = '_peque';
$handle->file_auto_rename = true;
$handle->image_resize = true;
$handle->image_x = 100;
$handle->image_ratio_y = true;
$handle->Process("./test/");
if ($handle->processed) {
echo $handle->file_dst_name;
// store the large image filename
$this_upload['large'] = $handle->file_dst_name;
} else {
echo 'error : ' . $handle->error;
}
$handle->file_auto_rename = true;
$handle->image_resize = false;
$handle->image_x = 100;
$handle->image_ratio_y = true;
$handle->Process("./test/");
if ($handle->processed) {
echo $handle->file_dst_name;
// store the small image filename
$this_upload['small'] = $handle->file_dst_name;
$handle->clean();
} else {
echo 'error : ' . $handle->error;
}
// add this set of pictures to the main array
$uploaded[] = $this_upload;
}
}
// now, you have all your picture names in $uploaded
// do your database insert like this for instance
$sql = "INSERT INTO photos (id, pic1, pic1_small, pic2, pic2_small....) ";
$sql .= "VALUES ('$id', '".$uploaded[1]['large']."', '".$uploaded[1]['small']."', '".$uploaded[2]['large']."', '".$uploaded[2]['small']."')");
$x = mysql_query($sql);
Yes, the files are only dealt with as files. I think I can add in the class a way to output the content of the image into a variable, so that it can be stored as a blob, etc...
But generally, a database is not exactly the best place to store images.
Whats hapenning?
Thanks a lot.
Rolan.
Thanks again!
it looks like you are saving the image name only to mysql.
is there any way of saving the actual image in the mysql database, as a blob?
thanks.
hugo
But generally, a database is not exactly the best place to store images.
I add this to my to-do list.
Yes. Probably I will save images outside of mysql.