Jump to content

Recommended Posts

I have been getting my rear kicked on this.

 

I want to:

 

1. Read directory called "uploads" for files "cat-[thispage]-[filename].jpg".

2. If file begins with "cat" and [thispage] matches the page slug of this current (WordPress) page, print image to screen.

3. Extract [filename] and split is components (which are separated by hypens) into discrete words.

4. Print discrete words (first letter capitalized) under image.

5. Provide hyperlink to page [filename].

 

While I'm not a great coder, I think I can do step 1 and probably 2 (although most of you would laugh at the coding). But i really need help exploding the content and then using that data.

 

Any help would be appreciated. Thanks.

Link to comment
https://forums.phpfreaks.com/topic/274579-read-directory-and-explode-filename/
Share on other sites

#1 and #2 can be done with one call to glob().

$title = /* current page slug */;
$files = glob("uploads/cat-{$title}-*.jpg");

For #3 you can substr() for the middle portion, explode() for the pieces, and ucfirst() for the capitalization.

// for each $file in $files {
    $filename = substr($file, strlen("uploads/cat-{$title}-"), -4);
    $pieces = array_map("ucfirst", explode("-", $filename));
// }

#4 is a loop and #5 is making a link to $filename.

Edited by requinix

Thanks! I went in and posted this to my page. I got a white screen in response. What did I do wrong?

 

 

$title = $post_slug;

$files = glob("/uploads/cat-{$title}-*.jpg");

for each $file in $files

{

$filename = substr($files, strlen("/uploads/cat-{$title}-"), -4);

$pieces = array_map("ucfirst", explode("-", $filename));

}

I'm extremely weak when it comes to arrays, so please forgive me. So now I have this piece of code:

 

$files = glob($abs_url.$img_path.'/cat-par*.jpg');

foreach $file in $files

{

$filename = substr($files, strlen($abs_url.$img_path.'/cat-par*.jpg'), 2,4);

$pieces = array_map('ucfirst', explode('-', $filename));

}

?>

 

So $files is the variable that contains the, for example, six or eight images in the folder, yes? And then $files is pulled into the foreach statement. There, I want it to read all pieces of the variable contained between the "-" (hyphen) AFTER the first two, "cat" and "par." I know that section is wrong. As for the foreach statement and how values are read into $files: totally lost.

 

The sad thing is that if I had time, I'd go take a PHP course. I love this language, but am often lost within it.

1. Look at the link jcbones gave you for the foreach construct. That pages tells you exactly what it does and how to use it.

2. Compare your strlen() code with mine. My version is everything up to the wildcard: no wildcard, no extension.

3. Also look at the manual page for substr. It takes three arguments, not four. The third argument needs to be a negative number.

 

Here's why. substr() extracts a piece of a string, starting at one place and going forward to another place. The filename has three parts to it: the initial part with the path and the "cat-par", the name that you want to extract from it, and everything else after it (ie, the extension). Using strlen() you know the length of the first part so that's where substr() should start; using -4 for the third argument tells it to from the starting point up to four characters from the end. Four because the extension - the third part - is four characters long.

 

4. Now that you have $pieces you need to do something with it. It will be an array so you could foreach over it. Or you could do something more clever. It depends how the "print discrete words under image" needs to work for your existing code.

Requinix,

 

First, thank you for walking me through this. I need to throw one more curve into this. Between posts, I went searching for more answers. I found a video tutorial. The code he gave was this (and this doesn't even touch on exploding):

 

<?php

$dir = 'http://rowlandwilliams.com/trophy/wp-content/uploads/';

$scan = scandir($dir);

for ($i=0; $i<count($scan); $i++) {

echo $dir; /* I was testing here to see if it would print */

echo $scan[$i]; /* I was testing here to see if it would print */

if (strpos($scan[$i], 'paper') !== false)

{

echo '<div id="#catalog-directory-images">';

echo '<img src="'.$dir.$scan[$i].'" alt="'.$scan[$i].'" />';

echo '</div>';

}

}

?>

 

$dir will print. $scan will not.

 

I kind of feel like with my struggle to understand arrays this would be good to understand. Why am I not getting any sort of value associated with $scan? (I know it isn't your example, but I'm trying to wrap my head around the concept of reading arrays. I think if I can get there, everything else will fall into place.)

Thanks! Dropping to a simple path got rid of my white screen.

 

Now, $dir is printing to the screen. But $scan[$i] is not. And the fact that $dir is printing only once tells me that $scan is not finding the image and therefore $i isn't incrementing.

 

So I went in and added $_SERVER['HTTP_HOST'] in front of $dir and "/cat-paper-shopping-bags-100-recycled-white-kraft-2.jpg" after, and an image popped up. So the path is correct, yes?

 

So why isn't $scan[$i] finding the images?

 

And thanks for your patience.

Edited by rowlandville

I have tried your code, other code, variations on the code, and I even think I sort of get how it all works, but nothing seems to see the images in the folder.

 

The images are in http://domain-name/trophy/wp-content/uploads. WordPress is installed in the trophy folder.

 

Any other ideas? I have spent ten hours plus trying to make this work to no avail.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.