image extensions

Hi all, im super stumped here. I have been trying to learn php for some time, and it is terribly confusing to me. I managed to make a product upload script for my shop, but I can’t figure out how to process the product images to main page, and how to send them to product page… So far when I upload the image of product via my backend inventory script it sends the pic to main page HOWEVER it puts it in JPG file extension… (Why does it do this when the file itself is a PNG?) It also sends the image to the product page which is great, but I have 2 distinct images that I need to be placed 1 image is a PNG(will only show a 200x200 preview image) I need it to be placed on main page, and 2nd image which is a JPG,(will be showing a full size of product) I need to be placed on product page. hope you guys can understand all that, I wrote it and I kinda don’t lol Anyway here is the PHP code that I have.

The first block of code is from my backend- add inventory script-

[php]
if (isset($_POST[‘product_name’])) {
$product_name = mysql_real_escape_string($_POST[‘product_name’]);
$price = mysql_real_escape_string($_POST[‘price’]);
$category = mysql_real_escape_string($_POST[‘category’]);
$subcategory = mysql_real_escape_string($_POST[‘subcategory’]);
$details = mysql_real_escape_string($_POST[‘details’]);
// See if that product name is an identical match to another product in the system
$sql = mysql_query(“SELECT id FROM products WHERE product_name=’$product_name’ LIMIT 1”);
$productMatch = mysql_num_rows($sql); // count the output amount
if ($productMatch > 0) {
echo ‘Sorry you tried to place a duplicate “Product Name” into the system, click here’;
exit();
}
// Add this product into the database now
$sql = mysql_query(“INSERT INTO products (product_name, price, details, category, subcategory, date_added)
VALUES(’$product_name’,’$price’,’$details’,’$category’,’$subcategory’,now())”) or die (mysql_error());
$pid = mysql_insert_id();
// Place image in the folder
$newname = “$pid.jpg”;
move_uploaded_file( $_FILES[‘fileField’][‘tmp_name’], “…/inventory_images/$newname”);
header(“location: inventory_list.php”);
exit();
}
?>[/php]

This is the snippet of how I upload my product images.
[php]

Product Image   [/php]

I tried making dual image upload options in the form above , I was trying to make one upload to main page, and the other I was trying to MacGyver it to the product page, but couldn’t make it happen.

Main page where the product will be sent after added via backend inventory script

[php]<?php
// Run a select query to get my letest 6 items
// Connect to the MySQL database
include “storescripts/connect_to_mysql.php”;
$dynamicList = “”;
$sql = mysql_query(“SELECT * FROM products ORDER BY date_added DESC LIMIT 6”);
$productCount = mysql_num_rows($sql); // count the output amount
if ($productCount > 0) {
while($row = mysql_fetch_array($sql)){
$id = $row[“id”];
$product_name = $row[“product_name”];
$price = $row[“price”];
$date_added = strftime("%b %d, %Y", strtotime($row[“date_added”]));
$dynamicList .= ‘

' . $product_name . '’.$product_name.’
';
}
} else {
$dynamicList = “We have no products listed in our store yet”;
}
mysql_close();
?>[/php]

And then I echo out the product list in my html container
[php]<?php echo $dynamicList; ?>[/php]

Hope someone can enlighten me, Seems like im going around in circles on my own.

Why can’t you just add a column of image so whenever you query is just like

not sure what you mean, but I think I have that already in the variable $dynamicList that lists echos and spits out the images to my main page and product page is structured the same way, but my prob is I want my product page image to be different than main page and they need to be loaded with different extensions, because as it is by default they are .JPG and I dont’ know how to change that.

[php]$dynamicList .= ‘

' . $product_name . '’.$product_name.’
';
}
} else {
$dynamicList = “We have no products listed in our store yet”;
}
mysql_close();
?>

And then I echo out the product list in my html container
php: [Select]

1
<?php echo $dynamicList; ?>

[/php]

You have these columns in your table

product_name
price
category
subcategory
details

Right?

to put it more simply, I have image files in PNG format and JPG… but when I upload them via my script they all get outputed as JPG no matter what I do or have done. I think it has something todo with [tmp_name] but im so new to php I wouldn’t know.

[php]












Product Name

Product Price
$

Category

New
Funny
Classic
Mashed
Techy
Comics
College
Sports
Sale
Gear
      <option value="Clothing">Clothing</option>
      <option value="Clothing">Clothing</option>
      </select>
    </label></td>
  </tr>
  <tr>
    <td align="right">Subcategory</td>
    <td><select name="subcategory" id="subcategory">
    <option value=""></option>
      <option value="Hats">Hats</option>
      <option value="Pants">Pants</option>
      <option value="Shirts">Shirts</option>
      </select></td>
  </tr>
  <tr>
    <td align="right">Product Details</td>
    <td><label>
      <textarea name="details" id="details" cols="64" rows="5"></textarea>
    </label></td>
  </tr>
  <tr>
    <td align="right">Product Image</td>
    <td><label>
      <input type="file" name="fileField" id="fileField" />
    </label></td>
  </tr>      
  <tr>
    <td>&nbsp;</td>
    <td><label>
      <input type="submit" name="button" id="button" value="Add This Item Now" />
    </label></td>
  </tr>
