Member Offer
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

php question

Discussion in 'Website Design Forum:' started by tim, Feb 14, 2009.

  1. tim

    tim Senior Member

    if i upload 27 files to a directory, how do i create a PHP code to ask it to pull the first 9 into one document, the second 9 into the next document and the last lot of 9 in a third document? i'm assuming it's possible...
  2. Scott

    Scott Member

    Have a read of:
    PHP: scandir - Manual
    PHP: fread - Manual

    Scan a directory and save it to an array. Then loop the array to handle the files.

    Hopefully this is a good starting point. If you need more details, let me know.
  3. tim

    tim Senior Member

    thanks scott, very helpful :D :batman:

    didn't know there was a php website with tips... cool!
  4. Scott

    Scott Member

    The official website is very useful. Googling around it is the best way, I think.

    Google for "site:PHP: Hypertext Preprocessor [keywords]" and you'll only get results from (if you didn't already know). But if you're not sure what functions you need, but you kind of know what you want to do, try searching in really basic English. In the above example, you could have searched

    "site:PHP: Hypertext Preprocessor scan directory" - first result? You got it!
  5. Nanane

    Nanane Junior Member

    However, if your goal is to have an array, i recommend you using the glob function.

    I found it weeks ago, and now i cant live without :)
  6. tim

    tim Senior Member

    oooh thank you :D
  7. philjohns

    philjohns Senior Member

    Just out of curiosity - what are you using this for Tim?

    Im trying to broaden my knowledge of PHP....
  8. Jazajay

    Jazajay Active Member

    Hay Tim,
    Security mindset coming in, you are not allowing the user to modify this file, ie ~ is the PHP code being created dynamically from user supplied data?

    So for example you have a link or what ever that looks like this?

    <?php $category=$_POST['category'];
    echo $category."/some-file.htm";?>

    Red ~ PHP
  9. tim

    tim Senior Member

    @Jazajay: i dunno what you mean Jaz, sorry! Do you mean like each link different per user, which could mean they could pull in all kinds of files? It'd be a product page with 9/16 small images, and I don't want to have to name them each time, but just pull them in.

    @Phil: I'm using this so that I can just upload a few images to a directory and the page automatically pull it in, instead of me having to link each one in at a time. There's 300 products on the site so it's just trying to make the job smaller.
  10. Jazajay

    Jazajay Active Member

    Yeah exactly if you allow the user to change the file path, they can then pull any file off the server, ie: db passwords, this is obviously something you don't want them to do.

    To circumvent it I would use the basename() function as this function makes sure that any file path that is added is trimmed off.

    So in the above example it would be safer to write ~
    echo basename($category)."/some-file.htm";

    I really, really respect that you want to teach yourself, IMO the only true way to learn, but always think how can this be exploited, because if you can think of 1 way so can a hacker, trust me from experience. :)

    Hope it helps.
    Red ~ PHP
  11. tim

    tim Senior Member

    oh right okay then :) will get onto making sure i do it perfectly then!

Share This Page