Hi again. This is the part of the code that doesn't work.

$fileBaseName=basename($file,'.jpg');
echo("<img src='{$handle}/{$file}' alt=$fileBaseName />");

So what i want to do is at alt to show only the name without the.jpg and i dont know why if i have two or three words is stops at the first word, meaning that it stops at the firt " ". So how can i fix this? Tried with explode too, i don't think the basename is to blame for this, but i dont know what to say...

Recommended Answers

All 11 Replies

Sorry i didn't make myself clear. So i have a file in which i read the files(the files are 100% jpg i made sure of that) and i want them to show on the scren with echo

for ($r=1;$r<=$p;$r++){
        if (false !== ($file = readdir($dir))){
            $fileBaseName=basename($file,'.jpg');
            echo("<img src='{$handle}/{$file}' alt=$fileBaseName />");
        }
}
Member Avatar for diafol

I like glob :) :

$handle = "img/"; //why is this different to the orig path??
foreach (glob("img/*.jpg") as $filename) {
    $file = basename($filename);
    echo "<img src='$handle/$file' />";
}

If $handle is superfluous:

foreach (glob("img/*.jpg") as $filename) {
    echo "<img src='$filename' />";
}

One downside of glob is that it doesn't support pcre regex, so if you want to get case-insensitive 'jpg' and 'jp?g' (for jpeg), then, you could do this:

foreach (glob("img/*.[jJ][pP]*[gG]") as $filename) {

However, that would return files with extensions such as .jpefefeg too! So if you're sure that they're all jpg and not jpeg:

foreach (glob("img/*.[jJ][pP][gG]") as $filename) {

may be better

there are 10 folders in which there are another 3 other folders so that's why i use $handle to make sure where am i.

$handle = ("countries/$folder/$sfolder");

$folder and $sfolder are known;

$dir = opendir("countries/$folder/$sfolder");

so if i use glob it will work at the img files in this dir?

And i am not using basename for the src, i am using it for alt, because i have a javascript who shows alt in a different way, and i really need its value.

Member Avatar for diafol

Glob won't traverse subdirs as well, yu can get results of files (and/or dirs) - UNLESS you use braces, e.g. {img,docs,another_folder} - but that's getting silly.

SO you have this sort of structure:

countries
country1
f1
f2
f3
country2
f1
f2
f3
(etc)

I'm assuming you need just the f1-f3 subfolder contents, there are no images in the country1 or country2 (etc) folders themselves, or the parent 'countries' folder.

If so, try this:

foreach(glob("countries/{USA,Brazil,UK,India}/{subfolder1,subfolder2,subfolder3}/*.[jJ][pP]*[gG]", GLOB_BRACE) as $r){
    $b = basename($r);
    echo "<img src='$r' alt = '$b' /><br />"; 
}

Place all the countries into a brace, then the subfolders - I'm assuming the subfolders for each country are named the same.

Goooood.

And now another question. How can i pass the value of a javascript variable to one of php?

Member Avatar for diafol

No. You need Ajax to do that. Probably not what you're looking for. You can certainly pass php data into js though. If you need help on this, I suggest starting a new thread as this is a totally different topic. That is, once you've searched this forum and Googled a bit!

If you want to obtain just the name of the file, you could also do it as follows:

<?php
    $file = 'folder1/folder2/image.jpg';
    $file_name = substr($file, strrpos($file, '/') + 1, strrpos($file, '.') - strrpos($file, '/') - 1);
    echo $file_name; // Will output "image".

Nononono, so this image think is just a page of my html and i have another page in which i want to do something else. Here's what i did:
-i have a background that changes every 5 seconds, or change by pressing one of left or right button;
-and if you press a button in the center of the page it pop-ups a video - and now i want to make, if a certain file exists(..../javascript_indicator.txt) and if it doesn't i will do something else. All i need is to know how to pass the javascript indicator to php so that i can do certain things. Here's my ajax:

var ajaxRequest;  // The variable that makes Ajax possible!

        try{
            // Opera 8.0+, Firefox, Safari
            ajaxRequest = new XMLHttpRequest();
        } catch (e){
            // Internet Explorer Browsers
            try{
                ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
            } catch (e) {
                try{
                    ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
                } catch (e){
                    // Something went wrong
                    alert("Your browser broke!");
                    return false;
                }
            }
        }
        // Create a function that will receive data sent from the server
        ajaxRequest.open("GET", url, true); 
        ajaxRequest.send(); 

and here is the html code(not all of it) which i think i'll make it ajax.php...

            $cont = ...
            $filec="countries/Video/$cont.txt";
            if (file_exists($filec)) {
                $fh=fopen($filec,'r');
                $ur=fgets($fh);
                echo"<a href=$ur class='winDiv' rel='video'>";
                fclose($filec);
            } else {
                echo"<a href='video.php?blu=$cont' class='winDiv'>";
            }

)

So how can i get the $cont variable to be equal to a cont variable in a javascript function? Forgot to say... in the file in the first line is a youtube url.

It doesn't matter, i will not use this...

Member Avatar for diafol

Like I said, this is a different question/topic. Consider a new thread and a descriptive title.

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.