Trying To Create Dynamic Array/String For A Class

Hey,

I am using the PclZip class. It works great, but the feature I REALLY want only works if you HARD code it.

Basically, I want to be able to dynamically, at any point, create 5 or 10 or 44 instances of the bolded code. It’s THIS one in particular I wish I could dynamically spit out 20 times if need be:

[B]array( PCLZIP_ATT_FILE_NAME => ‘data/file1.txt’,
PCLZIP_ATT_FILE_NEW_FULL_NAME => ‘newdir/newname.txt’
)

For instance, if I have 20 files, twenty of the above arrays
are created.

I know how to build the STRING to store 20 of them, but when I replace the bolded code below with the STRING that holds the text you see in bold, it doesn’t treat it literally.
[/B]
Below is the full example their site gives:



$list = $archive->create([B]array(
                    array( PCLZIP_ATT_FILE_NAME => 'data/file1.txt',
                           PCLZIP_ATT_FILE_NEW_FULL_NAME => 'newdir/newname.txt'
                         ),
                    array( PCLZIP_ATT_FILE_NAME => 'data/file2.txt',
                           PCLZIP_ATT_FILE_NEW_SHORT_NAME => 'newfilename.txt'
                         ),
                  )[/B],
                  PCLZIP_OPT_ADD_PATH, 'newpath',
                  PCLZIP_OPT_REMOVE_PATH, 'data');


The Error I Receive if I try to do this dynamically:

