How to insert image in php

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • vikamjain
    New Member
    • Jan 2010
    • 6

    How to insert image in php

    Hii all
    It might be a silly doubt but I am puzzled a lot by it.
    Basically my application need to display uploaded file from user.
    The name of image file is variable. Lets say the path to uploaded file is stored in
    Following four are various combination of $s
    Code:
    $S. Now when i used following commands
    $s="http://localhost/myfolder/imagefolder/image.jpeg"       //with quotations
    $s="c://xampp/htdocs/myfolder/imagefolder/image.jpeg"   
    $s=http://localhost/myfolder/imagefolder/image.jpeg         //without quotations
    $s=c://xampp/htdocs/myfolder/imagefolder/image.jpeg
    Following four commands are various syntax used to display image corresponding to each of the above $s
    Code:
    echo "img src="$S" width="60" height="80" ";
    echo "img src='$S' width='60' height='80' ";
    echo "img src=$S width='60' height='80' ";
    echo "img src=".$S. "width='60'". "height='80' ";
    Last edited by Dormilich; Jan 26 '10, 11:58 AM. Reason: Please use [code] tags when posting code
  • zorgi
    Recognized Expert Contributor
    • Mar 2008
    • 431

    #2
    Code:
    echo "<img src='$S' width='60' height='80' />";

    Comment

    • vikamjain
      New Member
      • Jan 2010
      • 6

      #3
      @ Zorgi Sorry, In my question i forgot to put <> but in actual i have tried this command
      echo "<image src='$S' width='60' height='80' />";
      but it did not work for me

      Comment

      • johny10151981
        Top Contributor
        • Jan 2010
        • 1059

        #4
        give the actual code, following the rule. your provided code is disturbing.

        Comment

        • zorgi
          Recognized Expert Contributor
          • Mar 2008
          • 431

          #5
          If you did this:

          Code:
          echo "<img src='$S' width='60' height='80' />";
          and image is not displayed than your path $S must be wrong.

          Comment

          • vikamjain
            New Member
            • Jan 2010
            • 6

            #6
            helllo jorgi I used follwing four kind of pathnames in $s
            Code:
            $s="http://localhost/myfolder/imagefolder/image.jpeg" //with quotations
            $s="c://xampp/htdocs/myfolder/imagefolder/image.jpeg"
            $s=http://localhost/myfolder/imagefolder/image.jpeg //without quotations
            $s=c://xampp/htdocs/myfolder/imagefolder/image.jpeg
            but none of them worked
            Last edited by Dormilich; Jan 26 '10, 12:00 PM. Reason: Please use [code] tags when posting code

            Comment

            • zorgi
              Recognized Expert Contributor
              • Mar 2008
              • 431

              #7
              Well its difficult for me to tell where are your image files but it looks to me as if you should do some reading on absolute and relative paths. There is loads of articles on internet on the subject. Just google for it. Here is one i found. I have't read it but I hope it helps.



              Googd luck

              Comment

              • kovik
                Recognized Expert Top Contributor
                • Jun 2007
                • 1044

                #8
                <image> and <img> are not the same.

                Comment

                • vikamjain
                  New Member
                  • Jan 2010
                  • 6

                  #9
                  Here is my code

                  Code:
                  <h2> Here is your Quiz</h2>
                  <?php
                  $i=0;
                  
                  echo "<table border=\"1\" align=\"top\">";
                  echo "<tr><th>Question no</th>";
                  echo "<th>Question </th>";
                  echo "<th>Hint</th>";
                  echo "<th>Answer</th>";
                  echo "<th>Picture</th></tr>";
                  echo "<tr><td>";
                  while($i<$num)
                  {
                    $qnn= mysql_result($result,$i, "Question_no");
                    $qn= mysql_result($result,$i, "Question");
                    $hint=mysql_result($result,$i, "Hint");
                    $ans=mysql_result($result,$i, "Answer");
                    $pic=mysql_result($result,$i, "Picture");
                    $pic1="http://localhost/test2/uploaded_files/1264644980-11_17250697.jpg";
                    echo $pic; 
                    // $pic on echo command gives [url]http://localhost/test2/uploaded_files/1264644980-11_17250697.jpg[/url]
                     echo $pic1;
                    // $pic1 on echo command gives [url]http://localhost/test2/uploaded_files/1264644980-11_17250697.jpg[/url] 
                  
                          echo "<tr><td>";
                  	echo $qnn;
                  	echo "</td><td>";
                  	echo $qn;
                  	echo "</td><td>";
                          echo $hint;
                          echo "</td><td>";
                          echo $ans;
                          echo "</td><td>";
                          echo "<img src='$pic1' />";
                          echo "</td></tr>";      
                  
                  $i+=1;
                    
                  }
                  
                  ?>

                  //Here Value of Picture is passed using following command in some other file
                  // $s='http://' . $_SERVER['HTTP_HOST'] . $directory_self .'/uploaded_files/'.$now.$_FILES[$fieldname]['name'];
                  Last edited by Dormilich; Jan 28 '10, 05:04 AM. Reason: Please use [code] tags when posting code

                  Comment

                  • vikamjain
                    New Member
                    • Jan 2010
                    • 6

                    #10
                    here all the connection to database are done in some other file. This file was included in that file so connection won't be a problem. $pic1 is showing the picture while $pic is not showing the picture when used in the command
                    echo "<img src='$pic or $pic1' />";

                    Comment

                    • kovik
                      Recognized Expert Top Contributor
                      • Jun 2007
                      • 1044

                      #11
                      Have you tried verifying that the image URL is correct by directly visiting it?

                      Comment

                      • vikamjain
                        New Member
                        • Jan 2010
                        • 6

                        #12
                        As I said before I have given the actual url value in $pic1. which is working fine and when I used $pic which has the same value as $ pic1(which is evident from this fact that both are giving same thing when echo command is used)

                        Comment

                        • zorgi
                          Recognized Expert Contributor
                          • Mar 2008
                          • 431

                          #13
                          Hm, when things are this confusing I consult firebug. http://getfirebug.com/

                          Comment

                          • kovik
                            Recognized Expert Top Contributor
                            • Jun 2007
                            • 1044

                            #14
                            You said that neither is working. You'll need to be much clearer if you want to get this solved.

                            If one works and the other doesn't, then they are NOT equal, regardless of what you think. Double-check them by copying the URL that they echo from the HTML in the <img> tag. Also, compare the two values using the "===" operator.

                            Comment

                            Working...