Image Upload Problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
crazitrain02
Forum Newbie
Posts: 22
Joined: Wed Dec 08, 2010 5:20 pm

Image Upload Problem

Post by crazitrain02 »

I'm working on getting a form to upload an image to the server and place it in a folder, and insert data into a MySQL database at the same time.
Everything works great on a test page that I have configured, but not with the full form page. Can someone please tell me what I'm missing with my copy/paste here?
My test pages are form.php and image.php and my production pages are add_inventory.php and add.php.

form.php

Code: Select all

 <html>
 <body>
 <form name="newad" method="post" enctype="multipart/form-data"  action="Forms\image.php">
 <table>
 	<tr><td><input type="file" name="image" accept="image/jpg"></td></tr>
 	<tr><td><input name="Submit" type="submit" value="Upload image"></td></tr>
 </table>	
 </form>
 </body>
 </html>
image.php

Code: Select all

<?php

define ("MAX_SIZE","10000"); 

function getExtension($str) { 
$i = strrpos($str,"."); 
if (!$i) { return ""; } 
$l = strlen($str) - $i; 
$ext = substr($str,$i+1,$l); 
return $ext; 
} 

$errors=0; 
if(isset($_POST['Submit'])) 
{ 
$image=$_FILES['image']['name']; 
if ($image) 
{ 
$filename = stripslashes($_FILES['image']['name']);
$extension = getExtension($filename); 
$extension = strtolower($extension); 
if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif"))     
{ 
echo '<h1>Invalid image type.</h1>'; 
$errors=1; 
} 
else 
{ 
$size=filesize($_FILES['image']['tmp_name']); 

if ($size > MAX_SIZE*1024) 
{ 
echo '<h1>The image is too big!</h1>'; 
$errors=1; 
} 
$image_name=time().'.'.$extension; 
$newname="../images/parts/".$image_name; 
$copied = copy($_FILES['image']['tmp_name'], $newname); 
if (!$copied) 
{ 
echo '<h1>Could not upload!</h1>'; 
$errors=1; 
}}}} 

include '..\admin\db_inventory.php';
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die('Error connecting to SQL Database');
mysql_select_db($dbname);

$insert="INSERT INTO parts (partnum, quantity, description, manufacturer, model, statusid, image)
VALUES ('222666','1','Computer','HAL','9000','1','$image_name')";
$error_button = "<FORM><INPUT TYPE='button' value='Go Back' onclick='history.go(-1)' /></FORM>";
if (!mysql_query($insert,$conn))
	{
	die("Error: ".mysql_error().$error_button);
	}
echo "Record added";
mysql_close($conn);

 ?>
add_inventory.php

Code: Select all

<html>
<head>
<title>Add New Inventory</title>
<link rel="stylesheet" href="CSS\standard.css">
</head>
<body align="center">
<h2>Add New Inventory</h2>
<form action="Forms\add.php" method="post" enctype="multipart/form-data">
<div align='center' />
<table class="add" width="30%">
<tr>
	<td>Part Number:</td>
	<td><input type="text" name="partnum" /></td>
</tr>
<tr>
	<td>Quantity:</td>
	<td><input type="text" name="quan" /></td>
</tr>
<tr>
	<td>Description:</td>
	<td><input type="text" name="desc" /></td>
</tr>
<tr>
	<td>Manufacturer:</td>
	<td><input type="text" name="man" /></td>
</tr>
<tr>
	<td>Model:</td>
	<td><input type="text" name="mod" /></td>
</tr>
<tr>
<td>Status:</td>
<td>
	<?php
		include 'admin\db_inventory.php';
		$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die('Error connecting to database');
		mysql_select_db($dbname);

		$query = "SELECT DISTINCT status FROM status ORDER BY statusid;";

		if($result = mysql_query($query))
		{
		if($success = mysql_num_rows($result) > 0)
			echo "<select name='status';'>\n";
		while ($row = mysql_fetch_array($result))
			echo "<option value='$row[status]'>$row[status]</option>\n";
			echo "</select>\n";
		}
		mysql_close();
	?>
</td>
</tr>
<tr>
	<td>Image:</td>
	<td><input type="file" name="image" accept="image/jpeg"/></td>
</tr>
</table>
<input type="submit" name="Add Part" Value="Add To Inventory" />
<input type="button" Value="Cancel" onclick="history.go(-1)" />
</form>
</center>
</body>
</html>
add.php

Code: Select all

<html>
<head>
<title>Add Inventory</title>
<link rel="stylesheet" href="..\CSS\standard.css">
</head>
<body>
<h2>Add Inventory</h2>
<?php
// Set Variables from POST
$partnum = "$_POST[partnum]";
$quan = "$_POST[quan]";
$desc = "$_POST[desc]";
$man = "$_POST[man]";
$mod = "$_POST[mod]";
$stat = "$_POST[status]";

define ("MAX_SIZE","10000"); 

function getExtension($str) { 
$i = strrpos($str,"."); 
if (!$i) { return ""; } 
$l = strlen($str) - $i; 
$ext = substr($str,$i+1,$l); 
return $ext; 
} 

