Jump to content

[SOLVED] FILE UPLOAD not UPLOADING


jiggens

Recommended Posts

I am trying to upload an image using a html form and PHP and it seems to be failing yet i am getting a 0 for array error code when i upload.  The code is as followed. 

 

<?php
#Start the PHP session
session_start();

#Variables set to log into SQL database
$SQLusername = 'xxxxx';
$SQLpassword = 'xxxxx';
$SQLdbname = 'xxxxx';
$db = mysql_connect("localhost", $SQLusername, $SQLpassword) or print mysql_error ();
mysql_select_db($SQLdbname) or print mysql_error ();

#Checks to see if user is logged in
if($_SESSION['sesLogIn'] != 1) {
	header("Location: http://www.domainscene.com/admin/");
	exit();
}

$ID = $_GET['ID'];
$siteID = $_GET['siteID'];

$uploaddir = '/homes/html/images/Temp/';
$uploadfile = $uploaddir . basename($_FILES['newMap']['name']);
$uploadfile2 = $uploaddir . basename($_FILES['newLogo']['name']);

#upload image function	
	function createImage($imgname,$maxh,$maxw,$newname){
	  list($width, $height) = getimagesize($imgname);
	  if ($width < $maxw && $height < $maxh) {
	  	$new_height = $height;
		$new_width = $width;
	  } else {
		$ratio = ($width/$height);
		$new_height = sqrt(10000/$ratio);
		$new_width = $width * ($new_height / $height);
	  } 
	  $image_p = imagecreatetruecolor($new_width, $new_height);
	  $image = imagecreatefromjpeg($imgname);
	  imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);  
		//Grab new image
		ob_start();
		ImageJPEG($image_p);
		$image_buffer = ob_get_contents();
		ob_end_clean();
		ImageDestroy($image_p);
		//Create temporary file and write to it
		$fp = tmpfile();
		fwrite($fp, $image_buffer);
		rewind($fp);
	}



//#Resize Image based on maximum width and maximum height
function resizeBoth($imgname,$maxh,$maxw,$newname) {
	  list($width, $height) = getimagesize($imgname);
	  if ($width < $maxw && $height < $maxh) {
	  	$new_height = $height;
		$new_width = $width;
	  } if ($width > $height) {
		$new_height = $height * ($maxw / $width);
		$new_width = $maxw;
	  } else {
		$new_height = $maxh;
		$new_width = $width * ($maxh / $height);
	  }
	  $image_p = imagecreatetruecolor($new_width, $new_height);
	  $image = imagecreatefromjpeg($imgname);
	  imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);  
		//Grab new image
		ob_start();
		ImageJPEG($image_p);
		$image_buffer = ob_get_contents();
		ob_end_clean();
		ImageDestroy($image_p);
		//Create temporary file and write to it
		$fp = tmpfile();
		fwrite($fp, $image_buffer);
		rewind($fp);
}

#Checks to see if add/edit project has been submitted
if(array_key_exists('submit', $_POST)) {
	if ($ID == "" || $ID == "new") {
		$query = "INSERT INTO psh_communities ";
		$query .= "(name,area,status,upcoming_description,current_description,current_details,completed_description)";
		$query .= " VALUES ('" . $_POST['communityName'] . "','" . $_POST['communityArea'] . "','" . $_POST['communityStatus'] . "','" . addslashes($_POST['communityUpDesc']) . "','" . addslashes($_POST['communityCurrDesc']) . "','" . addslashes($_POST['communityCurrDet']) . "','" . addslashes($_POST['communityCompDesc']) . "')";
		$result = mysql_query($query);
		$result = mysql_query("SELECT MAX(ID) from psh_communities");
		$row = mysql_fetch_row($result);
		$newProjectID = $row[0];
		$result = mysql_query("SELECT MAX(orderID) from psh_communities");
		$row = mysql_fetch_row($result);
		$newOrderID = $row[0] + 1;
		$query = "UPDATE psh_communities SET orderID = '" . $newOrderID . "' WHERE ID = '" . $newProjectID . "'";
		$result = mysql_query ($query);
		header("Location: " . $_SERVER['PHP_SELF'] . "?ID=" . $newProjectID);
		exit();
	} else {
		$query = "UPDATE psh_communities SET name = '" . $_POST['communityName'] . "', area = '" . $_POST['communityArea'] . "', status = '" . $_POST['communityStatus'] . "', upcoming_description = '" . addslashes($_POST['communityUpDesc']) . "', current_description = '" . addslashes($_POST['communityCurrDesc']) . "', current_details = '" . addslashes($_POST['communityCurrDet']) . "', completed_description = '" . addslashes($_POST['communityCompDesc']) . "' WHERE ID = $ID";
		$result = mysql_query($query);
	}
}

if(array_key_exists('deleteImage', $_POST)) {
	if ($_POST['deleteImage'] == "Delete Map") {
			unlink('/homes/html/images/browse/maps/'. $ID . '.jpg');
			unlink('/homes/html/images/browse/maps/'. $ID . '-TH.jpg');
		} elseif ($_POST['deleteImage'] == "Delete Logo") {
			unlink('/homes/html/images/browse/logos/'. $ID . '.jpg');
		} elseif ($_POST['deleteImage'] == "Delete Features List") {
			$query = "SELECT * from psh_communities WHERE ID = $ID";
			$result = mysql_query($query);
			$row = mysql_fetch_row ($result);
			$query = "UPDATE psh_communities SET features = 'NULL' WHERE ID = $ID";
			$result = mysql_query($query);
			unlink('/homes/html/browse/features/'. $row[10]);
		} elseif ($_POST['deleteImage'] == "Delete Brochure") {
			$query = "SELECT * from psh_communities WHERE ID = $ID";
			$result = mysql_query($query);
			$row = mysql_fetch_row ($result);
			$query = "UPDATE psh_communities SET brochure = 'NULL' WHERE ID = $ID";
			$result = mysql_query($query);
			unlink('/homes/html/browse/brochures/'. $row[8]);
		}  elseif ($_POST['deleteImage'] == "Delete Banner") {
			unlink('/homes/html/images/browse/banners/'. $ID . '.jpg');
		}

}


  if(array_key_exists('uploadNewLogo', $_POST)) {
	if ($ID != "" && $ID != "new") {
			move_uploaded_file($_FILES['newLogo']['tmp_name'], $uploadfile2);
			createImage($uploadfile2,100,400,'homes/html/images/browse/logos/' . $ID . '.jpg');
			createImage($uploadfile2,100,160,'homes/html/images/browse/logos/' . $ID . '-TH.jpg');
			unlink($uploadfile2);
	}
}


