Link to home
Start Free TrialLog in
Avatar of FairyBusiness
FairyBusinessFlag for United States of America

asked on

How to parse an image file in php?

Hello,

I am trying to get only the image name of an uploaded file.  I thought this would work:

$image = mysql_clean_strings($_POST['image']);
$image = basename($_FILES[$image]['name']);

but its not.  Any ideas?
ASKER CERTIFIED SOLUTION
Avatar of Greg Alexander
Greg Alexander
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of FairyBusiness

ASKER

I got this message back:

Notice: Undefined index: image in /hermes/web09c/b2950/moo.auroriellacom/includes/library.php on line 1209

When I did only this:

$image = basename($_FILES['image']['name']);      

This is the whole function:

function update_fields($table) {
	global $conn, $message, $errors, $error;
	if(empty($errors) && isset($_POST['submit'])) { // Perform Update
		if(isset($_GET['product'])) {
			$id = mysql_clean_strings($_GET['product']);
		}
		else {
			$id = item_id();
		}
		$name = mysql_clean_strings($_POST['name']);
		$content = mysql_clean_strings($_POST['content']);
		$image = basename($_FILES['image']['name']);		
		$material_id = update_materials();
		$featured = mysql_clean_strings($_POST['featured']);
		$color = mysql_clean_strings($_POST['color']);
		$stock = mysql_clean_strings($_POST['stock']);
		$price = mysql_clean_strings($_POST['price']);
/*
		$query = "UPDATE " . $table . " SET
				  name = '$name',
				  content = '$content',
				  filename = '$image',
				  material_id = '$material_id',
				  featured = '$featured',
				  color = '$color',
				  stock = '$stock',
				  price = '$price'
				  WHERE id = '$id'";
*/		$result = mysql_query($query, $conn);
		confirm_query($result);
		if ($result) {
			// Successful
			$message = "The product was successfuly updated.";
		}
		else {
			// Failed
			$error = "The product update failed.";
			$error .= "<br />" . mysql_error();
			echo var_dump($result);
		}
	}
}

Open in new window


What is your image field name called?
<input id="image" type="file" name="image" value="" />
That is basically saying that the image field is not between the form tags
<form method="post" action="edit_product.php?edit=<?php echo $table ?>&item=<?php echo ($_GET['item']); ?>">
<input type="text" name="name" value="<?php echo strip_tags(headers()); ?>" class="textLg" />
<br style="clear: both;" />
<iframe id="preview" src="<?php echo display();?>"></iframe>
<div id="info">
<textarea name="content">
<?php echo get_data($field='content', $table);?>
</textarea>
<h4>Materials</h4>
<div id="materials">
<?php
get_table_material();
echo materials();
?>
</div><!-- end #materials -->
</div><!-- end #info -->
<div id="options">
<input type="text" name="price" value="$<?php echo get_data($field='price', $table); ?>" /><p>
Color: <br />
<input type="text" name="color" value="<?php echo color(); ?>" />
<?php
echo qty();
$item = get_item();
?>
</div><!-- end #options -->
<br style="clear: left;" />
<label>Image: <input id="image" type="file" name="image" value="" /></label>
<p>
<label>Metal Id: <input type="text" name="metal_id" value="<?php echo get_data($field='metal_id', $table);?>" class="textSm" /></label>
<label>Featured: Yes <input type="checkbox" name="featured" value="1" />No <input type="checkbox" name="featured" value="0" checked /></label>
<label>In Stock: Yes <input type="checkbox" name="stock" value="1" checked />No <input type="checkbox" name="stock" value="0" /></label>
</p>
<input type="submit" name="submit" value="Update" /></a>
</form>

Open in new window

Ok, I used a var_dump on my $image and I got this back

string(7) "n16.png"

By basename I wanted it without the image type. ..  I do I remove the file type .png  (short of removing the characters of the string)
Are you wanting to make it a png?
I mean a jpg?
No, I want it to be a png.  But I use the file name n17 or whatever as the item name throughout my website so when I retrieve the file name I don't want the .png with it
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
but will that just return the first character? or everything before the period?
Everything before the period
Thanks again :)