</table>
</form>[/php]

this is my test form, that what you needed?

then change this line
$newname = “$pid.jpg”;
to
$newname = $pid;

[php]
if (isset($_POST[‘product_name’])) {
$product_name = mysql_real_escape_string($_POST[‘product_name’]);
$price = mysql_real_escape_string($_POST[‘price’]);
$category = mysql_real_escape_string($_POST[‘category’]);
$subcategory = mysql_real_escape_string($_POST[‘subcategory’]);
$details = mysql_real_escape_string($_POST[‘details’]);
// See if that product name is an identical match to another product in the system
$sql = mysql_query(“SELECT id FROM products WHERE product_name=’$product_name’ LIMIT 1”);
$productMatch = mysql_num_rows($sql); // count the output amount
if ($productMatch > 0) {
echo ‘Sorry you tried to place a duplicate “Product Name” into the system, click here’;
exit();
}
// Add this product into the database now
$sql = mysql_query(“INSERT INTO products (product_name, price, details, category, subcategory, date_added)
VALUES(’$product_name’,’$price’,’$details’,’$category’,’$subcategory’,now())”) or die (mysql_error());
$pid = mysql_insert_id();
// Place image in the folder
//$newname = “$pid.jpg”;
$newname = $pid;
move_uploaded_file( $_FILES[‘fileField’][‘tmp_name’], “…/inventory_images/$newname”);
header(“location: inventory_list.php”);
exit();
}
[/php]

when I take .jpg off of $pid.jpg I get no images at all

I read something about [temp_name] not sure if that helps. When I upload the images for example blahblahblah.PNG it comes out as the id like 10.JPG on the screen

I added this one

[php]
$valid_file_extensions = array(".jpg", “.jpeg”, “.gif”, “.png”);
$file_extension = strrchr($_FILES[“fileField”][“name”], “.”);
if (in_array($file_extension, $valid_file_extensions)) {
$destination = “…/inventory_images/” . $pid;
move_uploaded_file($_FILES[“fileField”][“tmp_name”], $destination);
}
[/php]

Try it

[php]
if (isset($_POST[‘product_name’])) {
$product_name = mysql_real_escape_string($_POST[‘product_name’]);
$price = mysql_real_escape_string($_POST[‘price’]);
$category = mysql_real_escape_string($_POST[‘category’]);
$subcategory = mysql_real_escape_string($_POST[‘subcategory’]);
$details = mysql_real_escape_string($_POST[‘details’]);
// See if that product name is an identical match to another product in the system
$sql = mysql_query(“SELECT id FROM products WHERE product_name=’$product_name’ LIMIT 1”);
$productMatch = mysql_num_rows($sql); // count the output amount
if ($productMatch > 0) {
echo ‘Sorry you tried to place a duplicate “Product Name” into the system, click here’;
exit();
}
// Add this product into the database now
$sql = mysql_query(“INSERT INTO products (product_name, price, details, category, subcategory, date_added)
VALUES(’$product_name’,’$price’,’$details’,’$category’,’$subcategory’,now())”) or die (mysql_error());
$pid = mysql_insert_id();
$valid_file_extensions = array(".jpg", “.jpeg”, “.gif”, “.png”);
$file_extension = strrchr($_FILES[“fileField”][“name”], “.”);
if (in_array($file_extension, $valid_file_extensions)) {
$destination = “…/inventory_images/” . $pid;
move_uploaded_file($_FILES[“fileField”][“tmp_name”], $destination);
}
header(“location: inventory_list.php”);
exit();
}
[/php]

still nothing yet… you can view my test site and see whats up.
shirtstash()com … the first image came through the original way , images 2 and 3 are from experimenting with $pid… and image 4 is result of the code you provided me.

I think something wrong with the location give full path and try it

TEST2

$dynamicList is what gets echoed onto the main page… see where it says .$id. thats why it has the 27.JPG instead of say blahblah.JPG… but I have it set as .$id.png still don’t understand why it comes out jpg though

$dynamicList .= ’

' . $product_name . '

yea im lost

The image does not exists check the folder if it has images you uploaded

they all are in the folder, but the one that shows on the page 26.jpg is the only one with file extension in the folder there for viewable… the others have no extensions and cannot be viewed.

try changing to and upload another image

$destination = “…/inventory_images/” . $_FILES[“fileField”][“name”];
move_uploaded_file($_FILES[“fileField”][“tmp_name”], $destination);

instead of $pid

Very strange i tried this way and works without extention

TEST4

remove the extension

' . $product_name . '

still no image, but it did save correctly in my image folder… however it still gets output as id.jpg… if you click on the latest one and goto product page view image, and change inventory_images/30.jpg to megaman.png and thats how it should be

that seemed to help some, but its strange some of the older tests came through while the new one I tryed did not? lol btw they are getting added to the image folder as png now though

Is it ok now

Sponsor our Newsletter | Privacy Policy | Terms of Service