Hi Guys

I have been extracting zip files and uploading them successfully with PclZip Library.However i want to rename the extracted files using some random string generator on the fly before placing them in my 'images' folder

On the site they said that a callback function can be used to rename the files but its not working
Referance
http://www.phpconcept.net/pclzip/user-guide/18
Here is code

if(move_uploaded_file($file_tmp, $target_path)) {
        $zip = new PclZip($target_path);
            $zip->extract(PCLZIP_OPT_PATH, $destination_path,
                          PCLZIP_OPT_REMOVE_ALL_PATH,
                          PCLZIP_CB_PRE_EXTRACT, "zipPreExtractCallBack");
         unlink($target_path);
        $message = "Your .zip file was uploaded and unpacked.";
        } else {    
        $message = "There was a problem with the upload. Please try again.";
}

Now the most important thing - the function

    function zipPreExtractCallBack($p_event, &$p_header) {
        $info = pathinfo($p_header['filename']);
        // limit the unzipped images
        if ($info['extension'] == 'jpeg' || $info['extension'] == 'jpg' || $info['extension'] == 'gif' || $info['extension'] == 'png'|| $info['extension'] == 'bmp' ) {
            $get_host_iconv_encode = iconv_get_encoding("internal_encoding");
            $get_file_mb_encode = mb_detect_encoding( $info['basename'] );
            $p_header['filename'] = iconv( $get_host_iconv_encode , $get_file_mb_encode.'//TRANSLIT//IGNORE' , $p_header['filename'] );
            return 1;
        }
        // other file extensions will not be unzipped
        else return 0;
    }

I want the function to auto-rename images name before saving them in $destination_path .
Can anyone of you help me with this library
Insert something similar in code

$random_code = random_gen(5);
$file_name = $random_code.'.'.$file['extension'];

Long Live DaniWeb
Thanks

Recommended Answers

All 6 Replies

Hi,

yOu can probably get by this using md5 ,uniqid, and rand combination like shown in the codes below. YOu can arrange your codes in such a way as

$filename = md5(uniqid(rand())).".".$ext;
$target_path = "YourDirectory/".$filename;
if(move_uploaded_file($file_tmp, $target_path)) {

## rest of your codes here

} 

Where? ext = to the actual extension of the file. So, you may want to get the extension first if needed, and then attached it to the filename.

Dude code to randomise is not the problem
Here i am using the PclZip Library to extract a zip file

What i want is to be able to randomise the images names

Like for e.g in zip file there is an image flower.jpg i want it to be I3so19.jpg(just an example of a random name)
I need someone to guide me and tell me where to put

$random_code = random_gen(5);
$file_name = $random_code.'.'.$file['extension'];

Inside that FUNCTion so that every extracted file get a random name

I am assuming here that the linked classes below are the same class you are currently using. Otherwise, please post any links for the source of the class as text file, but not to expect anyone downloading it.

Sorry about that Dude.. I just got busy to look at the full PclZip class at this moment. However, just by glancing at the script, there is a method called

 function extract()
{

Look for line 683 as shown here.
That's where you can add whatever you want.. I don't see any problem for you adding it or extending the class as you stated here. I am also assuming here that you are OOP expert.

Dude code to randomise is not the problem

Or, we can wait until someone extends that class for you.. I am hoping more volunteers will read this..

Next time, try to provide the link for the source code of the class you are using to prevent any confusion. Not all people are into using PclZip class. Like Class used, or this.

i posted only part of my code that are important so that i don't bored u guys
I am in no way an OOP , just a begineer with quest to learn:P
Source of class ==> http://www.spip.net/spip-dev/INSTALL/pclzip.php.txt
Basically all i want is that the function randomises the name of each file during extraction and then stored as a random name(uses random_gen(5)) to do that
Random_gen(5) is a function i created to generate random strings of length 5(This is insignificant here and works 100% that's why not added)

Can u show what u mean by extending class to achieve what i said above

No one to help me out.......

Fixed it myself
Closed

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.