Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP File Handling with PHP Parsing Specific Formats Reading CSV

Jonathan Grieve
MOD
Jonathan Grieve
Treehouse Moderator 91,253 Points

PHP: Reading the image URLS

I think i've got the gist of reading these CSV values but when I tested it on my machine (not workspaces admittedly), only one of the images was showing up... Elizabeth Smith. Her URL was actually the only direct link to Twitter in the CSV file.

Is this a mistake or something to catch us out, I wonder? Also 3 of the images use .jpg rather than .jpeg extensions.

Am I good to edit the images myself to get them to work?

Thanks! :-)

<?php
/*
 * read csv of contacts
 */
$title = 'People Recommendations';
require 'inc/header.php';

/*echo '<pre>';
include 'data/csv/people.csv';
echo '</pre>';*/


//open a file for reading
if(($handle = fopen('data/csv/people.csv', 'r')) !== false) {   

    //read CSV line by line  - returning an array of columns    
    $header = fgetcsv($handle, ','); 

    //var_dump($header);    
    extract(array_flip($header));    
    //echo $first;    

    echo '<div class="row">';
        $count=0;
        while (($contact = fgetcsv($handle)) !== false) {
            if($count > 0 && $count % 4 == 0) {
            echo "</div>\n";
            echo '<div class="row">';

        } 
        $count++;
        echo '<div class="col-xs-6 col-md-3">';
        echo '<div class="thumbnail">';
        echo '<img src="'. $contact[$img] . '" />';
        echo '<div class="caption">';
        echo '<h4 class="media-heading">' . $contact[$first] .  ' '  . $contact[$last] . '</h4>';
        echo '<a href="' . $contact[$website]. '" target="_blank">' . $contact[$website]. '</a>';
        echo '<br />';
        echo 'Twitter: <a href="https://twitter.com/' . $contact[$twitter] .  '" target="_blank">@' . $contact[$twitter]  . '</a>';
        echo '</div>';
        echo '</div>';
        echo '</div>';
    }
echo '</div>';
}

 fclose($handle); 

 require 'inc/footer.php';

Snippet

people.csv
Sammy Kaye,Powers,sammyk.me,SammyK,/img/aLTMT0LS_400x400.jpeg
Eli,White,eliw.com,EliW,/img/9KKDoUU-_400x400.jpg
Beth,Tucker Long,treelinedesign.com,e3BethT,/img/fgbDHP7h_400x400.png
Adam,Culp,geekyboy.com,adamculp,/img/n4Y3CzVa_400x400.png
Chris,Cornutt,blog.phpdeveloper.org,enygma,/img/NI5JOVA5_400x400.jpeg
Elizabeth,Smith,emsmith.net,auroraeosrose,https://pbs.twimg.com/profile_images/25760052/headshot_400x400.jpg
Jeremy,Mikola,jmikola.net,jmikola,/img/WUhPQ2l__400x400.png
Genevieve Herres
Genevieve Herres
Courses Plus Student 3,457 Points

Only the Elizabeth Smith image has an absolute URL. The others all use a relative URL. This means if you don't have the correct image files in that relative location to where your php file is, you won't see the image. If you are using a unix based machine, it's going to look in the root directory of your file system for that img directory. If you have a web server running on your local machine, then it will look in the root of wherever the web server is.

Jonathan Grieve
Jonathan Grieve
Treehouse Moderator 91,253 Points

Yes the image files are in the location they should be.

let me fire up the directory again just to be sure.

Well it looks like if I omit the forward slash in each image entry in the CSV file it will work. Guess it's all just a case of video and student working on different systems! :-)

2 Answers

Genevieve Herres
PLUS
Genevieve Herres
Courses Plus Student 3,457 Points

It's not so much the systems, as the underlying concept of beginning a relative location with / or not. On the Workspace in treehouse, your user permissions don't let you go outside of your directory, so /img is the same as img. However, on your own computer, you can go outside of your current directory, so /img and img are two very different directories.

This happens a lot when working on a website.

Let's say you're in the directory public_html and then the subdirectory my_project. If you request img from here it will look for public_html/my_project/img. However, if you request /img, it will look for public_html/img. That beginning / goes to the highest level that you have access to.

Many many problems in web applications have come from the wrong file reference, frequently just a / or not a /.

I just remove all the first front slashes of /img in people.csv file