Hi tendral,

If you simply want to display the image then why don`t you use simple HTML image tag to do that.

$path="/bookstore/basic_cooking.jpg";
<img src="<?php echo $path; ?>" width="image width" height="image height" />

Thanks,
regards,
niroshan

    I agree with niroshan's advice, though you may wish to apply [man]htmlspecialchars/man nonetheless.

      Though the above replies have merit, I thought I'd address your original problem as well (since it was a fundamental misunderstanding). Your problem was your path:

      $path="/bookstore/basic_cooking.jpg";

      Because this path begins with a '/', you're specifying an absolute path. You're telling PHP that the "bookstore" folder is found in the root of the drive ("/") of the server. This is almost certainly not so - the full absolute path is probably more like "/home/users/username/www/bookstore/" - something along those lines.

      If you want to use an absolute path that starts at the root of your website, you could use the $_SERVER['DOCUMENT_ROOT'] variable and append the path onto the end of that. Otherwise, try using a relative path (e.g. "bookstore/..", which says the "bookstore" folder is in the same directory as the script).

        niroshan;10948657 wrote:

        Hi tendral,

        If you simply want to display the image then why don`t you use simple HTML image tag to do that.

        $path="/bookstore/basic_cooking.jpg";
        <img src="<?php echo $path; ?>" width="image width" height="image height" />
        

        Thanks,
        regards,
        niroshan

        Hi niroshan,

        what i'm trying to do is match the selected record's title field with the jpg file name. If it is the same, then display the book cover jpg pic.

        I don't know how to access the jpg files in the bookstore folder and then verify if the jpg file name matches with the selected records's title field.

        I hope u get what I mean.

        thanks.

          I think the only way this can be done is to open the folder where all the jpg files are located, create an array, load the array with the file names. Then, compare each file name with the selected row's title field by looping. Finally display the jpg file.

          I will see if I do this now.

            HI tendral,

            I have few questions regarding this before answer your question.

            1. When are you uploading book cover jpg files into that bookstore folder? meaning do you upload the cover image when you insert the book details from backend or something?

            2. How do you rename the book cover images? Are they having the same name as book title?

            if you having the same name as book title for the images then simply you can use file_exists() function to check whether book cover exists for that particular book title.

            Also remember I have seen images have underscore ( _ ) in their names but im sure book title wont contain underscores. so make sure you replace all the spaces in the title with underscore before checking the image name.

            Hope this clear.

            Regards,
            niroshan

              Don`t read the whole folder... use file_exists()... its much faster

                Hello Niroshan,

                Finally, I've come up with the code below. Everything works fine, $book_cover="Basic Cooking.jpg" but the img src is not working. The picture is not getting displayed. The code in red is causing problem.

                <?php
                if (file_exists("images/book_cover"))
                 {
                     if (is_dir("images/book_cover"))
                      {$dh = opendir("images/book_cover") or die ("Directory Open failed!");
                	while ($file = readdir($dh))
                	   { //echo "$file\n";
                	     $book_cover_str = explode(".",$file);
                	     /*echo $book_cover_str[0];
                	     echo $book_cover_str[1];*/
                
                 if ($book_cover_str[0]=="Basic Cooking")
                   {echo "HURRAY!";
                     $book_cover = $file;
                     // echo $book_cover;
                
                     [COLOR="Red"] print "<img src='$book_cover' />"; [/COLOR]			    
                     }
                
                }
                         /*echo "Directory was opened successfully";*/
                closedir($dh);
                      }
                }	
                 ?>

                Please help me!
                Thanks.

                  Hi tendral,

                  if (file_exists("images/book_cover"))

                  This is not i really meant. Because since you have created the folder called 'book_cover' its really no use of checking it using file_exists(). (because we know that it exists anyway 🙂)

                  you should use file_exists() function to check the actual image file.

                  By the way you haven`t answered any of my question and if you would have answered, then it was much easy for us to solve your problem.

                  Since you haven`t answered my second question im assuming the image name is same as book title. so in this case what you can do is,

                  
                  $title = 'Basic Cooking';
                  $fileName = 'images/book_cover/'.str_replace(' ','_',trim($title)).'.jpg';
                  
                  if(file_exists($fileName))
                  {
                  	print "<img src='".$fileName."' />"; 	
                  }
                  
                  

                  there are several important points that you need to remember,

                  1. make sure you dont have spaces in between image file name. (I have used str_replace() to change the ' ' into '_')
                  2. use one case for files (Either uppercase or lowercase because in linux case does matters)

                  Hope this helps.

                  Best regards,
                  Niroshan

                    niroshan;10948756 wrote:

                    HI tendral,

                    I have few questions regarding this before answer your question.

                    1. When are you uploading book cover jpg files into that bookstore folder? meaning do you upload the cover image when you insert the book details from backend or something?

                    2. How do you rename the book cover images? Are they having the same name as book title?

                    if you having the same name as book title for the images then simply you can use file_exists() function to check whether book cover exists for that particular book title.

                    Also remember I have seen images have underscore ( _ ) in their names but im sure book title wont contain underscores. so make sure you replace all the spaces in the title with underscore before checking the image name.

                    Hope this clear.

                    Regards,
                    niroshan

                    Answers to your questions:
                    1) Yes, I'm creating a book cover for each title inserted in database

                    2)Yes, it is the same: title and book cover image has the same name.

                    Is it ok if the image name doesn't contain underscore like the title?

                    I apologize for not answering your questions earlier.

                    thank you.

                      Hi tendral,

                      Is it ok if the image name doesn't contain underscore like the title?

                      Is there any special reason why you want to have spaces for the file name?
                      In linux its considered to be a bad practice to have spaces between file names. because if you want to do any file manipulations on linux environment then you always have to use quotes to refer that file.

                      So since your renaming and uploading the cover image when you insert the book details, what you can do is use a simple str_replace() function and get rid of all the spaces.

                      str_replace(' ','_',trim($title)).'<image extension >'
                      

                      By the way did you tried my code snap-it. Is that what you want?

                      Regards,
                      Niroshan

                        Hello Niroshan,

                        This is my final code. But NOTHING is getting displayed except 3 dots for echo $file.
                        right now
                        format for title field ($title) is Basic Cooking

                        format for image file ($file) is basic_cooking.jpg
                        I also have gif images in the folder.

                        $dh = opendir("images/book_cover") or die ("Directory Open failed!");	
                        while (false !== ($file = readdir($dh)))
                            { 
                                 if (file_exists($file))      
                        {[COLOR="Red"]echo $file;[/COLOR] $book_cover_str = explode(".",$file); /*echo $book_cover_str[0]; echo $book_cover_str[1];*/ $title="Basic Cooking"; $title_str = str_replace(' ','_',trim($title)); if ($book_cover_str[0]==$title_str) {echo "HURRAY!"; $fileName = 'images/book_cover/'.trim($file); echo $fileName; //print "<img src='".$fileName."' />"; }//if ($book_cover_str[0]==$title_str) }//if (file_exists($file)) }//while closedir($dh);

                        I'm using
                        while (false !== ($file = readdir($dh)))
                        instead of
                        while ($file == readdir($dh)))
                        coz' I read http://php.net/manual/en/function.readdir.php

                        Does the capital letters in title as in Basic Cooking matter in linux?

                        Something is terribly wrong, please help!

                        THANK YOU

                          Hi tendral,

                          Does the capital letters in title as in Basic Cooking matter in linux?

                          Of course it matters. Linux is case sensitive.

                          Regards,
                          Niroshan

                            Hi Niroshan,

                            I changed Basic Cooking to basic cooking but still no proper output in the browser except 3 dots (...). Do u know what mistake I am making? 🙁

                            thanks,
                            Vaani

                              Hi,

                              Try this,

                              $dh = opendir("images/book_cover") or die ("Directory Open failed!");	
                              while (($file = readdir($dh)) !== false)
                              { 
                              	if($file <> '.' && $file <> '..')
                              	{
                              
                              	$book_cover_str = explode(".",$file);
                              
                              	$title="basic cooking";
                              	$title_str = str_replace(' ','_',trim($title));
                              
                              	if ($book_cover_str[0]==$title_str)
                              	{
                              		$fileName = 'images/book_cover/'.trim($file); 
                              		echo $fileName;
                              
                              	}
                              }
                              }
                              
                              closedir($dh);
                              

                              But what i dont understand is your requirement. Your just trying to find whether specific file exists in the image folder and if exists display it right.

                              If so then why do you read through all the folder. assume if you have 1000 titles are you trying to repeat this code for 1000 times?

                              so why dont you do this as i told you earlier.

                              $title = 'Basic Cooking'; 
                              $fileName = 'images/book_cover/'.str_replace(' ','_',trim($title)).'.jpg'; 
                              
                              if(file_exists($fileName)) 
                              { 
                                  print "<img src='".$fileName."' />";      
                              }

                              in this case its more dynamic where you check the specific file (not whole directory though a loop) and if that file exists display or do what ever you like else simply ignore.

                              Sorry if i misunderstood your requirement and hope this helps.

                              Regards,
                              niroshan

                                tendral;10948855 wrote:

                                Hi Niroshan,

                                I changed Basic Cooking to basic cooking but still no proper output in the browser except 3 dots (...). Do u know what mistake I am making? 🙁

                                thanks,
                                Vaani

                                By the way i forgot to tell you why your code failed. It failed because inside the while loop you check file_exists() and you pass the file name as an argument. but obviously the file_exists() function will return false after '.' and '..' because you dont specify the full path to the file. so that function check the file inside the working directory and returns false.

                                Hope this clear things up for you,

                                Best regards,
                                niroshan

                                  Hi Niroshan,

                                  You are right, I don't actually need the while loop.

                                  The following is my final code and it works well. The images are getting displayed.

                                  while ($row = mysql_fetch_array($result))
                                  { 
                                       print '<tr><th align=left>'. $row["title"].', '.$row["author"]. '</th></tr>'; 
                                       print '<tr>';
                                       print '<td>';
                                       print '<table cellspacing=10 cellpadding=10>';//2b-1
                                       print '<tr>';
                                      $fileName = 'images/book_cover/'.str_replace(' ','_',trim(strtolower($row["title"]))).'.gif'; 
                                       if(file_exists($fileName)) 
                                  	print "<td><img src='".$fileName."' /></td>";
                                       else
                                  	{$fileName = 'images/book_cover/'.str_replace(' ','_',trim(strtolower($row["title"]))).'.jpg';
                                                    if(file_exists($fileName)) 
                                  	      print "<td><img src='".$fileName."' /></td>";
                                                   }
                                  

                                  thanks a lot for all your help!

                                    hello Niroshan,

                                    there is one small problem now. For the title OCP Developer PL/SQL Exam Prem,
                                    How can I convert it to file format coz' of the /? What function can I use to ignore /?

                                    the final image file name has to be ocp_developer_plsql_exam_prep

                                    thank you.

                                      Write a Reply...