$errors=0; 
if(isset($_POST['Submit'])) 
{ 
$image=$_FILES['image']['name']; 
if ($image) 
{ 
$filename = stripslashes($_FILES['image']['name']);
$extension = getExtension($filename); 
$extension = strtolower($extension); 
if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif"))     
{ 
echo '<h1>Invalid image type.</h1>'; 
$errors=1; 
} 
else 
{ 
$size=filesize($_FILES['image']['tmp_name']); 

if ($size > MAX_SIZE*1024) 
{ 
echo '<h1>The image is too big!</h1>'; 
$errors=1; 
} 
$image_name=time().'.'.$extension; 
$newname="../images/parts/".$image_name; 
$copied = copy($_FILES['image']['tmp_name'], $newname); 
if (!$copied) 
{ 
echo '<h1>Could not upload!</h1>'; 
$errors=1; 
}}}} 

include '..\admin\db_inventory.php';
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die('Error connecting to SQL Database');
mysql_select_db($dbname);

$insert="INSERT INTO parts (partnum, quantity, description, manufacturer, model, statusid, image)
VALUES ('222666','1','Computer','HAL','9000','1','$image_name')";
$error_button = "<FORM><INPUT TYPE='button' value='Go Back' onclick='history.go(-1)' /></FORM>";
if (!mysql_query($insert,$conn))
	{
	die("Error: ".mysql_error().$error_button);
	}
echo "Record added";
mysql_close($conn);

 ?>
 <div align=center>
<FORM>
<INPUT TYPE="button" VALUE="Add Inventory" onclick="window.location.href='../add_inventory.php';" />
<INPUT TYPE="button" VALUE="Modify Inventory" onclick="window.location.href='../modify_inventory.php';" />
<INPUT TYPE="button" VALUE="View Inventory" onclick="window.location.href='../current_inventory.php';" />
<INPUT TYPE="button" VALUE="Home" onclick="window.location.href='../index.php';" />
</FORM>
</div>
</body>
</html>
The form.php calls the image.php to upload the image and insert the data.
The add_inventory.php calls the add.php to upload the image and insert the data.
crazitrain02
Forum Newbie
Posts: 22
Joined: Wed Dec 08, 2010 5:20 pm

Re: Image Upload Problem

Post by crazitrain02 »

Here's the error that I forgot to add:

Notice: Undefined variable: image_name in C:\Inetpub\wwwroot\crazitrain.com\inventory\Forms\add.php on line 64 Record added
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Image Upload Problem

Post by Jonah Bron »

Without looking at the code, what exactly doesn't work? Do you get an error message? What happens?
crazitrain02
Forum Newbie
Posts: 22
Joined: Wed Dec 08, 2010 5:20 pm

Re: Image Upload Problem

Post by crazitrain02 »

The short answer is, the image is not uploading and the file name is not being inserted into the SQL entry on the production pages.
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Image Upload Problem

Post by Jonah Bron »

Oh, you gave the error whilst I was posting.

Hm, well, the spot where you define the $image_name is in an if...else. Obviously the condition is failing somewhere. Try echoing random stuff at certain points in the if matrix to see where it's getting. Debugging 101.
crazitrain02
Forum Newbie
Posts: 22
Joined: Wed Dec 08, 2010 5:20 pm

Re: Image Upload Problem

Post by crazitrain02 »

So I've managed to narrow it down to the add_inventory.php page, but I'm confused on what I'm doing wrong.
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Image Upload Problem

Post by Jonah Bron »

If the error is on add.php, that's where the problem is. What condition fails that the variable is not defined? That's what you're trying to find out by echoing out random stuff at different points.
crazitrain02
Forum Newbie
Posts: 22
Joined: Wed Dec 08, 2010 5:20 pm

Re: Image Upload Problem

Post by crazitrain02 »

I've echoed out a ton of stuff, and even did an ISSET($_POST['image']) and have come to the conclusion that the 'image' is not being posted to the add.php page. I'm still confused on what I did wrong with the form on the add_inventory.php page where the image is not being posted.
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Image Upload Problem

Post by Jonah Bron »

In add_inventory.php, try running print_r($_FILES).
crazitrain02
Forum Newbie
Posts: 22
Joined: Wed Dec 08, 2010 5:20 pm

Re: Image Upload Problem

Post by crazitrain02 »

I only get Array() on the add_inventory.php, but when I add it to the add.php I get this:
Array ( [image] => Array ( [name] => small_hal.jpg [type] => image/jpeg [tmp_name] => C:\WINDOWS\Temp\php2C8.tmp [error] => 0 [size] => 19336 ) )
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Image Upload Problem

Post by Jonah Bron »

Oops, yeah, I meant add.php. Then yes, the image is being POSTed to the add.php page, so the problem is there.
crazitrain02
Forum Newbie
Posts: 22
Joined: Wed Dec 08, 2010 5:20 pm

Re: Image Upload Problem

Post by crazitrain02 »

I found the problem. In the code snippet that I got the if(isset($_POST['Submit'])) was not correct. I removed that from the code and the image uploads and the image name is uploaded into the MySQL database.

Once again, thank you for the assistance.
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Image Upload Problem

Post by Jonah Bron »

You're welcome, glad you got it working :)
Post Reply