Multiple file upload using a ZIP archive Multiple file upload using a ZIP archive
 

News:

cpg1.5.48 Security release - upgrade mandatory!
The Coppermine development team is releasing a security update for Coppermine in order to counter a recently discovered vulnerability. It is important that all users who run version cpg1.5.46 or older update to this latest version as soon as possible.
[more]

Main Menu

Multiple file upload using a ZIP archive

Started by Japp, March 04, 2004, 03:53:37 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Japp

I have made it possible to upload a zip-file of multiple images and add these to a gallery. I have tested the function now for a couple of weeks and it seems to work, have tested up to 250 images in a zip-file.

If some one is interested in this feature i can upload the modified .php files to a server.

To use it you will need zip support in php: http://se.php.net/manual/en/ref.zip.php

Edit: You use the same interface as if you are uploading just one picture, no FTP or any special program needed..

Joachim Müller

Japp has sent the files along - I had a hard time with a diff viewer, but here's what has to be changed...
  • edit db_input.php
find                                if ($matches[2] == '' || !stristr($CONFIG['allowed_file_extensions'], $matches[2])) {and replace with            if ($matches[2] == '' || !stristr($CONFIG['allowed_file_extensions'].'/ZIP', $matches[2])) {
            cpg_die(ERROR, sprintf($lang_db_input_php['err_invalid_fext'], $CONFIG['allowed_file_extensions'].'/ZIP'), __FILE__, __LINE__);
        }

//ZIP-Mod by Patrik Berglund                        
        //We have got a .ZIP file      
        if(strtolower($matches[2]) == "zip"){
                        //Load time
                        $time = explode(" ", microtime());
                        $time = $time[1] + $time[0];
                        $start_time = $time;
                       
                        $number_of_uploaded_files = 0;
                                                               
                        $zip_archive = zip_open($HTTP_POST_FILES['userpicture']['tmp_name']);
                        $uploaded_files = '<br>';
                       
                        if ($zip_archive) {
                                //Script never times out, might be dangerous if some thing go wrong.
                                set_time_limit (0);
                                   while ($zip_entry = zip_read($zip_archive)) {
                               
                                        $uploaded_files = $uploaded_files . zip_entry_name($zip_entry);
                                                                               
                                    if (!zip_entry_open($zip_archive, $zip_entry, "r")){
                                            $uploaded_files = $uploaded_files . ' : Failed!<br>';
                                                zip_entry_close($zip_entry);
                                                continue;
                                    }
                                                               
                                        // Replace forbidden chars with underscores                                        
                                        $matches = array();
                                $forbidden_chars = strtr($CONFIG['forbiden_fname_char'], array('&amp;' => '&', '&quot;' => '"', '&lt;' => '<', '&gt;' => '>'));                                        
                                $picture_name = strtr(zip_entry_name($zip_entry), $forbidden_chars, str_repeat('_', strlen($CONFIG['forbiden_fname_char'])));
                                if (!preg_match("/(.+)\.(.*?)\Z/", $picture_name, $matches)) {
                                    $matches[1] = 'invalid_fname';
                                    $matches[2] = 'xxx';
                                }
                                if ($matches[2] == '' || !stristr($CONFIG['allowed_file_extensions'], $matches[2])) {
                                    $uploaded_files = $uploaded_files . ' : Failed!<br>';
                                        zip_entry_close($zip_entry);
                                        continue;
                                }
                               
                                // Create a unique name for the uploaded file
                                $nr = 0;
                                $picture_name = $matches[1] . '.' . $matches[2];
                                           
                                           while (file_exists($dest_dir . $picture_name)) {
                                                $picture_name = $matches[1] . '~' . $nr++ . '.' . $matches[2];
                                        }
                       
                                        $uploaded_pic = $dest_dir . $picture_name;
                       
                                        $buf = zip_entry_read($zip_entry, zip_entry_filesize($zip_entry));
                                        if($buf){
                                                if (!$handle = fopen($uploaded_pic, 'w')) {
                                                        $uploaded_files = $uploaded_files . ' : Failed!<br>';
                                                zip_entry_close($zip_entry);
                                                continue;
                                                }
                                                if (!fwrite($handle, $buf)) {
                                                        $uploaded_files = $uploaded_files . ' : Failed!<br>';
                                                zip_entry_close($zip_entry);
                                                fclose($handle);
                                                continue;
                                                }
                                                fclose($handle);
                                        } else {
                                                $uploaded_files = $uploaded_files . ' : Failed!<br>';
                                                zip_entry_close($zip_entry);
                                                continue;                                                                
                                        }
                                                       
                                        // Change file permission        
                                        chmod($uploaded_pic, octdec($CONFIG['default_file_mode']));

                                        $imginfo = getimagesize($uploaded_pic);
                                       
                                        // Check that picture size (in pixels) is lower than the maximum allowed
                                        if (max($imginfo[0], $imginfo[1]) > $CONFIG['max_upl_width_height']) {
                                                @unlink($uploaded_pic);
                                                $uploaded_files = $uploaded_files . ' : Failed!<br>';
                                                zip_entry_close($zip_entry);
                                                continue;                
                                                //cpg_die(ERROR, sprintf($lang_db_input_php['err_fsize_too_large'], $CONFIG['max_upl_width_height'], $CONFIG['max_upl_width_height']), __FILE__, __LINE__);
                                               
                                        // Check that picture file size is lower than the maximum allowed
                                        } elseif (filesize($uploaded_pic) > ($CONFIG['max_upl_size'] << 10)) {
                                                @unlink($uploaded_pic);
                                                $uploaded_files = $uploaded_files . ' : Failed!<br>';
                                                zip_entry_close($zip_entry);
                                                continue;
                                                //cpg_die(ERROR, sprintf($lang_db_input_php['err_imgsize_too_large'], $CONFIG['max_upl_size']), __FILE__, __LINE__);
                                       
                                        // getimagesize does not recognize the file as a picture
                                        } elseif ($imginfo == null) {
                                                @unlink($uploaded_pic);
                                                $uploaded_files = $uploaded_files . ' : Failed!<br>';
                                                zip_entry_close($zip_entry);
                                                continue;
                                                //cpg_die(ERROR, $lang_db_input_php['err_invalid_img'], __FILE__, __LINE__, true);
                                       
                                        // JPEG and PNG only are allowed with GD
                                        } elseif ($imginfo[2] != GIS_JPG && $imginfo[2] != GIS_PNG && ($CONFIG['thumb_method'] == 'gd1' || $CONFIG['thumb_method'] == 'gd2')) {
                                                @unlink($uploaded_pic);
                                                $uploaded_files = $uploaded_files . ' : Failed!<br>';
                                                zip_entry_close($zip_entry);
                                                continue;                                        
                                                //cpg_die(ERROR, $lang_errors['gd_file_type_err'], __FILE__, __LINE__, true);
                                       
                                        // Check image type is among those allowed for ImageMagick
                                        } elseif (!stristr($CONFIG['allowed_img_types'], $IMG_TYPES[$imginfo[2]]) && $CONFIG['thumb_method'] == 'im') {
                                                @unlink($uploaded_pic);
                                                $uploaded_files = $uploaded_files . ' : Failed!<br>';
                                                zip_entry_close($zip_entry);
                                                continue;                                                                                                
                                       
                                        //cpg_die(ERROR, sprintf($lang_db_input_php['allowed_img_types'], $CONFIG['allowed_img_types']), __FILE__, __LINE__);
                                        } else {
                                                // Create thumbnail and internediate image and add the image into the DB
                                                $result = add_picture($album, $filepath, $picture_name, $title, $caption, $keywords, $user1, $user2, $user3, $user4, $category, $raw_ip, $hdr_ip);
                                                if (!$result) {
                                                        @unlink($uploaded_pic);
                                                        $uploaded_files = $uploaded_files . ' : Failed!<br>';
                                                        zip_entry_close($zip_entry);
                                                        continue;                                                                
                                                        //cpg_die(CRITICAL_ERROR, sprintf($lang_db_input_php['err_insert_pic'], $uploaded_pic) . '<br /><br />' . $ERROR, __FILE__, __LINE__, true);
                                                }
                                        }
                                        $number_of_uploaded_files++;
                                        $uploaded_files = $uploaded_files . ' : Uploaded!<br>';
                                        zip_entry_close($zip_entry);
                                   }
                                   zip_close($zip_archive);
                                   
                                   //Load time
                                   $time = explode(" ", microtime());
                                   $time = $time[1] + $time[0];
                                   $finish_time = $time;
                                   $totaltime = ($finish_time - $start_time);
                                   
                                   $uploaded_files = $uploaded_files . '<br> Processed ' . $number_of_uploaded_files . ' images in ' . substr($totaltime, 0, strpos($totaltime, '.')+3) . ' seconds <br>';
                                                             
                                   pageheader($lang_info);
                                msg_box($lang_info, $lang_db_input_php['upls_success'] . $uploaded_files, $lang_continue, 'index.php');
                                pagefooter();
                                ob_end_flush();
                        }
                        else
                                cpg_die(CRITICAL_ERROR, sprintf($lang_errors['open_zip_failed'], $HTTP_POST_FILES['userpicture']['name']) . '<br /><br />' . $ERROR, __FILE__, __LINE__, true);
                        exit;
                }                
//ZIP-Mod ended

  • edit upload.php
find    $max_file_size = $CONFIG['max_upl_size'] << 10;and replace with//    $max_file_size = $CONFIG['max_upl_size'] << 10;
//        <input type="hidden" name="MAX_FILE_SIZE" value="$max_file_size">


Find                        <input type="hidden" name="MAX_FILE_SIZE" value="$max_file_size">and replace with                        <!--<input type="hidden" name="MAX_FILE_SIZE" value="$max_file_size">-->
  • edit lang/english.php (or whatever language file you're using)
Find        'not_with_udb' => 'This function is disabled in Coppermine because it is integrated with forum software. Either what you are trying to do is not supported in this configuration, or the function should be handled by the forum software.',and add after it        'open_zip_failed' => 'Failed to open ZIP-Archive.',[/list:u]

Upload everything to your server and you should be done.

Please note: I haven't checked this mod to work - I have only provided the code Japp sent me.
I can see some serious issues with it (since it removes max_upload_size checkings. Use at your own risk. Make sure to back up the unmodified files and your database in case something goes wrong. You may experience time-outs and other misbehaviour.

GauGau

Japp

Thanks for posting the code gaugau =)

Installation and usage guide:

You will need to have zip-support in php for this mod to work.
http://se2.php.net/manual/en/ref.zip.php

To check i you have zip-support in php do the following.
Extract this http://home.vxu.se/pbrti00/coppermine/phpinfo.zip in a folder on your webserver and surf to http://my_address/phpinfo.php and look for the following line: http://home.vxu.se/pbrti00/coppermine/phpinfo.gif . If you find it your server has zip-support.

The next step is to download the modified php-files: http://home.vxu.se/pbrti00/coppermine/zip_mod.zip

Extract these and replace the files in your coppermine folder.
db_input.php in the root directory
upload.php in the root directory
english.php in the lang folder

Now you are ready to test the mod, just upload the zip-file of pictures the same way as if you were upload just one picture
http://home.vxu.se/pbrti00/coppermine/upload.gif

Things you might wan't to consider:
 
If you upload a hugh amount of pictures (~200) the web browser might time out before the script has processed all the images. My server at home process about 10 pictures (1600x1200) per minute using imagemagick, the server is a P3 750MHz with 570MB ram.
 
The script takes all the images in the zip-file, even those in sub directories.
 
All the images get the same "Picture title", "Picture description" and "Keywords (separate with spaces)".

Edit:
I have been using this mod for about a month now and i have uploaded ~2000 pictures without a problem.

If you don't have zip-support in php, i have modified rpm files for redhat 9 and fedora 1.90 with zip-support enabled.

epsilon

(https://coppermine-gallery.com/forum/proxy.php?request=http%3A%2F%2Fhome.vxu.se%2Fpbrti00%2Fcoppermine%2Fupload.gif&hash=8ba1d3a2c5f000d5443e0ced2430e681b434aeef)

If i create an album called X for example and upload c:\x.zip in which directory will be extracted the files?

i have all my fotos in directorys like a mirror of categories, for examplea
sex > anal > 01

in ftp will be \albums\sex\anal\01

How can use this mod to mirror the extraction of the zip file in the ftp like the coppermine category/subcategory/album structure?[/img]

MisterJAD

... you could have choosen another example...

Japp

gaugau:
The "max_upload_size" used in upload.php is not a secure way to check that users don't upload huge files on the server. Some browsers simply ignore that settings. As you now there is another check in the php-script to check the file-size, and you have limitations in php.ini and httpd.conf and maybe php.conf.

The mod never makes any own changes to the database, it uses the standard coppermine functions to do that, so if these are secure, the mod should be that to.

epsilon:
Q: If i create an album called X for example and upload c:\x.zip in which directory will be extracted the files?
A: The same directory as if you just uploaded one picture to the album.

Q: How can use this mod to mirror the extraction of the zip file in the ftp like the coppermine category/subcategory/album structure?
A: You can't, I never thought of that when I wrote the mod

gmarik


chief

I have installed the mod.  It doesn't seem to work.  I ftp the zip files to my server.  I couldn't upload it in the single photo section because the file was too big.  All videos.

After I upload it and go to batch add the content, it appears in my director as a zip file.  The zip file doesn't automatically unzip.  When I attempt to add it to my ablum.  It is added as a zip file that other can download.  Do I need to do anything to unzip the files once it is on my server?

Please let me know.  Thanks for the help in advance.

Joachim Müller

first you can't put videoas in a zip - that's the first bad news.
Second, you'll have to use the http upload.
Third there's a limit on filesize on your server your webhost has set up, nothing the zip mod or coppermine can do about it.

GauGau

Japp

chief:
The mod doesn't work with the FTP-batch add function, and you can't use it on videos, just pictures.

If you have administrator (root) access to the server you can modify the php.ini and httpd.conf (php.conf) to let you upload larger files.

Edit: I and gaugau must have posted our reply at the same time ; )

RatKing

See post: http://forum.coppermine-gallery.net/index.php?topic=7750.0

The MOD described there has been loosely based on the code used for this MOD so I just felt like thanking the author(s) for the fine work done here.

andy_cul

#11
Great this mod works perfectly.

How can I combine/work the mod with this http://forum.coppermine-gallery.net/index.php?topic=8861.0

Already made some changes with my uploadzip.php

1. Find function "function form_alb_list_box($text, $name) {"
2. Replace the whole function with these two new functions:

// The function to create the album list drop down.
function form_alb_list_box($text, $name) {
// frogfoot re-wrote this function to present the list in categorized, sorted and nicely formatted order

    // Pull the $CONFIG array and the GET array into the function
    global $CONFIG, $HTTP_GET_VARS, $lang_upload_php;

    // Also pull the album lists into the function
    global $user_albums_list, $public_albums_list;

    // Check to see if an album has been preselected by URL addition. If so, make $sel_album the album number. Otherwise, make $sel_album 0.
    $sel_album = isset($HTTP_GET_VARS['album']) ? $HTTP_GET_VARS['album'] : 0;

    // Create the opening of the drop down box
    echo <<<EOT
    <tr>
        <td class="tableb">
            $text
        </td>
        <td class="tableb" valign="top">
            <select name="$name" class="listbox">

EOT;

    // Reset counter
    $list_count = 0;

    // Cycle through the User albums
    foreach($user_albums_list as $album) {

        // Add to multi-dim array for later sorting
        $listArray[$list_count][cat] = $lang_upload_php['personal_albums'];
        $listArray[$list_count][aid] = $album['aid'];
        $listArray[$list_count][title] = $album['title'];
        $list_count++;
    }

    // Cycle through the public albums
    foreach($public_albums_list as $album) {

        // Set $album_id to the actual album ID
        $album_id = $album['aid'];

        // Get the category name
        $vQuery = "SELECT cat.name FROM " . $CONFIG['TABLE_CATEGORIES'] . " cat, " . $CONFIG['TABLE_ALBUMS'] . " alb WHERE alb.aid='" . $album_id . "' AND cat.cid=alb.category";
        $vRes = mysql_query($vQuery);
        $vRes = mysql_fetch_array($vRes);

        // Add to multi-dim array for sorting later
        if ($vRes['name']) {
            $listArray[$list_count][cat] = $vRes['name'];
        } else {
            $listArray[$list_count][cat] = $lang_upload_php['albums_no_category'];
        }
        $listArray[$list_count][aid] = $album['aid'];
        $listArray[$list_count][title] = $album['title'];
        $list_count++;
    }

    // Sort the pulldown options by category and album name
    $listArray = array_csort($listArray,'cat','title');

    // Finally, print out the nicely sorted and formatted drop down list
    $alb_cat = '';
    echo '                <option value="">' . $lang_upload_php['select_album'] . "</option>\n";
    foreach ($listArray as $val) {
        if ($val[cat] != $alb_cat) {
if ($alb_cat) echo "                </optgroup>\n";
            echo '                <optgroup label="' . $val[cat] . '">' . "\n";
            $alb_cat = $val[cat];
        }
        echo '                <option value="' . $val[aid] . '"' . ($val[aid] == $sel_album ? ' selected' : '') . '>   ' . $val[title] . "</option>\n";
    }
    if ($alb_cat) echo "                </optgroup>\n";

    // Close the drop down
    echo <<<EOT
            </select>
        </td>
    </tr>

EOT;
}

// Multi-dim array sort, with ability to sort by two and more dimensions
// Coded by Ichier2003, available at php.net
// syntax:
// $array = array_csort($array [, 'col1' [, SORT_FLAG [, SORT_FLAG]]]...);
function array_csort() {
   $args = func_get_args();
   $marray = array_shift($args);

   $msortline = "return(array_multisort(";
   foreach ($args as $arg) {
       $i++;
       if (is_string($arg)) {
           foreach ($marray as $row) {
               $sortarr[$i][] = $row[$arg];
           }
       } else {
           $sortarr[$i] = $arg;
       }
       $msortline .= "\$sortarr[".$i."],";
   }
   $msortline .= "\$marray));";

   eval($msortline);
   return $marray;
}


3. fix a bug. Find
function& spring_cleaning($directory_path, $cache_time = 86400, $exclusion_list = array('index.html')) {

4. Replace with:
function spring_cleaning($directory_path, $cache_time = 86400, $exclusion_list = array('index.html')) {

5. Find
// Check for incoming album placement data.
    if ((isset($_POST['album'])) and (isset($_POST['unique_ID']))) {

6. After that add:
// Check if user selected an album to upload picture to. If not, die with error.
        // added by frogfoot
        $album = (int)$HTTP_POST_VARS['album'];
        if (!$album)cpg_die(ERROR, $lang_db_input_php['album_not_selected'], __FILE__, __LINE__);


I'm using cpg 1.33 and I've attached my uploadzip.php

unknown_girl

Hi! if we don't have this isntalled (zip-support in php) how do we install it?

Joachim Müller

Quote from: unknown_girl on July 23, 2005, 05:39:11 PM
Hi! if we don't have this isntalled (zip-support in php) how do we install it?
When webhosted: not at all! If the server is yours to administer, you could compile the zip library in (not recommended) or get a php package that contains the zip module.

unknown_girl

I thought my server hasn't installed like the zip support but I found out it is installed.. I tryed with another gallery (mambo module) and it works... can someone help me?
this is the error:

Fatal error: Call to undefined function: zip_open() in /home/hilary/public_html/fotos/db_input.php on line 266

unknown_girl


Nibbler

If the function is undefined then you don't have the zip extension loaded.

unknown_girl

And how do i do that? Do I put like an "url" to like the file - pclzip.lib.php

Joachim Müller

See my answer from above: this means you simply can't use this mod. It is not meant for you, unless you find another webhost that actually has what you need. Further replies to this thread are not particularly helpful, there's nothing we could do or advice to make this mod work for you.

verser233

#19
Can i watch this mod in action, please????

Cause i dont understand what's this....maybe this one, im looking for........... i use phpnuke with coppermine 1.2b


Can someone help me please?