Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New Coder
    Join Date
    Aug 2011
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Array Processing

    I have a changeable list of files that need to be sent as email attachments. My cgi app can process and assemble the list then pass it on to PHP.

    I'm running into trouble when trying to put the files and their paths into an array.

    This works:
    PHP Code:
    $path '/Library/WebServer/Documents/TheSite/TheFiles';
    $files = array($path.'/ThisFile.pdf',$path.'/ThatFile.pdf'); 
    And this works:
    PHP Code:
    $files = array('/Library/WebServer/Documents/TheSite/TheFiles/ThisFile.pdf','/Library/WebServer/Documents/TheSite/TheFiles/ThatFile.pdf'); 
    So this should work, but it fails to find the files:
    PHP Code:
    $filelist '/Library/WebServer/Documents/TheSite/TheFiles/ThisFile.pdf','/Library/WebServer/Documents/TheSite/TheFiles/ThatFile.pdf';
    $files = array($filelist); 
    I've tried other variations but can't find a way to handle it.

    The need is to find a format with the path and file name that I can pass to PHP as I've done in $filelist above. I can't see any other way to provide an array of file names and combine them with the path to make the equivalent of $path.'/ThisFile.pdf',$path.'/ThatFile.pdf'

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    $a | $b <> $c.

    Don't know why you think that last line should work. That would be a syntax error, since you cannot just place a , in the middle of nowhere, and in the event that its a correct string you couldn't just wrap it in an array. You could explode it then though.

    I don't quite follow what your question here is. Are you asking how to prepend the same string to each of the items within an array?

  • #3
    New Coder
    Join Date
    Aug 2011
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts
    In my cgi app, I have a list, an array, of several file names such as This.pdf, That.pdf, etc.. I know where they are so I have the path. I can pass the list of files as is to PHP separately or I can add the path to each file name.

    It seems that putting them together would be best before I pass it to PHP., so each would become /Library/WebServer/Documents/TheSite/TheFiles/ThisFile.pdf

    In my cgi I can very easily format it with quotes, commas or whatever, but I just can't find a way to get the same end result in the PHP process.

    So I'm looking for the syntax that would make this list:
    /Library/WebServer/Documents/TheSite/TheFiles/ThisFile.pdf
    /Library/WebServer/Documents/TheSite/TheFiles/ThatFile.pdf

    become the same as $files after being run through this:
    PHP Code:
    $path '/Library/WebServer/Documents/TheSite/TheFiles'
    $files = array($path.'/ThisFile.pdf',$path.'/ThatFile.pdf'); 
    Last edited by MichigamJim; 08-29-2011 at 01:32 PM.

  • #4
    Senior Coder Rowsdower!'s Avatar
    Join Date
    Oct 2008
    Location
    Some say it's everything.
    Posts
    2,027
    Thanks
    5
    Thanked 397 Times in 390 Posts
    You could use a "for" loop or a "foreach" loop for this:

    For loop:
    PHP Code:
    $path="/Library/WebServer/Documents/TheSite/TheFiles'/";
    for(
    $i=0;$i<count($files);$i++){
        
    $files[$i]=$path.$files[$i];

    Foreach loop:
    PHP Code:
    $path="/Library/WebServer/Documents/TheSite/TheFiles'/";
    foreach(
    $files as $key => $value){
        
    $value=$path.$value;

    The object of opening the mind, as of opening the mouth, is to shut it again on something solid. –G.K. Chesterton
    See Mediocrity in its Infancy
    It's usually a good idea to start out with this at the VERY TOP of your CSS: * {border:0;margin:0;padding:0;}
    Seek and you shall find... basically:
    validate your markup | view your page cross-browser/cross-platform | free web tutorials | free hosting

  • #5
    New Coder
    Join Date
    Aug 2011
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Loops would work, but it just feels clumsy. I'm sure there's some simple syntax I'm missing that would make my array of paths and file names come out the same as array($path.'/ThisFile.pdf',$path.'/ThatFile.pdf')

  • #6
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Quote Originally Posted by MichigamJim View Post
    Loops would work, but it just feels clumsy. I'm sure there's some simple syntax I'm missing that would make my array of paths and file names come out the same as array($path.'/ThisFile.pdf',$path.'/ThatFile.pdf')
    You mean just the printing of them? var_export($path); would literally show it as if you were to make an array of them in PHP.
    ie:
    PHP Code:
    <?php
    $files 
    = array('/Library/WebServer/Documents/TheSite/TheFiles/ThisFile.pdf','/Library/WebServer/Documents/TheSite/TheFiles/ThatFile.pdf');  

    var_export($files);
    ?>
    Would literally produce:
    Code:
    array ( 0 => '/Library/WebServer/Documents/TheSite/TheFiles/ThisFile.pdf', 1 => '/Library/WebServer/Documents/TheSite/TheFiles/ThatFile.pdf', )
    Or is it something else you are referring to?

    Or are you asking something like a directory listing which can be done using a glob. Still need to either loop it or implode at as these are array types, and if you do a scan technique than its a loop on the resource.

  • #7
    New Coder
    Join Date
    Aug 2011
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts
    explode(

    That is the simple solution I was looking for. It converts my list to an array.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •