Need help adding 'get codes' on image upload script

Discussion in 'PHP' started by Huo_Yang, Jan 21, 2009.

  1. #1
    Hi.

    I am terrible at PHP and I want to modify an image-hosting script.

    When a user uploads an image they get those sharing codes after the upload is finished on the next page, which works perfectly. The code looks like this:

    <?php
    include_once("config.php");
    include_once("function-library.php");
    
    $ptitle   = "Free Image Hosting ".$site_name;
    $pkeys    = "image site, image hosting, free image host, image host, img host, upload picture, adult image hosting";
    $pdesc    = "Fast & Free Image Hosting for everyone. 3 step upload and simple instructions for uploading your images.";
    $selected = "HOME";
    
    if($default_upload_view == "MULTIPLE") {
    	$full_ref = $_SERVER["HTTP_REFERER"];
    	$root_ref = str_replace(array("http://www.", "https://www.", "http://", "www."), "", $full_ref);
    	
    	if(!strstr($root_ref, $site_url)) {
    		header("Location: index-multi-upload.html");
    		exit;
    		die();
    	}
    }
    
    include_once("header.php");
    ?>
    
    <SCRIPT LANGUAGE="JavaScript">
    function copyit(theField) {
    	var tempval=eval("document."+theField)
    	tempval.focus()
    	tempval.select()
    	therange=tempval.createTextRange()
    	therange.execCommand("Copy")
    }
    </script>
    
    <?php
    if($_POST['submitme']) {
    
      echo '<table width="100%" cellpadding="0" cellspacing="0"><form name="codeform"><tr><td width="70%" valign="top" style="padding:10px;">';
    
      // validation bits
      $filesize    = $_FILES['imgfile']['size'];
      $tmpfile     = $_FILES['imgfile']['tmp_name'];
      $filename    = $_FILES['imgfile']['name'];
      $contenttype = $_FILES['imgfile']['type'];
      $extension   = pathinfo($_FILES['imgfile']['name']);
      $extension   = strtolower($extension[extension]);
      
      if($filesize == 0) $error .= "Please specify an image file to upload.<br>";
      else {
    
    	  $valid_mime_types = array('image/jpeg', 'image/pjpeg', 'image/gif', 'image/png', 'image/x-png');
    	  if(!in_array($contenttype, $valid_mime_types)) {
    	    $error .= "File type does not appear to be a supported image (".$contenttype."). Please try another format.<br>";
    	  }
    	  if($filesize > $max_upload_size) $error .= "Uploaded file is greater than the maximum filesize. (".$max_upload_display_size.")<br>";
      
      }
      
      if(strlen($error) > 0) echo "<b>ERROR:</b><br><br>".$error."<br><br><a href='index.html'>back</a>";
      else {
    
        $uploaddir        = 'storage/originals/';
        $newfilename      = randomfilename() . "." . $extension;
    	
        $uploadfile = $uploaddir . $newfilename;
    	
        if (!move_uploaded_file($_FILES['imgfile']['tmp_name'], $uploadfile)) {
    	  $error .= "Could not upload file, please try again later.<br>";
    	}
    	else {
    	  // add to db
    	  $userip = $_SERVER['REMOTE_ADDR']; 
    	  list($originalwidth, $originalheight, $type, $attr) = getimagesize($uploadfile);
    	  
    	  // check for refreshes
    	  $check_image    = "SELECT id FROM images WHERE mimetype = '".preparedata($contenttype)."' AND originalfilename = '".preparedata($filename)."' AND filesize = '".preparedata($filesize)."' AND description='".preparedata($imgdesc)."' AND originalip='".preparedata($userip)."' AND originalwidth='".$originalwidth."' AND originalheight='".$originalheight."' LIMIT 1";
    	  $do_check_image = @mysql_query($check_image);
    	  if($do_check_image) {
    	    if(mysql_numrows($do_check_image) > 0) {
    		  $error .= "It looks like you've already added this image, please go back and upload a different one.<br>";
    		  unlink($uploadfile);
    		}
    	  }
    	  
    	  if(strlen($error) > 0) echo "<b>ERROR:</b><br><br>".$error."<br><br><a href='index.html'>back</a>";
    	  else {
    	  
    	  $tracker = randomfilename();
    	  
    	  $insert_image    = "INSERT INTO images (dateadded, mimetype, originalfilename, filename, filesize, description, originalip, originalwidth, originalheight, lastaccessed, tracker) VALUES (NOW(), '".preparedata($contenttype)."', '".preparedata($filename)."', '".preparedata($newfilename)."', '".preparedata($filesize)."', '".preparedata($imgdesc)."', '".preparedata($userip)."', '".$originalwidth."', '".$originalheight."', NOW(), '".preparedata($tracker)."')";
    	  $do_insert_image = @mysql_query($insert_image);
    	  $item_id = mysql_insert_id();
    	  
    	  if(!$do_insert_image) $error .= "Can not update the database, please try again later or contact support.";
    	  else {
    	  
    		$originalfilename = $filename;
    	  
    	    echo "<b>CONGRATULATIONS!</b><br><br>";
    		echo "Your image has now been uploaded to ".$site_name." and is ready for linking. Use any of the following code snippets to display the image within your preferred location:<br><br>";
    		
    		echo "<b>Forum code:</b> (<a href=\"javascript:copyit('codeform.code1');\">copy code</a>)<br>";
    		echo "<textarea readonly name='code1' style='width:550px;height:40px;font-size:11px;color:#999999;' onClick=\"javascript:copyit('codeform.code1');\">[URL=http://".$site_url."/showpic-".$item_id."/".createhtmlname($originalfilename)."][IMG]http://".$site_url."/thumb-".$item_id.".jpg[/IMG][/URL]</textarea><br><br>";
    		
    		echo "<b>Share this image with friends:</b> (<a href=\"javascript:copyit('codeform.code2');\">copy code</a>)<br>";
    		echo "<textarea readonly name='code2' style='width:550px;height:40px;font-size:11px;color:#999999;' onClick=\"javascript:copyit('codeform.code2');\">http://".$site_url."/showpic-".$item_id."/".createhtmlname($originalfilename)."</textarea><br><br>";
    		
    		echo "<b>HTML code for your websites WITH thumbnail:</b> (<a href=\"javascript:copyit('codeform.code3');\">copy code</a>)<br>";
    		echo "<textarea readonly name='code3' style='width:550px;height:40px;font-size:11px;color:#999999;' onClick=\"javascript:copyit('codeform.code3');\"><a href=\"http://".$site_url."/showpic-".$item_id."/".createhtmlname($originalfilename)."\"><img src=\"http://".$site_url."/thumb-".$item_id.".jpg\" border=\"0\" alt=\"".$site_url." - click to view full image\"></a></textarea><br><br>";
    		
    		echo "<b>HTML code for your websites WITHOUT thumbnail:</b> (<a href=\"javascript:copyit('codeform.code4');\">copy code</a>)<br>";
    		echo "<textarea readonly name='code4' style='width:550px;height:40px;font-size:11px;color:#999999;' onClick=\"javascript:copyit('codeform.code4');\"><a href=\"http://".$site_url."/showpic-".$item_id."/".createhtmlname($originalfilename)."\">view image</a></textarea><br><br>";
    		
    		if($show_delete_code) {
    		  echo "<b>Delete Image Link:</b><br>Use this link to remove your image at any time.<br>";
    		  echo " - <a href='http://".$site_url."/delete.html?d=".$tracker."'>http://".$site_url."/delete.html?d=".$tracker."</a><br><br>";
    		}
    		
    		if($show_pass_option) {
    		  echo "<iframe src='set-pass.php?im=".$item_id."' frameborder='no' scrolling='no' width='400'></iframe>";
    		}
    		
    		echo "</td><td valign='top' align='center'><br><br><br>Thumbnail:<br><br>";
    		
    		echo "<a href='showpic-".$item_id."/".createhtmlname($originalfilename)."'><img src='view-image.html?img=".$item_id."&w=160&h=160' border='0' width='160' alt='".$site_url." - click here to view this image' style='border:1px solid #cccccc;'></a>";
    		
    		echo "<br><br>";
    		
    		// recent images
    		$get_recent = @mysql_query("SELECT id, dateadded, originalfilename, filesize FROM images WHERE originalip = '".$userip."' ORDER BY id DESC LIMIT 8");
    		if($get_recent) {
    		  if(mysql_numrows($get_recent) > 0) {
    		    echo "Your Recent Uploads:<br><br>";
    		    while($row = mysql_fetch_array($get_recent)) {
    			  $filesize = number_format($row[filesize]/1024, 1);
    			  if($filesize > 1000) $filesize = number_format($filesize/1024, 1)."mb";
    			  else $filesize = $filesize."kb";
    			  if(strlen($row[originalfilename]) > 18) $originalfile = substr($row[originalfilename], 0, 18)."...";
    			  else $originalfile = $row[originalfilename];
    			  echo "<a href='showpic-".$row[id]."/".createhtmlname($row[originalfilename])."'>".$row[originalfilename]." (".$filesize.")</a><br>";
    			}
    		  }
    		  echo "<br>";
    		}
    	  
    	  }
    	 }
    	}
    
      }
      
      echo '</td></tr></form></table>';
      
    }
    else {
    ?>
    
    
    <table width="100%" cellpadding="0" cellspacing="0">
    <tr>
    <td width="70%" valign="top" style="padding:10px;">
    
    Use the form below to upload your image to <?php echo $site_url; ?>. <a href="index-multi-upload.html">Multiple file uploader tool here</a>. Please note the following before uploading:<br><br>
    <ul>
    <li><b>File Types:</b> jpg, gif, png.
    <li><b>Max File Size:</b> <?php echo $max_upload_display_size; ?>.
    <li><b>No</b> copyrighted images/artwork.
    <li>By uploading you are agreeing to the Terms of Service.
    </ul><br style="line-height: 6px;">
    <table width="100%" cellpadding="10" cellspacing="0" style="border:1px solid #CCCCCC;">
    <form method="POST" action="index.html" enctype='multipart/form-data'>
    <tr>
    <td width="100%" valign="top" bgcolor="#EAEAEA">
    <b>1. Select your image:</b>&nbsp;(max <?php echo $max_upload_display_size; ?>. jpg, gif, png)<br><br style="line-height: 4px;">
    <input type="file" name="imgfile" style="width:435px;"><br><br style="line-height: 6px;">
    <b>2. Enter a description:</b>&nbsp;(optional)<br><br style="line-height: 4px;">
    <textarea name="imgdesc" style="width:240px; height:41px;"></textarea><br><br style="line-height: 6px;">
    <b>3. Upload now:</b><br><br style="line-height: 4px;">
    <input name="submitme" type="hidden" value="1">
    <input name="submit" type="submit" value="upload image"> or use the <a href="index-multi-upload.html">Multiple Image Uploader Tool</a>.
    </td>
    </tr>
    </form>
    </table>
    
    </td>
    </tr>
    </table>
    
    <?php
    }
    ?>
    
    <?php
    include("footer.php");
    ?>
    PHP:
    But now I want to add those sharing codes on the page, where the users are actually viewing the picture, but of course I am to much of a dump guy to get it to work.

    <?php
    include_once("config.php");
    include_once("function-library.php");
    include_once("gallery_img_handler.php");
    
    if(!$img) {
    	$ptitle   = "Free Image Hosting";
    	$pkeys    = "image hosting, free image host, image host, img host, upload picture";
    	$pdesc    = "Fast & Free Image Hosting";
    	$selected = "VIEWPIC";
    	include_once("header.php");
    	  echo "<b>ERROR: IMAGE NOT FOUND!</b>";
    	include("footer.php");
    }
    else {
    
      // load image data
      if(!$w) $w = 720;
      if($w > 2000) $w = 2000;
      if($w) $image = previewImage($img, $w);
      else $image = previewImage($img);
      $done = file_get_contents($image['targetImagePath']);
    
      if(strlen($image['password']) > 0) {
    	  if($_POST['p'] != $image['password']) {
    	    // redirect to adult confirmation page
    		header("Location: http://".$site_url."/enter-password.html?img=".$img);
    		die();
    		exit;
    	  }
      }
      elseif($image['status'] == "adult") {
    	  if(!$_POST['adult']) {
    	    // redirect to adult confirmation page
    		header("Location: http://".$site_url."/adult-warning.html?img=".$img);
    		die();
    		exit;
    	  }
      }
    
      $ptitle   = "Free Image Hosting";
      $pkeys    = "image hosting, free image host, image host, img host, upload picture";
      $pdesc    = "Fast & Free Image Hosting";
      $selected = "VIEWPIC";
      
      include_once("header.php");
      
      echo '<td valign="top" align="center"><table width="750" cellpadding="0" cellspacing="0"><tr><td valign="top" style="padding:10px;" align="center">';
      
     ?>
     <!-- Begin: AdBrite, Generated: 2008-12-27 17:00:13  -->
    <script type="text/javascript">
    var AdBrite_Title_Color = '0000FF';
    var AdBrite_Text_Color = '000000';
    var AdBrite_Background_Color = 'FFFFFF';
    var AdBrite_Border_Color = 'FFFFFF';
    var AdBrite_URL_Color = '008000';
    try{var AdBrite_Iframe=window.top!=window.self?2:1;var AdBrite_Referrer=document.referrer==''?document.location:document.referrer;AdBrite_Referrer=encodeURIComponent(AdBrite_Referrer);}catch(e){var AdBrite_Iframe='';var AdBrite_Referrer='';}
    </script>
    <span style="white-space:nowrap;"><script type="text/javascript">document.write(String.fromCharCode(60,83,67,82,73,80,84));document.write(' src="http://ads.adbrite.com/mb/text_group.php?sid=947462&zs=3732385f3930&ifr='+AdBrite_Iframe+'&ref='+AdBrite_Referrer+'" type="text/javascript">');document.write(String.fromCharCode(60,47,83,67,82,73,80,84,62));</script>
    <a target="_top" href="http://www.adbrite.com/mb/commerce/purchase_form.php?opid=947462&afsid=1"><img src="http://files.adbrite.com/mb/images/adbrite-your-ad-here-leaderboard.gif" style="background-color:#FFFFFF;border:none;padding:0;margin:0;" alt="Your Ad Here" width="14" height="90" border="0" /></a></span>
    <!-- End: AdBrite -->
      <?php 
      echo '<a href="http://'.$site_url.'/showoriginal-'.$img.'/'.createhtmlname($image[originalfilename]).'"><img src="http://'.$site_url.'/view-pic-img.html?img='.$img.'&w='.$w.'" width="'.$image[width].'" height="'.$image[height].'" alt="'.$image[description].'" border="0"></a>';
      echo "<br><br>";
      
      $newviews = $image[totalviews]+1;
      echo "File Name: ".$image[originalfilename]."<br><br>";
      echo "Date Added:&nbsp;".$image[dateadded]." | Original Filesize:&nbsp;".$image[filesize]." | Views:&nbsp;".$newviews;
      if(strlen($image[description]) > 0) echo "<br><br>Comments:&nbsp;".$image[description];
      echo "<br><br><a href='http://".$site_url."/showoriginal-".$img."/".createhtmlname($image[originalfilename])."'>view original</a> | <a href='http://".$site_url."/download-".$img."/".createhtmlname($image[originalfilename])."'>download</a>";
      
      // ads
      if($image['status'] == "adult") {
        // display adult advertising here
      }
      else {
        // display non-adult advertising here
      }
      
      echo '</td></tr></table></td>';
      
      // update views
      $update_views = mysql_query("UPDATE images SET totalviews = ".$newviews.", lastaccessed = NOW() WHERE id = ".$img." LIMIT 1");
      
      include("footer.php");
      
    }
    ?>
    PHP:
    I tried to just add the following, but i guess there is something missing to grabb the correct IDs/paths from the database.

    echo "<b>Forum code:</b> (<a href=\"javascript:copyit('codeform.code1');\">copy code</a>)<br>";
    		echo "<textarea readonly name='code1' style='width:550px;height:40px;font-size:11px;color:#999999;' onClick=\"javascript:copyit('codeform.code1');\">[URL=http://".$site_url."/showpic-".$item_id."/".createhtmlname($originalfilename)."][IMG]http://".$site_url."/thumb-".$item_id.".jpg[/IMG][/URL]</textarea><br><br>";
    		
    		echo "<b>Share this image with friends:</b> (<a href=\"javascript:copyit('codeform.code2');\">copy code</a>)<br>";
    		echo "<textarea readonly name='code2' style='width:550px;height:40px;font-size:11px;color:#999999;' onClick=\"javascript:copyit('codeform.code2');\">http://".$site_url."/showpic-".$item_id."/".createhtmlname($originalfilename)."</textarea><br><br>";
    		
    		echo "<b>HTML code for your websites WITH thumbnail:</b> (<a href=\"javascript:copyit('codeform.code3');\">copy code</a>)<br>";
    		echo "<textarea readonly name='code3' style='width:550px;height:40px;font-size:11px;color:#999999;' onClick=\"javascript:copyit('codeform.code3');\"><a href=\"http://".$site_url."/showpic-".$item_id."/".createhtmlname($originalfilename)."\"><img src=\"http://".$site_url."/thumb-".$item_id.".jpg\" border=\"0\" alt=\"".$site_url." - click to view full image\"></a></textarea><br><br>";
    		
    		echo "<b>HTML code for your websites WITHOUT thumbnail:</b> (<a href=\"javascript:copyit('codeform.code4');\">copy code</a>)<br>";
    		echo "<textarea readonly name='code4' style='width:550px;height:40px;font-size:11px;color:#999999;' onClick=\"javascript:copyit('codeform.code4');\"><a href=\"http://".$site_url."/showpic-".$item_id."/".createhtmlname($originalfilename)."\">view image</a></textarea><br><br>";
    PHP:
    Hopefully someone can take a quick look and help me with this!?
    thanks so much in advance!
     
    Huo_Yang, Jan 21, 2009 IP
  2. ads2help

    ads2help Peon

    Messages:
    2,142
    Likes Received:
    67
    Best Answers:
    1
    Trophy Points:
    0
    #2
    What I noticed is

    createhtmlname($image[originalfilename])
    is used in the scripts

    but yours is
    createhtmlname($originalfilename)

    I don't see where $originalfilename get its value.

    so, try replacing createhtmlname($originalfilename) with createhtmlname($image[originalfilename])

    see if it helps.
     
    ads2help, Jan 21, 2009 IP
  3. Huo_Yang

    Huo_Yang Peon

    Messages:
    250
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks ads2help!
    It helped with the file-name issue.
    Any Idea how I can get the .$item_id from the databse?

    EDIT: (problem resolved) I just changed .$item_id to .$img., gives me the result I wanted!
    Thanks for your quick help
     
    Huo_Yang, Jan 21, 2009 IP