Mysql - Database

See all posts Reply

Mysql - Database new!
by Rolan, 17 years, 4 months ago
When i save to the BD the photo field came like: /tmp/phpXHcxGT instead of the name of the photo.

Whats hapenning?Reply
Re: Mysql - Database new!
by colin, 17 years, 4 months ago
You can read some class variables after calling process(). The following ones can be used to get the filename details:
$foo->file_dst_name
$foo->file_dst_name_body
$foo->file_dst_name_ext
$foo->file_dst_path
$foo->file_dst_pathname
Reply
Re: Mysql - Database new!
by Rolan, 17 years, 4 months ago
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';
}
Reply
Re: Mysql - Database new!
by colin, 17 years, 4 months ago
Mmmh, like this, no?

$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')");
}
Reply
Re: Mysql - Database new!
by Julian Delgado, 17 years, 4 months ago
This works excellent if you want to upload one file and save one file_dst_name, what about multiple files names to the database?Reply
Re: Mysql - Database new!
by colin, 17 years, 4 months ago
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);
Reply
Re: Mysql - Database new!
by Rolan, 17 years, 4 months ago
Brilliant guys it works.
Thanks a lot.
Rolan.Reply
Re: Mysql - Database new!
by Julian Delgado, 17 years, 4 months ago
Thanks Colin, excellent suggestion. Worked excellent! Only 1 change. On the mysql query the first value must start with $uploaded[0]['large'].

Thanks again!Reply
Re: Mysql - Database new!
by hugo, 17 years, 3 months ago
hello,

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.

hugoReply
Re: Mysql - Database new!
by colin, 17 years, 3 months ago
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.

I add this to my to-do list.Reply
Re: Mysql - Database new!
by hugo, 17 years, 3 months ago
Thank you very much.
Yes. Probably I will save images outside of mysql.Reply