Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Nov 2011
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Add photo to newssystem

    Hi,

    I'm working on a newssystem for a website that I'm making. I just started with php so please be patient! :-) I already can add, update and delete news, but the part to add photo's to a newsmessage still don't work.

    Already thanks!

    Here is the code:

    Code:
    <?php
    
    ini_set('display_errors', 1); // 0 = uit, 1 = aan
    error_reporting(E_ALL);
    
    if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    	ini_set('display_errors', 1); // 0 = uit, 1 = aan
    	error_reporting(E_ALL);
    	
    	if ($_POST['voegtoe'])
    	ini_set('display_errors', 1); // 0 = uit, 1 = aan
    	error_reporting(E_ALL);
    	{	
    		$photoFileName = $_FILES['photo']['name']; // get client side file name
    		if ($photoFileName) // file uploaded
    		{
    			$fileNameParts = explode(".", $photoFileName);
    			$fileExtension = end($fileNameParts); // part behind last dot
    	
    			if ($fileExtension != "jpg" && $fileExtension != "JPEG"	&& $fileExtension != "JPG")
    			{
    				die ("Kies aub een <b>jpg</b> foto, ga terug en probeer het opnieuw.");
    			}
    			
    			$photoSize = $_FILES['photo']['size']; // size of uploaded file
    			if ($photoSize == 0)
    			{
    				die ("Kies eerst een foto en klik daarna pas op \"voegtoe\", ga terug en probeer het opnieuw.");
    			}
    		
    			// read photo
    			$tempFileName = $_FILES['photo']['tmp_name']; // temporary file at server side
    			$src_img = imagecreatefromjpeg ($tempFileName);
    	
    			$width = imagesx($src_img); // get original source image width
    			$height = imagesy($src_img); // and height
    			
    			// create small thumbnail
    			if (($height * 3) / 4 >  $width)
    			{
    				$dest_height = 260;
    				$dest_width = (260 * $width) / $height;
    			}
    			else
    			{
    				$dest_width = 200;
    				$dest_height = (200 * $height) / $width;
    			}
    			
    			$dest_img = imagecreatetruecolor($dest_width, $dest_height);
    	
    			$result = imagecopyresampled($dest_img, $src_img,0, 0, 0, 0,	$dest_width, $dest_height,$width, $height); // resize the image
    	
    			$indexke = strrpos($photoFileName, '.');
    			$photoFileName = substr ($photoFileName, 0, $indexke) . ".png";
    			
    			imagepng($dest_img, "fotos_groot/thumbs/" . $photoFileName); // save image
    			imagepng($src_img, "fotos_groot/fotos/" . $photoFileName);
    			
    			imagedestroy($src_img);
    			imagedestroy($dest_img);
    	
    			mysql_query("INSERT INTO nieuws_fotos_groot (naam, height, width, height_groot, width_groot) 
    							values ('$photoFileName', '$dest_height', '$dest_width', '$height', '$width')") or die ("fout bij het vastleggen van de foto in de databank");
    		}
    	}
    
    }
    
    if (isset($id)) {
    	$result = mysql_query("SELECT * 
    						  FROM nieuws_fotos_groot 
    						  WHERE id = '$id'");
    	$show_msg = mysql_fetch_array($result);
    	
    	echo (	"<script language=\"javascript\" type=\"text/javascript\">\n<!--\n" .
    		"opener.document.submitform.boodschap.value += \" [foto $show_msg[naam] width=$show_msg[width] height=$show_msg[height]]" . $id . "[/foto]\";\n" .
    		"window.close();\n" .
    		"// -->\n</script>\n");   
    
    // else show the form to submit new data:
    }
    else
    {
    ?>
    
    <script language="javascript" type="text/javascript">
    <!--
    function hidebuttons(theform) {
    	document.body.style.cursor = "wait";
    	//if IE 4+ or NS 6+
    	if (document.all||document.getElementById) {
    		//screen thru every element in the form, and hunt down "submit" and "reset"
    		for (i=0;i<theform.length;i++) {
    			var tempobj=theform.elements[i]
    			try {
    				if(tempobj.type.toLowerCase()=="submit"||tempobj.type.toLowerCase()=="reset") {
    					tempobj.style.display='none';
    					theform.elements[tempobj.name + "_hidden"].style.display="inline";
    				}
    			}
    			catch(errorObject) {}	
    		}
    	}
    }
    // --->
    </script>
    <div align="center">
    	<table border="0" width="95%" cellspacing="0" cellpadding="0">
    		<tr>
    			<td class="tekst">
    				<br>
    				<div class="titel">Selecteer een foto</div>
    			</td>
    		</tr>
    		<tr>
    			<td bgcolor="#c0c0c0">
    				<img src="images/niks.gif" height="1" width="1" alt="">
    			</td>
    		</tr>
    		<tr>
    			<td class="tekst">
    				<div align="center">
    				<table border="0" cellspacing="15">
    					<tr>
    
    <?php
    $teller = 1;
    
    $results = mysql_query("SELECT * 
    					   FROM nieuws_fotos_groot 
    					   WHERE id != '0'");
    while($show_msg = mysql_fetch_array($results))
    {
    	echo("<td align=\"center\"><table border=\"0\" cellspacing=\"2\" cellpadding=\"0\" bgcolor=\"#c0c0c0\"><tr><td><a href=\"foto_groot.php?id=$show_msg[id]&naam=$show_msg[naam]\"><img src=\"fotos_groot/thumbs/$show_msg[naam]\" width=\"$show_msg[width]\" height=\"$show_msg[height]\" border=\"0\" alt=\"klik om te selecteren\"></a></td></tr></table></td>\n");
    	if ($teller++ % 2 == 0)
    		echo ("</tr><tr>\n");
    }
    ?>
    			</table>
    			<br>	
    
    			<form onsubmit="hidebuttons(this)" method="post" action="<?php $_SERVER['PHP_SELF']; ?>" enctype="multipart/form-data">
    			<table border="0" bgcolor="#cccccc" cellspacing="1" cellpadding="0" width="95%">
    				<tr>
    					<td>
    						<table border="0" cellspacing="5" width="100%" bgcolor="#f6f6f6">
    							<tr>
    								<td>
    									<table border="0" cellspacing="5" width="100%">
    										<tr>
    											<td class="tekst" valign="top" colspan="3"><font size="-2">foto toevoegen<br><br></font>
    											</td>
    										</tr>
    										<tr>
    											<td class="tekst" valign="top"><b>Selecteer foto</b>
    											</td>
    											<td><input type="file" name="photo" class="formke" size="25" accept="image/jpeg"><br><br>
    											</td>
    											<td class="tekst" valign="top">
    												<input type="submit" value=" Voeg toe " class="form" name="voegtoe"><input type="button" value=" wacht even " class="form" name="voegtoe_hidden" disabled style="display:none">
    											</td>
    										</tr>
    									</table>
    								</td>
    							</tr>
    						</table>
    					</td>
    				</tr>
    			</table>
    		</div>
    		</form>
    		<input type="Button" value=" Cancel " class="form" OnClick="self.close();">
    		</td>
    	</tr>
    </table>
    </div>
    <?php
    }
    mysql_close($connection);
    ?>

  • #2
    Senior Coder
    Join Date
    Sep 2010
    Posts
    1,987
    Thanks
    15
    Thanked 231 Times in 231 Posts
    PhP builds an array '$_FILES' for uploads, if you print_r($_FILES); you can see the array and what is happening. You will see in it $_FILES['photo']['type']. and it will be image/jpeg if it is a jpeg image, instead of checking against the extension use that. I'm quite sure that GD must have the ability to read the file header, use that too, to check for valid uploads. I use Imagemagick so I can't tell you about GD.


  •  

    Tags for this Thread

    Posting Permissions

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