Multiple image upload filenames

See all posts Reply

Multiple image upload filenames new!
by Matt, 14 years, 8 months ago
Hi,

I have successfully implemented this class to upload 6 images as per the examples given in the FAQ on this site and also storing the correct filename in my database table. My question relates to an update record page that I have, that allows for the changing of images associated with a record. As each image name is stored in a separate field in the table I am having difficulty updating the correct field with the correct image name. This I believe is down to the fact that not all images are being replaced and therefore the $_FILES array has empty records depending on which images were selected to be changed. Having carried out some testing on the contents of the arrays during this process, I am still none the wiser so I am hoping that someone maybe able to point me in the right direction.

Here is my code:
// 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_safe_name 	= true;
    $handle->file_auto_rename   = true;
	$handle->file_max_size 		= '512000';
	$handle->allowed			= array('image/jpeg', 'image/jpg');
    $handle->Process($dir_dest);
    
    if ($handle->processed) {
      echo $handle->file_dst_name;
      // store the image filename  
      $this_upload['image'] = $handle->file_dst_name;
    } else {
      echo 'error : ' . $handle->error;
    }
    
    // add this set of pictures to the main array
    $uploaded[] = $this_upload;
  }
}

Many thanks for your help.

MattReply
Re: Multiple image upload filenames new!
by Matt, 14 years, 8 months ago
Apologies, I missed off one part of the code. Thanks

//initialize new images to be uploaded
$files = array();
foreach ($_FILES['image'] 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_safe_name 	= true;
    $handle->file_auto_rename   = true;
	$handle->file_max_size 		= '512000';
	$handle->allowed			= array('image/jpeg', 'image/jpg');
    $handle->Process($dir_dest);
    
    if ($handle->processed) {
      echo $handle->file_dst_name;
      // store the image filename  
      $this_upload['image'] = $handle->file_dst_name;
    } else {
      echo 'error : ' . $handle->error;
    }
    
    // add this set of pictures to the main array
    $uploaded[] = $this_upload;
  }
}
Reply
Re: Multiple image upload filenames new!
by colin, 14 years, 8 months ago
This is not really an upload problem. Check the keys in the $files array, in order to know which image has to be updated.Reply
Re: Multiple image upload filenames new!
by Matt, 14 years, 8 months ago
Hi Colin,

I understand that this is not directly related to the problem with the upload class itself, and I can see the logic of checking the initial $_FILES array to see which images have been selected to update. I was however hoping on the off chance that someone might be able give me an idea of how to keep track of filenames so I can match up the filename with the field.

Thanks in advanceReply
Re: Multiple image upload filenames new!
by José, 14 years, 8 months ago
Hello!
I got it to upload multiple files.

Now I would like to know how to add the database the name of each file separately?

Example:
INSERT INTO table VALUES ( '$file0', '$file1', '$file2', '$file3')

Thanks.Reply
Re: Multiple image upload filenames new!
by colin, 14 years, 8 months ago
This is not a question pertaining to the class. Please see the PHP website, or other websites offering tutorials on how to store values in a database.Reply
Re: Multiple image upload filenames new!
by José, 14 years, 8 months ago
I think have not explained well ...
I managed to do both upload and insert images in the database, but I could not retrieve the name of the 3 files provided by the input file.Reply
Re: Multiple image upload filenames new!
by colin, 14 years, 8 months ago
After calling process(), you can read the following variables:

- file_dst_path: Destination file path
- file_dst_name_body: Destination file name body
- file_dst_name_ext: Destination file extension
- file_dst_name: Destination file name
- file_dst_pathname: Destination file complete path and name

Use these to store the image names and paths to your database.Reply