Multiple File Uploads

See all posts Reply

Multiple File Uploads new!
by Roger, 13 years, 2 months ago
Hi,

Wonderful class!

I've been working on implementing it into my site and I've run into a problem. Multiple uploads. I've read the FAQ's and other posts on the forum, but I'm still lost. Here is what is going on...

I have 6 image fields on my form. Each one has been tested and they all work independently. Each has its own unique name. When processed, the class works fine and writes the file name to my database. Each image has it own row within my "users" table.

However, existing data within the db gets deleted once I upload an image to a secondary. ie.

1. Session one - the user logs in and uploads a profile picture.
2. Session two - the user logs in and uploads an image to a different row
3. The users original "profile" picture filename (in the db) gets deleted and the new one added to the appropriate row.

From what I understand, the loop is only needed if I'm uploading multiple files with the same name, correct?

Here is my form code...
<input type="file" name="pPic" />
<input type="file" name="Album1" />
<input type="file" name="Album2" />
<input type="file" name="Album3" />
<input type="file" name="Album4" />
<input type="file" name="Album5" />
<input type="file" name="Album6" />

and
if (POST_SUBMITTED!='' && $currentUser->getId() == $userid) {
$profilePic = new upload($_FILES['pPic']);
//checking to see if the users image folder exists. If not, create it.
$picroot = '../uImages/' . '$userid';
$result = file_exists($picroot);
if ($result === 0) {
  mkdir ("../uImages". "/" .$userid, 0777, TRUE); 
}
if ($profilePic->uploaded) {
  //$this_upload_ext = array();
  //$profilePic->this_upload_ext		= true;
  $profilePic->mime_check = true;
  $profilePic->allowed = array('image/*');
  $profilePic->file_new_name_body   = rand();
  $profilePic->image_resize         = true;
  $profilePic->image_x              = 100;
  $profilePic->image_ratio_y        = true;
  $pPic_upload = $profilePic->file_dst_name;
  $this_upload_ext = $profilePic->file_dst_name_ext;
  $profilePic->process("../uImages" . "/" .$userid);
}
if ($profilePic->processed) {
  //echo 'image resized';
  $pPic_upload = $profilePic->file_dst_name;
  $this_upload_ext = $profilePic->file_dst_name_ext;
  $profilePic->clean();
} else {
  $tplVars['error'] = T_($profilePic->error);
  //echo 'error : ' . $profilePic->error;
}
//This processes and uploads the photo album image #1
$Album1 = new upload($_FILES['Album1']);
if ($Album1->uploaded) {
  //$this_upload_ext = array();
  //$Album1->this_upload_ext		= true;
  $Album1->mime_check = true;
  $Album1->allowed = array('image/*');
  $Album1->file_new_name_body   = rand();
  $Album1->image_resize         = true;
  $Album1->image_x              = 100;
  $Album1->image_ratio_y        = true;
  $album1_upload = $Album1->file_dst_name;
  $this_upload_ext = $Album1->file_dst_name_ext;
  $Album1->process("../uImages" . "/" .$userid);
}
if ($Album1->processed) {
  //echo 'image resized';
  $album1_upload = $Album1->file_dst_name;
  $this_upload_ext = $Album1->file_dst_name_ext;
  $Album1->clean();
} else {
  $tplVars['error'] = T_($Album1->error);
  //echo 'error : ' . $Album1->error;
}

Thanks!Reply
Re: Multiple File Uploads new!
by colin, 13 years, 2 months ago
> From what I understand, the loop is only needed if I'm uploading
> multiple files with the same name, correct?


Yes. In your case, you just have a bunch of different fields, you don't have a multiple upload par se.

The rest of your problem (entries get deleted in the database) is not related to the class. You must have a problem of logic in your code. I would also recommend not using rand() to generate a filename, as you may have collisions (even though the class will rename files of the filemane already exists)Reply
Re: Multiple File Uploads new!
by Roger, 13 years, 2 months ago
Great, thanks!

I'll dig deeper into my code.

What type of collisions are you talking about with rand()? I'm not sure I understand you correctly, are you saying the class checks for existing file names, and if a match is found, it renames the new file? If I need a random file name, what method do you recommend?Reply