help with news script with image upload feature (limage not displays when not added)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • azegurb
    New Member
    • Sep 2009
    • 32

    help with news script with image upload feature (limage not displays when not added)

    Hi
    i have taken news script with image upload feature
    it works fine.
    it show image and its being getting smalled thumbnail at the right side
    but when i added news without image instead of image it displays red instead of little (thumbnail) image X sign you can see script from here
    you can go here http://aziko.6te.net/yukle/haber_ekle.php
    and you can view the news you added from here http://aziko.6te.net/yukle/
    here is full code
    Thanks for attention
    Attached Files
  • Atli
    Recognized Expert Expert
    • Nov 2006
    • 5062

    #2
    Hey.

    You need to make sure there is an image attached to the article before you try to display it. - The file_exists function should help with that.

    P.S.
    It's generally best to write your code in English. Makes it more "portable".
    It's not fun having to translate all your code because the people who ended up using it don't understand a word of it. (Trust me, I've been there xD)

    Comment

    • azegurb
      New Member
      • Sep 2009
      • 32

      #3
      ok
      i will translate it

      Comment

      • Atli
        Recognized Expert Expert
        • Nov 2006
        • 5062

        #4
        Originally posted by azegurb
        ok
        i will translate it
        No need to do that on my account. It's just something to keep in mind. (I won't be reading all that code anyways.)

        It would perhaps be best if you just posted the parts of the code you are having trouble with, and translated those so we can make sense of it.

        Comment

        • azegurb
          New Member
          • Sep 2009
          • 32

          #5
          image_add.php

          Code:
          <?php
            function resample($image,$max_width,$max_height) 
               { 
          
               # Icerik icin kesi baslat ... 
               ob_start(); 
          
               # Ilk heightutlar 
               $boyuut = getimagesize($image); 
               $width    = $boyut[0]; 
               $height   = $boyut[1]; 
          
               # new heightutlar 
               $x_oran = $max_width  / $width; 
               $y_oran = $max_height / $height; 
          
               if (($width <= $max_width) and ($height <= $max_height)){ 
                  $son_width  = $width; 
                  $son_height = $height; 
                  } 
               else if (($x_oran * $height) < $max_height){ 
                  $son_width  = $max_width; 
                  $son_height = ceil($x_oran * $height); 
                  } 
               else { 
                  $son_width  = ceil($y_oran * $width); 
                  $son_height = $max_height; 
                  } 
          
               # old ve new imageler 
               $old = imagecreatefromjpeg($image); 
               $new = imagecreatetruecolor($son_width,$son_height); 
          
               # old image
               imagecopyresampled( 
                  $new,$old,0,0,0,0, 
                  $son_width,$son_height,$width,$height); 
          
               # new image
               imagejpeg($new,null,-1); 
               $icerik = ob_get_contwidthts(); 
          
              
               ob_widthd_clean(); 
               imagedestroy($old); 
               imagedestroy($new); 
          
               return $icerik; 
          
           }
          ?>
          add_news.php
          Code:
          <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
          <html xmlns="http://www.w3.org/1999/xhtml">
          <head>
          <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-9" />
          <title>Dahi Tasarim</title>
          <script type="text/JavaScript">
          <!--
          function MM_jumpMenu(targ,selObj,restore){ //v3.0
            eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
            if (restore) selObj.selectedIndex=0;
          }
          //-->
          </script>
          </head>
          
          <body>
          <p>
          <? 
          include("connect.php"); 
          //include ("oturum_kontrol.php");	
          //include ("yonetim_menu.php");
          include ("image_add.php"); 
          ?>
          
          </p>
          <p style="font-size:24pt; color:#FF0000;">Haber Ekle </p>
          
          <hr />
          News :&nbsp;
          <? /
          
          $no=$_POST[no];
          $title=$_POST[title];
          $subject=$_POST[subject];
          $description=$_POST[description];
          $aktiv=$_POST[aktiv];
          $search=$_POST[search];
          $show=$_POST[show];
          
          // image settings
          	$little_pic="little_pictures"; // open this directory
          	$large_pic="large_pictures"; // and give permission chmod 777
          
          	$large_pic_width=640;
          	$large_pic_heigth=480;
          
          	$little_pic_width=200;
          	$little_pic_heigth=150;
          	
          	
          	
          //if ($image)
          
          ?>
          <?  if ($_POST[del])
          {// if pressed del button
          $query=mysql_query("select * from news where no='$no'");
          while($row=mysql_fetch_row($query))
          	
          	{ @unlink($row[4]);
          		@unlink($row[5]); 
          	}
          
          if (mysql_query("delete from news where no='$no'")) echo "news deleted";
          else echo "not deleted";
          
          }
          ?>
          <? 
          if ($_POST[edit])
          {
          
          $query=mysql_query("select * from news where no='$no'");
          	while($row=mysql_fetch_row($query))
          	{ 
          	$no2=$row[0];
          	$baslik2=$row[1];
          	$ozet2=$row[2];
          	$aciklama2=$row[3];
          	$kucukresim2=$row[4];
          	$resim2=$row[5];
          	$aktif2=$row[7];
          	}
          }
          ?>
          <? if($_POST["add"])
          { //echo "
          if(strlen($_FILES["image"]["name"])>4)
          		{// 
          		 // 
          		 // 
          		$find=$_FILES["image"] ;
          		$tobesearched = array('ç','Ç','ý','Ý','ð','Ð','ü','ö','Þ','þ','Ö','Ü',',',' ','(',')','[',']'); 
          		$change  = array('c','C','i','I','g','G','u','o','S','s','O','U','','_','','','',''); 
          	
          		$_FILES["image"]=str_replace($tobesearched, $change, $find);
          
          		$image = "$large_pic/".$_FILES["image"]["name"]; 	   
          		move_uploaded_file($_FILES["image"]["tmp_name"],$image);
          		
          		$little_image  = "$little_pic/".$_FILES["image"]["name"];  
          		move_uploaded_file($_FILES["image"]["tmp_name"],$little_image);
          
          		
          		// little image resampling
          		$prepare = resample($image,$little_pic_width,$little_pic_heigth); 
          		$folder  = fopen ($little_image,"w+"); 
          				   fwrite($folder,$prepare); 
          				   fclose($folder); 
          				   
          		// large image resampling
          		$prepare = resample($image,$large_pic_width,$large_pic_heigth); 
          		$folder  = fopen ($image,"w+"); 
          				   fwrite($folder,$prepare); 
          				   fclose($folder); 
          
          		// end of resampling
          		
          		}
          
          
          if (!$no)
          	{
          		
          	if (mysql_query("insert into news values ('','$title','$subject','$description','$little_image','$image','$aktiv')"))
          	{echo " done"; $image="";$little_image="";}
          	else echo " not added";
          	
          	}
          else 
          { // 
          	if ($image) $imageadd=",image='$image',littleimage='$little_image'";
          	if (mysql_query("update  news set title='$title',subject='$subject',description='$description',aktiv='$aktiv' $imageadd where no='$no'"))
          	{echo " done"; $image="";$little_image="";}
          	else echo " not added ";
          	}
          }
          ?>
          <hr />
          
          <form id="form1" name="form1" method="post" action="">
           c urrent news :
            <label>
            <select name="no" id="no">
            <?
            $query=mysql_query("select * from news order by no desc");
          while($row=mysql_fetch_row($query))
          {
          echo "<option value='$row[0]'>$row[1]</option>";
          }
            ?>
              
            </select>
            </label>
            <label>
            <input name="edit" type="submit" id="edit" value="edit" />
            </label>
            <label>
            <input name="del" type="submit" id="del"  value="del" />
            </label>
            <label></label>
          </form>
          <hr />
          <form id="form2" name="form2" method="post" action=""   enctype="multipart/form-data">
            <table width="800" border="1" cellspacing="5" cellpadding="0">
              <tr>
                <td width="129">no 
                <input name="no" type="hidden" value="<? echo $no2; ?>" /></td>
                <td width="650"><label></label>	  </td>
              </tr>
              <tr>
                <td>image
           <? if($little_image2)
          
          {echo "<img src=$little_image2 align=right><br />";
          //echo "<input name='kucukresim' type='hidden' id='kucukresim' value='$kucukresim2' />";
          }
          ?>
          </td>
                <td>   <label>
                <input name="image" type="file" id="image" />
                </label></td>
              </tr>
              <tr>
                <td>title </td>
                <td><input name="title" type="text" id="title" value="<? echo $title2;?>" size="100" /></td>
              </tr>
              <tr>
                <td>subject</td>
                <td><input name="subject" type="text" id="subject" value="<? echo $subject2;?>" size="100" /></td>
              </tr>
              <tr>
                <td>description</td>
                <td><label>
                  <textarea name="description" cols="80" rows="5" id="is"><? echo $description2;?></textarea>
                </label></td>
              </tr>
              <tr>
                <td>Aktiv</td>
                <td><label>
                  <input <?php if ($aktiv2=="aktiv") {echo "checked=\"checked\"";} ?>  name="aktiv" type="radio" value="aktiv" checked="checked" />
               activate
                <input <?php if ($aktiv2=="pasiv") {echo "checked=\"checked\"";} ?>  name="aktiv" type="radio" value="pasiv" />
                passivate</label></td>dffdg
              </tr>
              <tr>
                <td>&nbsp;</td>
                <td><input name="edit" type="submit" id="edit" value="edit" />
                  <label>
                  <input name="search" type="submit" id="search" value="search" />
                  <input name="show" type="submit" id="show" value="show all news" />
                  </label></td>
              </tr>
            </table>
          </form>
          
          <p>&nbsp;</p>
          <? 
          if (($search) || ($show))
          {
          if ($search)
          {
          if ($title) $query=mysql_query("select * from news where title like '%$title%'");
          if ($subject) $query=mysql_query("select * from news where subject like '%$subject%'");
          if ($description) $query=mysql_query("select * from news where description like '%$description%'");
          }
           if ($show)
          {
          $query=mysql_query("select * from news order by no desc");
          }
          echo "<table border=1>
          <tr>
          <td>edit</td>
          <td>del</td>
          <td>No</td>
          <td>baslik</td>
          <td>subject</td>
          <td>description</td>
          <td>littleimage</td>
          <td>aktiv</td>
          </tr>";
          while($row=mysql_fetch_row($query))
          {echo "
          <tr><form action=\"\" method=\"post\"><input name=\"no\" type=\"hidden\" value=\"$row[0]\">
          
          <td><input name=\"edit\" type=\"submit\" value=\"edit\">	</td>
          <td><input name=\"del\" type=\"submit\" value=\"del\"></td>
          
          <td>$row[0]&nbsp;</td>
          <td>$row[1]&nbsp;</td>
          <td>$row[2]&nbsp;</td>
          <td>$row[3]&nbsp;</td>
          <td><img src=$row[4]>&nbsp;</td>
          <td>$row[6]</td>
          </form>	</tr>";
          }
          echo "</table>";
          }
          ?>
          </body>
          </html>
          index.php

          Code:
          <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
          <html xmlns="http://www.w3.org/1999/xhtml">
          <head>
          <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-9" />
          <title>Dahi Tasarim . CoM</title>
          <style type="text/css">
          <!--
          a:link {
          	color: #0066FF;
          	text-decoration: underline;
          }
          a:visited {
          	text-decoration: underline;
          	color: #0033CC;
          }
          a:hover {
          	text-decoration: none;
          	color: #FFFFFF;
          }
          a:active {
          	text-decoration: underline;
          }
          -->
          </style></head>
          
          <body>
          
          <? // baglan php yi yukluyoruz
          include("connect.php"); 
          // bu satýrda adres satýrýndan (GET metodu) no bilgisi gelmiþmi diye kontrol ediyoruz...
          $no=$_GET["no"];
          //eger $no yoksa demekki talep edilen bir newsin devami yok
          //biz en son kaydi veritabanýndan cekiyoruz 
          if (!$no)
          {$query=mysql_query("select * from news where aktiv='aktiv' order by no desc limit 1");
          }
          else // eger $no varsa istenilen nolu newsi veritabanýndan cekiyoruz
          {$query=mysql_query("select * from news where no='$no'");
          }
          while($satir=mysql_fetch_row($query))
          {
          $baslik=$satir[1];
          $ozet=$satir[2];
          $metin=$satir[3];
          $kucukresim=$satir[4];
          $buyukresim=$satir[5];
          $metin=str_replace ("\n","<br />",$metin);
          // \n br ile admin sayfasýnda aciklama kismina girdigimiz enterlari
          // br ye cevirip alt satir yapiyoruz
          }
          ?>
          
          
          
          
          
          
          <table border="1" cellpadding="10" cellspacing="10" bordercolor="#666666" bgcolor="#CCCCCC">
            <tr>
              <td width="640" valign="top" bgcolor="#FFCC00"><img alt="" src="<?=$buyukresim; ?>" />
                                        <div class="indent1">
                                        	<h1><?=$baslik; ?> <span></span></h1>
              <p><?=$metin; ?></p></td>
              <td width="200" valign="top" bgcolor="#FF9900">
          
          <? 
          
          $query=mysql_query("select * from news where aktiv='aktiv' order by no desc limit 4");
          // en son 4 newsi veritabanindan cekiyoruz
          // baslik, kucuk resimleri ve ozetlerini ekrana basacagiz
          while($satir=mysql_fetch_row($query))
          {
          $no=$satir[0];
          $baslik=$satir[1];
          $ozet=$satir[2];
          $metin=$satir[3];
          $kucukresim=$satir[4];
          $buyukresim=$satir[5];
          echo "
          <p><a href='index.php?no=$no'>
          <img alt='' src='$kucukresim' /><br><span>$baslik</span></a><br />$ozet
          </p>";
          // index.php?no=$no ile istenilen newse link olusturuyoruz
          }
          ?></td>
            </tr>
            <tr>
              <td colspan="2" bgcolor="#669933"><ul>
               
                  <?
          $query=mysql_query("select * from news where aktiv='aktiv' order by no desc limit 0,20");
          // newslerin sadece basliklarini ekranda gosterip link verecegiz
          while($satir=mysql_fetch_row($query))
          {
          $no=$satir[0];
          $baslik=$satir[1];
          echo "<li> <a href=index.php?no=$no>$baslik</a></li>";
          }
          ?>
                 
              </ul></td>
            </tr>
          </table>
          <? 
          if ($query) mysql_free_result($query);
          if ($connect) mysql_close($connect); 
          ?>
          </body>
          </html>
          i will wait your responses
          thanks for attention

          Comment

          • Atli
            Recognized Expert Expert
            • Nov 2006
            • 5062

            #6
            That's hundred of lines of code! I just needed the part that was causing the problem, not the whole thing.

            In your index.php file - around line #80 - you are printing the HTML that shows the list of articles, and their accompanying thumbnail images. In that part of your code, you simply need to make sure the image file exists (see the link I posted earlier) before you print it. If it doesn't exist, replace it with a place-holder or simply remove the <img> tag.

            Comment

            • azegurb
              New Member
              • Sep 2009
              • 32

              #7
              thank you
              but exactly i dont know it is dependent on haber_ekle.php or index.php
              because i did so much thinks in index.php
              but i got nothink
              if it is index.php can you show where to paste the code
              code that you said
              thank you for attention

              Comment

              • Atli
                Recognized Expert Expert
                • Nov 2006
                • 5062

                #8
                Originally posted by azegurb
                if it is index.php can you show where to paste the code
                code that you said
                Index.php, lines #71 through #84. - This is where your HTML for the article headlines is being printed. This is what you need to change to replace/remove the thumbs that are broken.

                The query fetches the article data from the database - and ignoring for now the fact that you are putting invalid links into the database in the first place - you need to simply verify that the image is valid before printing the <img> tag.

                It's not all that complex. You just use the file_exists function on the image location and print the appropriate HTML based on what it returns.

                Try it. Post your attempts here. (Just the alterations please, we don't need another 400 lines of code!)

                Comment

                • azegurb
                  New Member
                  • Sep 2009
                  • 32

                  #9
                  finally I did
                  Thank you very much
                  you helped me best
                  Let God loves you
                  Thanks Thanks Thanks

                  Comment

                  Working...