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

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

Dani AI

Generated

PCLZip already exposes a pre-extract callback that can change the filename that will be written to disk; the callback gets a modifiable header and PCLZip will use the header after the callback returns (so renaming in the callback is the right place to do it). (packagist.org)

Practical pattern (no verbatim copy of the code already on the thread): inside the pre-extract callback, first whitelist/validate the file type (extension) and normalize the extension to a known safe set. Produce a short, hard-to-guess base name (for example, a hex string from a CSPRNG such as the output of bin2hex(random_bytes(5))) and append the validated extension. If the project needs absolute uniqueness, test the destination with file_exists() (regenerate on collision) before letting the callback return the value that allows extraction to proceed. Use the callback only to set the new name — PCLZip will then write that name when it extracts the entry. (php.net)

Security and edge cases: guard against directory traversal entries (Zip Slip) by stripping directory components from the entry name or by using PCLZip extraction options that restrict/strip paths; do not trust archive paths from clients. Also keep extension checks strict (lowercase comparison) and consider running a short post-extract sanity check (permissions, image validation, scan for malformed files). Zip-Slip risks and general mitigation advice are well documented. (security.snyk.io)

Notes tied to the thread: ’s md5/uniqid suggestion will work in many cases, but a CSPRNG-based short token (or combining uniqid with a secure random value) reduces collision and predictability risks. closed the thread after solving the immediate problem; the steps above show a more robust, collision-aware and secure approach that avoids directory-traversal pitfalls and ensures the renamed files are safe for production use. (php.net)

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.
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 , or .

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 ==>
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.