?>
<!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-1" />
<title>domain Scene Family of Companies</title>
<link href="../../ps_main.css" rel="stylesheet" type="text/css" />
<script language="JavaScript" type="text/javascript">
<!--

//-->
</script>
<script language="JavaScript" src="../../flash_detect.js" type="text/javascript"></script>

</head>

<body 		
    			          
		  <form enctype="multipart/form-data" action="<? print $_SERVER['PHP_SELF'] . "?ID=" . $ID ?>" method="POST">

                <table border="0" cellspacing="0" cellpadding="4">
                  <tr>
                    <td align="right" valign="top"><div align="right">Community Name: </div></td>
                    <td align="left" valign="top"><input name="communityName" type="text" size="30" value="<? print $row[1]; ?>" /></td>
                  </tr>
                  <tr>
                    <td align="right" valign="top"><div align="right">Development Area: </div></td>
                    <td align="left" valign="top"><select name="communityArea">
                        <option value="Inland Empire" <? if ($row[2] == "Inland Empire") { print "selected"; } ?>>Inland Empire</option>
					<option value="San Diego" <? if ($row[2] == "San Diego") { print "selected"; } ?>>San Diego</option>
                    </select></td>
                  </tr>
			  <tr>
                    <td align="right" valign="top"><div align="right">Project Status: </div></td>
                    <td align="left" valign="top"><select name="communityStatus">
                      <option value="Inactive" <? if ($row[3] == "Inactive") { print "selected"; } ?>>Inactive</option>
                        <option value="Upcoming" <? if ($row[3] == "Upcoming") { print "selected"; } ?>>Upcoming</option>
					<option value="Current" <? if ($row[3] == "Current") { print "selected"; } ?>>Current</option>
                        <option value="Completed" <? if ($row[3] == "Completed") { print "selected"; } ?>>Completed</option>
                    </select></td>
                  </tr>
			  
                  <tr>
                    <td align="right" valign="top"><div align="right"></div></td>
                    <td align="left" valign="top"><strong><br />
                    Upcoming</strong></td>
                  </tr>
                  <tr>
                    <td align="right" valign="top"><div align="right">Description:</div></td>
                    <td align="left" valign="top"><textarea name="communityUpDesc" cols="40"><? print stripslashes($row[4]); ?></textarea></td>
                  </tr>
                  <tr>
                    <td align="right" valign="top"><div align="right"></div></td>
                    <td align="left" valign="top"><strong><br />
                    Current</strong></td>
                  </tr>
                  <tr>
                    <td align="right" valign="top"><div align="right">Logo:</div></td>
                    <td align="left" valign="top"><?
					  print_r($_FILES);
					if ($ID != "" && $ID != "new") { 
					if (file_exists("/homes/html/images/browse/logos/" . $ID . ".jpg")) {

						print "<img src='http://homes.domainscene.com/images/browse/logos/" . $ID . ".jpg' />"; 
						print "<input type='submit' name='deleteImage' value='Delete Logo' />";
					  print "<input type=\"hidden\" name=\"MAX_FILE_SIZE\" value=\"30000\" />";
						print "<br />";
					} 
					else {				
					  print "<input name='newLogo' type='file' size='40' /><input type='submit' name='uploadNewLogo' value='Upload' />";
					}
				}

				?>

 

 

Any help will be appreciated.

Link to comment
Share on other sites

well this is just more common, not really a problem.

Change

<?php $db = mysql_connect("localhost", $SQLusername, $SQLpassword) or print mysql_error (); ?>

to

<?php $db = mysql_connect("localhost", $SQLusername, $SQLpassword) or die mysql_error (); ?>

Link to comment
Share on other sites

To simplify i am having a problem with this portion I checked this portion of my code and its not working, i get the message There was an error uploading the file, please try again!

 


<?php

if(array_key_exists('uploadNewLogo', $_POST)) {
	if ($ID != "" && $ID != "new") {
		if(move_uploaded_file($_FILES['newLogo']['tmp_name'], $uploadfile2)) {
			echo "The file ". basename($_FILES['newLogo']['tmp_name'][name]). "has been  
                                uploaded";
		}else{
			echo "There was an error uploading the file, please try again!";
     }		
		createImage($uploadfile2,100,400,'homes/html/images/browse/logos/' . $ID . '.jpg');
		createImage($uploadfile2,100,160,'homes/html/images/browse/logos/' . $ID . '-TH.jpg');
			unlink($uploadfile2);
	}
}
?>

Link to comment
Share on other sites

$ID = $_GET['ID'];

$siteID = $_GET['siteID'];

 

$uploaddir = '/homes/html/images/Temp/';

$uploadfile = $uploaddir . basename($_FILES['newMap']['name']);

$uploadfile2 = $uploaddir . basename($_FILES['newLogo']['name']);

 

 

 

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.