0

I have a photo album, with a 'photos' field in database which has serialized data and is base 64 encoded. I can upload up to 21 photos at the same time for the album (multiple uploading). When trying to add new photos to the album on edit mode, I can't get the new $_FILES array to merge with the old array in the database. I want to update only the values that I have changed. So let's say I already had 2 images inside my album, I would like to add a 3rd image without losing the other 2 images. I know I need to use unset() to delete empty values but I think i'm not doing it correctly. Here's my code:

if (isset($_REQUEST['action']) && $_REQUEST['action'] == 'edit') {
        //select photo album and get photos and descriptions to be merged with new.
        $album_id           = $_REQUEST['album_id'];    
        $old_photos         = null;
        $old_descriptions   = null;

        $getAlbumQ = mysql_query("select * from albums where id='$album_id'");

        while ($old_album = mysql_fetch_array($getAlbumQ)) {
            $old_photos         = unserialize(base64_decode($old_album['photos']));
            $old_descriptions   = unserialize(base64_decode($old_album['descriptions']));
        }

        if (isset($_POST['album_name']) && isset($_POST['desc'])) {
            $name       = $_POST['album_name'];
            $desc       = $_POST['desc'];
            $idesc      = array();
            $target_path = "../uploads/albums/";



            foreach ($_FILES as $k => $v) {
                //first upload photos
                $path = $target_path . basename($v['name']); 
                if(move_uploaded_file($v['tmp_name'], $path)) {

                    $hasUpload = true;
                }   
            }



            for ($j = 1; $j < 21; $j++) {
                    $img_index  = $j;
                    $img_desc   = $_POST['desc_' . $img_index];
                    $asdf   = $_FILES['image_' . $img_index]['name'];

                    array_push($idesc, $img_desc);          

            }

            foreach( $_FILES['images']['name'] as $key => $value ) { 
                   if( empty($value) ) { 
                                       unset( $_FILES['images']['name'][$key] ); 
                    } 
            } 


            for ($i = 1; $i < 21; $i++) {


            if($_FILES['image_'.$i]['name']!= '')
            {
            $hasUpload = true;

            $presults       = array_merge($old_photos, $_FILES); //THE PROBLEM WITH MERGING ARRAYS OCCURS HERE
            $dresults       = array_merge($old_descriptions, $idesc);

            $images         = base64_encode(serialize($presults));
            $descriptions   = base64_encode(serialize($dresults));
            $posted         = date("Y-m-d H:i:s");
            }
            else {
                $hasUpload = false;

            $presults       = $old_photos;
            $dresults       = $idesc;

            $images         = base64_encode(serialize($presults));
            $descriptions   = base64_encode(serialize($dresults));
            $posted         = date("Y-m-d H:i:s");
            }
        }

        }
    }
7
  • 1
    Why do so many people seem intent on saving images as binary data in a db? Commented Oct 31, 2011 at 12:14
  • @MikeB, not saying it's necessarily the best approach (and not one I ever opt for) but it does make backing up and migration one step shorter :-) Commented Oct 31, 2011 at 12:16
  • @MikeB Easier backups in one sql file. As long as you cache outputs correctly, why not? Commented Oct 31, 2011 at 12:18
  • @Ashley It's not like you're saving a step in your backup process by storing them in the db. You still have to backup files at some point (even if it's the mysql file). Commented Oct 31, 2011 at 12:33
  • Depends how you do the backup, I suppose. Commented Oct 31, 2011 at 13:55

2 Answers 2

1

I tried something similar to what Muu suggested. Combined it with another piece of code of a custom merge function found here http://keithdevens.com/weblog/archive/2003/Mar/30/MergeTwoArrays and it worked fine! Thanks for your help

Just needed to add the following lines of code outside the for ($i = 1; $i < 21; $i++) loop:

//delete empty values for $_FILES array

foreach ($_FILES as $key => $value) {

                    foreach ($value as $k => $v) {
                         if ($k=='name' && $v!='') {
                             break;
                         } else {
                             unset($_FILES[$key]);
                         }
                   }
                 }

//custom array merge function 

function merge(&$a, &$b){
   $keys = array_keys($a);
   foreach($keys as $key){
       if(isset($b[$key])){
           if(is_array($a[$key]) and is_array($b[$key])){
               merge($a[$key],$b[$key]);
           }else{
               $a[$key] = $b[$key];
           }
       }
   }
   $keys = array_keys($b);
   foreach($keys as $key){
       if(!isset($a[$key])){
           $a[$key] = $b[$key];
       }
   }
}

then instead of array_merge(), I used merge() inside the for ($i = 1; $i < 21; $i++) loop:

 $presults       = merge($old_photos, $_FILES);
Sign up to request clarification or add additional context in comments.

Comments

0

Instead of removing the empty values from the array, why not just ignore them? For example:

$nonEmptyArray = array();
foreach ($_FILES as $k => $v) {
    if (!$v) {
        continue;
    }
    $nonEmptyArray[$k] = $v;
}
$_FILES = $nonEmptyArray;

10 Comments

You mean somehing like this? foreach( $_FILES['image_'.$img_index]['name'] as $key => $value ) { if(empty($value)) { unset($_FILES ['image_'.$img_index]['name'][$key]); } } Where would this code need to go? Thanks
No, my point is that there's no need to actually unset those parts of the array - just ignore them. That's what 'continue' does - it skips past the rest of the code inside the loop and goes to the next item. All you need to do is check to see if that bit of the array is empty or not and if it is ignore it and go to the next item in the array.
Does this part of the code do that? if($_FILES['image_'.$i]['name']!= '') { $hasUpload = true; $presults = array_merge($old_photos,$_FILES); $dresults = array_merge($old_descriptions, $idesc);
Yep, didn't even notice that. If you've got that in there, why are you looking to remove the empty values from the $_FILES array?
Because I dont want them to replace/erase any values that might exist inside the old array. I just want to replace those array elements that need to be updated. For example, if I want to edit an already created album with 2 photos in it, I want to upload a 3rd photo and at the same time keep the other 2 photos unchanged after editing the album.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.