SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Addict
    Join Date
    May 2006
    Posts
    349
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    how to add an image in with other files?

    Hello, I'm trying to get this code to show the images along with other files but it won't work. I tried adding an image tag and that didn't help, didn't know how to include an if statement if its a ".gif " or something. Can someone please tell me how to include an image in with the rest of these in the array? Thanks very much.

    PHP Code:
    <?php
    ini_set
    ('error_reporting'8191);
    ini_set('display_startup_errors'1);
    ini_set('display_errors'1);
    $maximum 3
    $a = (isset($_COOKIE['mb']) && intval($_COOKIE['mb']) < $maximum)? intval($_COOKIE['mb'])+1
    setcookie("mb"$atime()+60*60*24*180); 
    $currentFile $a;
    $allfiles  = Array();
    $allfiles[1] = "thisfile.txt"
    $allfiles[2] = "thatfile.html";
    $allfiles[3] = "thatfile.gif";
    $get_content file_get_contents($allfiles[$currentFile]);
    echo 
    $get_content;
    ?>

  2. #2
    SitePoint Enthusiast
    Join Date
    Nov 2006
    Posts
    71
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    file_get_contents() literally sticks the contents of a file into the PHP code. It doesn't automatically wrap <img> tags around images or anything like that (which is required to display an image on an HTML page). Adding <img> tags to the array value (i.e. $allfiles[3] = '<img src="thatfile.gif" />') won't work either, not only because I didn't put a semi-colon (stupid winking smiley...) but also because PHP will try and find a file named <img src="thatfile.gif" /> (including the <img> tags etc.).

    You'd have to use something simple like:

    Code:
    echo '<img src="' . $allfiles[3] . '" />';
    And if that doesn't work... you must have your image path wrong or something, because it should work.

  3. #3
    SitePoint Enthusiast
    Join Date
    Jun 2006
    Posts
    70
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    like this?

    PHP Code:
    if (in_array(substr($allfiles[$currentFile], -3), array('gif','jpg','png')))
      echo 
    '<img src="'.$allfiles[$currentFile].'">';
    else 
    {
      
    $get_content file_get_contents($allfiles[$currentFile]);
      echo 
    $get_content;

    that code should replace these 2 lines:
    PHP Code:
    $get_content file_get_contents($allfiles[$currentFile]);
    echo 
    $get_content
    Be careful though, if you're not sure where your image files are from, you should check the image type first with getimagesize function.

    ps. when I preview my reply, I saw there's a reply already, but I send mine anyway because I had typed it

  4. #4
    SitePoint Addict
    Join Date
    May 2006
    Posts
    349
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Explorer, thats exactly what I needed, thanks very much, didn't know how to do it. If I have more than 3 image files I can just then change the -3 to -4?

    There is only one problem happening, the file echo works fine but if there is an image the image jumps out of the page and appears somewhere where it wants. It looks like it will only keep one echo in the page and not sure why. Any way to keep both echos inside a web page? Please let me know thanks a lot.

    Image echo doesn't stay near the other echo in a web page.
    PHP Code:
    if (in_array(substr($allfiles[$currentFile], -3), array('gif','jpg','png')))  
    echo 
    '<img src="'.$allfiles[$currentFile].'">';
    else {  
    $get_content file_get_contents($allfiles[$currentFile]);  
    echo 
    $get_content;} 
    Last edited by 1Jen; Aug 22, 2007 at 19:00.

  5. #5
    SitePoint Enthusiast
    Join Date
    Jun 2006
    Posts
    70
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If I have more than 3 image files I can just then change the -3 to -4?
    No, -3 tells substr to take 3 chars starting from right position from string (see: http://php.net/substr), thus in the code above, it gets the extension of file name supplied.

    I'm not surely understand your 2nd question, but I guess it depends on surrounding html code for that page (if you inserting that php code in the middle of html code). Maybe if you can provide your url or if you don't want to show it to public you can pm me, so I can see what the real problem is.

  6. #6
    SitePoint Addict
    Join Date
    May 2006
    Posts
    349
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Its working now, thanks everybody.
    Last edited by 1Jen; Aug 22, 2007 at 22:14.


Bookmarks

Posting Permissions

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