Jump to content

$_FILE['name']['type'] not working


Mutley

Recommended Posts

  $type = $_FILES['imagefile']['type'];
  if($type == "image/jpg" || $type == "image/jpeg" || $type == "image/pjpeg"){ 

 

Doesn't work and when I echo $type it displays nothing.

 

My Form field is simply:

<input type="file" name="imagefile" />

 

All the other $_POST statements work in the script but I can't get the $_FILES to work? Any idea?

 

Thanks,

Nick.

Link to comment
Share on other sites

Full script, it just echos out "ERROR: Wrong filetype (has to be a .jpg or .jpeg. Yours is "

 

if($_GET['inner']=='create'){
?>
<div class="adminBrief">Use the form below to create Pages.</div>
<form name="gallery" action="" method="post">
<table class="innerMain">
<tr>
	<td class="innerLeft">Image:</td>
	<td><input type="file" name="imagefile" /></td>
</tr><tr>
	<td class="innerLeft">Title:</td>
	<td><input type="text" name="title" value="" size="32" /></td>
</tr>
	<td>Category</td>
	<td>
		<select name="category">
		<?
			$sql="SELECT `id`, `name` FROM `categories` WHERE `for` = 'pages'";
			$res=mysql_query($sql);
			if(mysql_num_rows($res)!=0) {
			while(list($catID, $catName) = mysql_fetch_row($res)){
			?>
				<option value="<?=$catID?>"><?=$catName?></option>
			<?	
			}
			}
		?>
		</select>	
	</td>
</tr><tr>
	<td valign="top">Page Content:</td>
	<td valign="top">
		<script type="text/javascript" src="inc/WYSIWYG/fckeditor.js"></script>
		<script type="text/javascript">
			<!--
			var oFCKeditor = new FCKeditor( 'pagesContent' ) ;
			oFCKeditor.BasePath	= 'inc/WYSIWYG/' ;
			oFCKeditor.Height	= 300 ;
			oFCKeditor.Value	= '' ;
			oFCKeditor.Create() ;
			//-->
		</script>		
	</td>
</tr><tr>
	<td> </td>
	<td><input type="submit" name="SubmitImage" value="Submit"></td>
</tr>
</table>
</form>

 

if(isset($_POST['SubmitImage'])){
	$title = $_POST['title'];
	$category = $_POST['category'];
	$imagefile = $_POST['imagefile'];
	$content = $_POST['pagesContent'];
if($title == NULL || $content == NULL || $imagefile == NULL){
?>
	<script language="javascript">
	alert("Please fill in all the fields.");
	window.location = "admin.php?a=gallery&inner=create"
	</script>
<?
}else{

// Image Upload Form Below
$idir = "images/gallery/";								// Path To Images Directory 
$tdir = "images/gallery/thumbs/";						// Path To Thumbnails Directory 
$twidth = "100";										// Maximum Width For Thumbnail Images 
$theight = "100";										// Maximum Height For Thumbnail Images 

  $url = $_FILES['imagefile']['name'];   // Set $url To Equal The Filename For Later Use 
  $type = $_FILES['imagefile']['type'];
  if($type == "image/jpg" || $type == "image/jpeg" || $type == "image/pjpeg"){ 
    $file_ext = strrchr($_FILES['imagefile']['name'], '.');   // Get The File Extention In The Format Of , For Instance, .jpg, .gif or .php 
    $copy = copy($_FILES['imagefile']['tmp_name'], "$idir" . $_FILES['imagefile']['name']);   // Move Image From Temporary Location To Permanent Location 
    if ($copy){   // If The Script Was Able To Copy The Image To It's Permanent Location 
      //print 'Image uploaded successfully.<br />';		// Was Able To Successfully Upload Image 
      $simg = imagecreatefromjpeg("$idir" . $url);		// Make A New Temporary Image To Create The Thumbanil From 
      $currwidth = imagesx($simg);						// Current Image Width 
      $currheight = imagesy($simg);						// Current Image Height 
      if($currheight > $currwidth){						// If Height Is Greater Than Width 
         $zoom = $twidth / $currheight;					// Length Ratio For Width 
         $newheight = $theight;							// Height Is Equal To Max Height 
         $newwidth = $currwidth * $zoom;				// Creates The New Width 
      }else{											// Otherwise, Assume Width Is Greater Than Height (Will Produce Same Result If Width Is Equal To Height) 
        $zoom = $twidth / $currwidth;					// Length Ratio For Height 
        $newwidth = $twidth;							// Width Is Equal To Max Width 
        $newheight = $currheight * $zoom;				// Creates The New Height 
      } 
      $dimg = imagecreate($newwidth, $newheight);		// Make New Image For Thumbnail 
      imagetruecolortopalette($simg, false, 256);		// Create New Color Pallete 
      $palsize = ImageColorsTotal($simg); 
      for ($i = 0; $i < $palsize; $i++) {				// Counting Colors In The Image 
       $colors = ImageColorsForIndex($simg, $i);		// Number Of Colors Used 
       ImageColorAllocate($dimg, $colors['red'], $colors['green'], $colors['blue']);   // Tell The Server What Colors This Image Will Use 
      } 
      imagecopyresized($dimg, $simg, 0, 0, 0, 0, $newwidth, $newheight, $currwidth, $currheight);   // Copy Resized Image To The New Image (So We Can Save It) 
  $rand = rand(1,99999);
      imagejpeg($dimg, "$tdir" . $rand.'-'.$url);		// Saving The Image 

			$imgsql = $rand.'-'.$url;
			$sql="INSERT INTO `gallery` (`cat`, `url`, `title`, `content`) VALUES ('$category', '$imgsql', '$title', '$content')";
			mysql_query($sql);
					//$sql = "UPDATE gallery SET url = '".$rand.'-'.$url."' WHERE id = '".$id."' LIMIT 1";
					//mysql_query($sql);
					//$oldimg = "images/avatars/team/$url";
					//unlink($oldimg);
      imagedestroy($simg);								// Destroying The Temporary Image 
      imagedestroy($dimg);								// Destroying The Other Temporary Image 
      print 'Image uploaded successfully.';				// Resize successful 
  ?>
  	<script type="text/javascript">
		window.location = "admin.php?a=gallery&inner=create"
	</script>
  <?
    }else{ 
      print '<font color="#FF0000">ERROR: Unable to upload image.</font>';   // Error Message If Upload Failed 
    } 
  }else{ 
    print "<font color='#FF0000'>ERROR: Wrong filetype (has to be a .jpg or .jpeg. Yours is ";   // Error Message If Filetype Is Wrong 
    print $file_ext;   // Show The Invalid File's Extention 
    print '.</font>'; 
  }

	?>
		<script language="javascript">
		alert("Image Succesfully Uploaded.");
			window.location.href='';
		</script>
	<?
	}
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.