ERROR : ‘PCLZIP_ERR_MISSING_FILE (-4) : File ’ array( array( PCLZIP_ATT_FILE_NAME => ‘data/file1.txt’ does not exist’

Here is what I have NOW, using the string instead of it being hard coded as in the example above:



$test_array="

array(

array( PCLZIP_ATT_FILE_NAME => 'data/file1.txt',
                           PCLZIP_ATT_FILE_NEW_FULL_NAME => 'newdir/newname.txt'
                         ),
                    array( PCLZIP_ATT_FILE_NAME => 'data/file2.txt',
                           PCLZIP_ATT_FILE_NEW_FULL_NAME => 'data/newname2.txt'
                         ),
 ),
                    
                    ";
                    
$archive = new PclZip("rename_test.zip");
$list = $archive->create("[B]$test_array[/B]",
                  PCLZIP_OPT_ADD_PATH, 'Your Order',
                  PCLZIP_OPT_REMOVE_PATH, '157');
             


So the TOP bolded part works when hard coded. The second version doesn’t.
No parse errors, just the damn thing not treating the class functions the way I want. If I could fix this, it saves us from having to re-write THOUSANDS of mp3 files that have ugly non readable names (was done before I was hired) and would make my thanksgiving a lot more enjoyable.

Thanks for your time.

PS. For reference, just to be clear, *building the string in any way/shape or form - I can do that. Getting the script to read the string the same way it would read/process the hard coded code (bolded, in the first code example), that’s my dilemma. Thanks, again.

It’s expecting an array argument, so you can’t just pass it a string. The string won’t be interpreted. Just store an array of arrays in a variable, e.g.



$files = array(

  array(

    PCLZIP_ATT_FILE_NAME => 'data/file1.txt',

    PCLZIP_ATT_FILE_NEW_FULL_NAME => 'newdir/newname.txt'

  ),


  array(

    PCLZIP_ATT_FILE_NAME => 'data/file2.txt',

    PCLZIP_ATT_FILE_NEW_SHORT_NAME => 'newfilename.txt'

  )

);
// array


$files[] = array(

  PCLZIP_ATT_FILE_NAME => 'data/file3.txt',

  PCLZIP_ATT_FILE_NEW_SHORT_NAME => 'newfilename3.txt'

);
// array


$files[] = array(

  PCLZIP_ATT_FILE_NAME => "data/{$some_var}.txt",

  PCLZIP_ATT_FILE_NEW_SHORT_NAME => $dynamic_value

);
// array


$archive->create( $files, PCLZIP_OPT_ADD_PATH, 'newpath', PCLZIP_OPT_REMOVE_PATH, 'data' );


Instead of dynamically building a string, dynamically build an array of arrays and pass it to PclZip::create(). Whatever dynamic values you would add to a string, put them in an array and store that array in $files.

It’s funny. After I posted I realized I needed to create an array but, of course, the 2nd question would be the proper way to implement it. This is very helpful. I will test this out and report back (in case anybody else can benefit from this).

Thank you!

I do receive an error (One that the class spits out, not due to PHP syntax/etc). For reference, the error is:

ERROR : 'PCLZIP_ERR_NO_ERROR (0) : ’

Wasn’t sure what that means. I guess it has a hard time reading the array. In the instructions, the rename function array must be in this form:


array(
                    array( [B]PCLZIP_ATT_FILE_NAME[/B] => 'data/file1.txt',
                           [B]PCLZIP_ATT_FILE_NEW_FULL_NAME [/B]=> 'newdir/newname.txt'
                         )
                  )

Our array looks like this when it is printed out:



Array
(
    [0] => Array
        (
            [[B]79001[/B]] => 157/Track_10_01_256.mp3
            [[B]79003[/B]] => Artist_Name/single_1_title.mp3
        )

    [1] => Array
        (
            [[B]79001[/B]] => 158/Track_10_04_256.mp3
            [[B]79003[/B]] => Artist_Name2/single_1_title.mp3
        )



)


Notice the difference in bold. I wonder how I can make sure the [79001] is replaced with PCLZIP_ATT_FILE_NAME
and the [79003] with PCLZIP_ATT_FILE_NEW_FULL_NAME.

Thank you for your help and time.

Constants:-

A constant is an identifier (name) for a simple value. As the name suggests, that value cannot change during the execution of the script (except for magic constants, which aren’t actually constants). A constant is case-sensitive by default. By convention, constant identifiers are always uppercase.


<?php

define('CUSTOM_CONST_ANTHONY', 100001);

var_dump(
  CUSTOM_CONST_ANTHONY
); #int(100001)

?>

That makes sense. What concerns me, then, is how do I fulfill my original request? Is it a lost cause? To allow an array to be ready by the class, dynamically, so I can help launch this site is ideal but I haven’t seen a real world example of something like this request in action. I was hoping to avoid the idea of adding a temp folder, re-naming the files via rename function and then zip that up as I wanted to avoid putting the server through that if we could avoid it.

Thank you for your time and enjoy your Thanksgiving if you are U.S based. (Well, maybe not for Anthony but the appreciation is all that matters) :wink:

I don’t have any specific knowledge of the class you’re using, so I don’t know what that error message means. Your zip file isn’t created? I guess you’d have to look at the docs / source for that class and see if it explains the error message. Are your other arguments to PclZip::create() correct?

[QUOTE=jbh,post:6,topic:71929"]
I was hoping to avoid the idea of adding a temp folder, re-naming the files via rename function and then zip that up as I wanted to avoid putting the server through that if we could avoid it.
[/quote]

How many files / gigabytes of files do you have?

Well, usually the most we’ll zip up is 150 mb worth. The script is fine. The error, it seems, comes from the fact that the ‘rename’ function can’t read the array (as it would if it were hard coded) since the defined constants are not part of the dynamic array.

The reason I use this method is because we are on php 4+ without the php zip file required libraries to use native function. I love this class, it is amazing. It’s just not naturally meant to use that re-name function dynamically.

I am currently in the process of trying to build an array where I can define the keys and then use that to try to ‘trick’ the class into reading the defined constants and re-name multiple files at once:


function my_const_arr()
{
   return array
   (
      [B]'key1' => 'val 1',
      'key2' => 'val 2'[/B]
   );
}

Trying to get the bold part to be dynamic. Learning how to do that would likely help solve my problem, as the ‘key1’ and ‘key2’ would be the constants that the class needs in order to operate as I want it to using this re-name function.

Thanks, guys!

Ok, I don’t really understand this comment:

I was hoping to avoid the idea of adding a temp folder, re-naming the files via rename function and then zip that up as I wanted to avoid putting the server through that if we could avoid it.

As long as you have a bit of disk space and can tolerate a momentary strain on the CPU (maybe just a matter of seconds), then you could just rename the files to a temp folder and zip or tar -cz them from the shell. EDIT: I mean a momentary strain on the CPU to create the zip or tar archive. The PHP script to rename the files might run for a bit longer. I don’t know that it would be much of a strain on the system, but if you’re concerned about that you could do it in batches.

It’s just not naturally meant to use that re-name function dynamically.

I am currently in the process of trying to build an array where I can define the keys and then use that to try to ‘trick’ the class into reading the defined constants and re-name multiple files at once:

Trying to get the bold part to be dynamic. Learning how to do that would likely help solve my problem, as the ‘key1’ and ‘key2’ would be the constants that the class needs in order to operate as I want it to using this re-name function.

You seem to have some kind of fundamental misunderstanding of how arrays and / or functions work, or what dynamic means. PclZip::create() requires arguments in a certain format. I’ve never used the class, but you represented that the first argument is an array of arrays where the keys are constants defined by the class and the values are filenames. If that’s the case, then as long as you pass it an argument conforming to that description, the method doesn’t care how the array was composed.

The rename method is always dynamic – it operates on the data you pass it at any given time.

According the code you’ve posted the constants are PCLZIP_ATT_FILE_NAME and PCLZIP_ATT_FILE_NEW_FULL_NAME. Those are constant variable names. According to what you’ve posted, the values are 79001 and 79003, respectively. You don’t trick the class into reading the constants, you just use them as the keys of the arrays. The following two snippets are equivalent:



$file = array(

  PCLZIP_ATT_FILE_NAME => "157/Track_10_01_256.mp3",
  
  PCLZIP_ATT_FILE_NEW_FULL_NAME => "Artist_Name/single_1_title.mp3"
            
);


$file = array(

  79001 => "157/Track_10_01_256.mp3",
  
  79003 => "Artist_Name/single_1_title.mp3"
            
);


The bold parts would be made dynamic the same way you would have made them dynamic when you were trying to compose a string to pass to PclZip::create().

Your chances of succeeding at this are severely compromised by your misunderstanding of why it’s not working and your reactions to that, which are leading you on a wild goose chase.

What version of PclZip are you using?

Hey. Ty so much for all of this. Php 4+. It’s awful, I am in hell with this company.

One thing I must note:

“The following two snippets are equivalent:”

I thought so, too. Problem is it spits an error out when it’s dynamic, using your method, but not static. So I just figured something isn’t adding up. That’s why I wondered if the darn thing needed the defined constant, that’s all.

It’s odd. It’s as if it can’t go anywhere with the dynamic array. It’s purely a class error, not PHP (at least, not a syntax/logic issue I should say).

I’m resigned to think I might just have to read from the array, copy the files to a temp directory, re-name them myself and then zip it. Sucks, but I am not sure what else to do.

I cannot thank you enough for your time.

Well, I guess all I can tell you is that it’s about what’s in your array, not how it was generated.

Using your original code example, if this works for you:


$list = $archive->create(array(
                    array( PCLZIP_ATT_FILE_NAME => 'data/file1.txt',
                           PCLZIP_ATT_FILE_NEW_FULL_NAME => 'newdir/newname.txt'
                         ),
                    array( PCLZIP_ATT_FILE_NAME => 'data/file2.txt',
                           PCLZIP_ATT_FILE_NEW_SHORT_NAME => 'newfilename.txt'
                         ),
                  ),
                  PCLZIP_OPT_ADD_PATH, 'newpath',
                  PCLZIP_OPT_REMOVE_PATH, 'data');

Then try this:



$files = array(
	array( PCLZIP_ATT_FILE_NAME => 'data/file1.txt',
				 PCLZIP_ATT_FILE_NEW_FULL_NAME => 'newdir/newname.txt'
			 ),
	array( PCLZIP_ATT_FILE_NAME => 'data/file2.txt',
				 PCLZIP_ATT_FILE_NEW_SHORT_NAME => 'newfilename.txt'
			 )
);
                  
$list = $archive->create( $files,
                  PCLZIP_OPT_ADD_PATH, 'newpath',
                  PCLZIP_OPT_REMOVE_PATH, 'data');
                  

If that works for you, then try building that exact same array using your dynamic method, then try passing it to create(). All of that should work equally. If not, then var_dump() your static array and your dynamic one and see what the difference is.

What version of PclZip are you using?

Version 2.8.2. This worked, I just have to make sure I build this array properly. Not sure why this works but the other example you posted didn’t. I’ll try to figure that out later.

Ty so much. I’ll update when I get this right for others out there, in case somebody else wants to do something like this.

Enjoy.