Jump to content

Uploading Images


dromorkid

Recommended Posts

Hey
I'm new to both php and this forum so bare with me if you will!

I've just been landed with finishing up an excoworkers work and need help in uploading images to the database.

I'm using DW 8 and the files are being stored on Plesk. I'm uploading the usual information to the database relating to products i.e. name, description, etc. I have no problem with inserting this information into the database but there is an image field that requires the user to upload an image to the database. Right now I'm baffled. I can browse my computer for the image no problem. My next interface is setproduct.php (I'm not sure if this guy created this page or took it from somewhere so maybe this is familiar to some of you). On this page it informs me of the information that has been uploaded successfully to the database. The image file is not being uploaded. When I check the database all the information is stored correctly in the their respective places INCLUDING the name of the image. This tells me that the image's pathway is there but the actual image file did not upload to the databse therefore its being displayed as a blank image.

Is there some kind of add on that I need to use to upload the image? IS there code to upload the image? Can anyone help? PLease, I'd be more than grateful if someone could reply. Anything that is needed I can provide i.e. the code. The website is being stored on Unix.

Heres hoping!
Conor
Link to comment
Share on other sites

[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]Is there some kind of add on that I need to use to upload the image? [/quote]

No, upload functions are built into php.

[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]IS there code to upload the image? [/quote]

Yes, code to upload images exists. We can't tell if the code your using contains the code to handle file uploads because we can't see your code. Please post the relavent parts if you would like us to help.

[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]Can anyone help? [/quote]

Probably.
Link to comment
Share on other sites

[!--quoteo(post=356035:date=Mar 17 2006, 05:26 PM:name=hitman6003)--][div class=\'quotetop\']QUOTE(hitman6003 @ Mar 17 2006, 05:26 PM) [snapback]356035[/snapback][/div][div class=\'quotemain\'][!--quotec--]Probably.[/quote]

I am going through these pains as we speak. If there is anything I can assist you with there is no PROBABLY. I swear it makes no sense why folks think because they are smarter then the average bear they gotta skip the average bear.....but then again I rekkon thats why I'll never find myself on The Geek and the Beauty.....heh

almost forgot...I had this same problem and I figured it out via lots of searching..... it had to do with my upload directory....I used the following.....takin out my stuff but you can add yours accordingly....

$(your diretory word goes here)= $_SERVER['DOCUMENT_ROOT'].'/usr/your server info';

If anyone has a better suggestion then hopefully it'll be better then PROBABLY
Link to comment
Share on other sites

Ok, I'm aware I have posted no code, but I'm not sure what file deals with uploading images. I can paste the code here but there are a few files to choose from, so I will name you the files and hopefully you can tell me which ones would most likely contain the relevant code. Please forgive this tactless post.

[u]File 1: generateimage.php [/u]

<?php

define(thumbnailWidth, "150");

$image = $_GET['image'];

$src = ImageCreateFromJPEG($image);

$width = ImageSx($src);
$height = ImageSy($src);
$x = thumbnailWidth;
$y = (int)(($x*$height) / $width );

$dst = ImageCreateTrueColor($x,$y);
ImageCopyResampled($dst,$src,0,0,0,0,$x,$y,$width,$height);
header('Content-type: image/png');
ImagePNG($dst);

?>
[u]File 2: setproduct.php[/u]

<?

require("../librequire.php");

$action = $_POST['action'];

$imagefile = isset($_FILES['image']['name'])? $_FILES['image']['name']:'noimage.jpg';

$product = new Product(
$_POST['name'],$_POST['manufacturer'],$imagefile,
$_POST['address'],$_POST['city'],$_POST['state'],
$_POST['zip'],$_POST['phone'],$_POST['url'],$_POST['comments']
);


if ($action == 'addrecord')
{

$results = $product->runQuery(0,'insert');

if($results)
{
if ($_FILES['image']['name'])
{
$uploadfile = $product_image_root . $_FILES['image']['name'];

if (move_uploaded_file($_FILES['image']['tmp_name'], $uploadfile))
{
$uploadstatus = "File " . '"' . $_FILES['image']['name'] . '"' . " successfully uploaded";
}
}
else
{
$uploadstatus = "No vessel image file uploaded";
}

$sqlresults = "SQL Success: " . $product->num_rows . " Row(s) Affected (Product(s)Added)";
}
else
{
$sqlresults = "SQL Fail: " . $product->num_rows . " Row(s) Affected (Product(s) Added)";
}
}

if ($action == 'modifyrecord')
{
$id = $_POST['id'];

$results = $product->runQuery($id,'update');
if($results)
{
if (isset($_FILES['image']['name'])? $_FILES['image']['name']:'')
{
$uploadfile = $product_image_root . $_FILES['image']['name'];
if (move_uploaded_file($_FILES['image']['tmp_name'], $uploadfile))
{
$uploadstatus = "New File " . '"' . $_FILES['image']['name'] . '"' . " successfully uploaded";
}
}
else
{
$uploadstatus = "No new vessel image file uploaded";
}
$sqlresults = "SQL Success: " . $product->num_rows . " Row(s) Modified (Product(s) Modified)";
}
else
{
$sqlresults = "SQL Fail: " . $product->num_rows . " Row(s) Affected (Product(s) Modified)";
}
}

if ($action == 'deleterecord')
{
$id = $_POST['id'];

$results = $product->runQuery($id,'deletebyid');
if($results)
{
$sqlresults = "SQL Success: " . $product->num_rows . " Row(s) Deleted (Product(s) Deleted)";
}
else
{
$sqlresults = "SQL Fail: " . $product->num_rows . " Row(s) Deleted (Product(s) Deleted)";
}

}

?>
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
<html> <head>
<title></title>
<link href="style.css" rel="stylesheet" type="text/css">
</head>
<body>
<div ID="sqlstatus">
<h1>Administration Results Page</h1>
The following is the sql status for the operation:<br>
SQL Results: <?= $sqlresults ?><br>
Upload File: <?= $uploadstatus ?><br>
</body>
<hr>

<div ID="menubottom">[ <a href="index.html">Admin Home</a> ||<a href="addproduct.php">Add Product</a>|| <a href="editproduct.html">Edit Product</a> ]</div>

</div>

[u]File 3: addproduct.php[/u]

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
<html> <head>
<title></title>
<link href="style.css" rel="stylesheet" type="text/css">
</head>

<body>
<h1>Add Product</h1>

<table width="100%" height="370" border="0" cellpadding="0" cellspacing="0" style="background-image: url(images/page_structure/30_pack_jack_content_bg.gif); background-repeat:no-repeat;">
<tr>
<td align="left" valign="top"> <br>
<form enctype="multipart/form-data" name="form1" method="POST" action="setproduct.php">
<input type="hidden" name="action" value="addrecord">
<table align="center" border="0" width="400" cellpadding="2" cellspacing="0" id="contact">
<tr>
<td align="right" valign="middle"><p><span class="required">*</span>Product
Name: </p></td>
<td align="left" valign="middle"><input name="name" type="text" class="formFieldReq" size="30"></td>
</tr>
<tr>
<td align="right" valign="middle"><p><span class="required">*</span>Manufacturer
Name: </p></td>
<td align="left" valign="middle"><input name="manufacturer" type="text" class="formFieldReq" size="30"></td>
</tr>
<tr>
<td align="right" valign="middle"><p><span class="required">*</span>Image: </p></td>
<td align="left" valign="middle"><input name="image" type="file" class="formFieldReq" size="30"></td>
</tr>
<tr>
<td align="right" valign="middle"><p>Street Address: </p></td>
<td align="left" valign="middle"><input type="text" name="address" size="30"></td>
</tr>
<tr>
<td align="right" valign="middle"><p>City: </p></td>
<td align="left" valign="middle"><input type="text" name="city" size="30"></td>
</tr>
<tr>
<td align="right" valign="middle"><p>State: </p></td>
<td align="left" valign="middle"><select name="state">
<option value="AL">Alabama</option>
<option value="AK">Alaska</option>
<option value="AZ">Arizona</option>
<option value="AR">Arkansas</option>
<option value="CA">California</option>
<option value="CO">Colorado</option>
<option value="CT">Connecticut</option>
<option value="DE">Delaware</option>
<option value="FL">Florida</option>
<option value="GA">Georgia</option>
<option value="HI">Hawaii</option>
<option value="ID">Idaho</option>
<option value="IL">Illinois</option>
<option value="IN">Indiana</option>
<option value="IA">Iowa</option>
<option value="KS">Kansas</option>
<option value="KY">Kentucky</option>
<option value="LA">Louisiana</option>
<option value="ME">Maine</option>
<option value="MD">Maryland</option>
<option value="MA">Massachusetts</option>
<option value="MI">Michigan</option>
<option value="MN">Minnesota</option>
<option value="MS">Mississippi</option>
<option value="MO">Missouri</option>
<option value="MT">Montana</option>
<option value="NE">Nebraska</option>
<option value="NV">Nevada</option>
<option value="NH">New Hampshire</option>
<option value="NJ">New Jersey</option>
<option value="NM">New Mexico</option>
<option value="NY">New York</option>
<option value="NC">North Carolina</option>
<option value="ND">North Dakota</option>
<option value="OH">Ohio</option>
<option value="OK">Oklahoma</option>
<option value="OR">Oregon</option>
<option value="PA">Pennsylvania</option>
<option value="RI">Rhode Island</option>
<option value="SC">South Carolina</option>
<option value="SD">South Dakota</option>
<option value="TN">Tennessee</option>
<option value="TX">Texas</option>
<option value="UT">Utah</option>
<option value="VT">Vermont</option>
<option value="VA">Virginia</option>
<option value="WA">Washington</option>
<option value="WV">West Virginia</option>
<option value="WI">Wisconsin</option>
<option value="WY">Wyoming</option>
</select> </td>
</tr>
<tr>
<td align="right" valign="middle"><p>Zip code: </p></td>
<td align="left" valign="middle"><input name="zip" type="text" size="11" maxlength="5"></td>
</tr>
<tr>
<td align="right" valign="middle"><p>Day Phone: </p></td>
<td align="left" valign="middle"><input type="text" name="phone" size="30"></td>
</tr>
<tr>
<td align="right" valign="middle"><p>URL: </p></td>
<td align="left" valign="middle"><input type="text" name="url" size="30"></td>
</tr>
<tr>
<td align="right" valign="middle"><p>Comments: </p></td>
<td align="left" valign="middle"><textarea name="comments" cols="40" rows="5"></textarea></td>
</tr>
<tr>
<td align="center" colspan="2"><input type="submit" value="Submit" class="button">
<input type="reset" class="button"></td>
</tr>
</table>
</form></td>
</tr>
</table></td>
<td width="10" background="images/page_structure/30_pack_jack_right_repeat.gif"> </td>
</tr>
</table></td>


<hr>
<div ID="menubottom">[ <a href="index.html">Admin Home</a> ||<a href="addreview.html">Add Review</a> || <a href="editreview.html">Edit Review</a> || <span class="chosen">Add Product</span> || <a href="editproduct.html">Edit Product</a> ]</div>
</body>
</html>

[u]File 4: librequire.php[/u]

<?

require_once(getenv('CLASS_ROOT') . 'Base.php');
require_once(getenv('CLASS_ROOT') . 'MySQL_Access.php');
require_once(getenv('CLASS_ROOT') . 'CaptainsGuide_Admin.php');
require_once(getenv('CLASS_ROOT') . 'Review.php');
require_once(getenv('CLASS_ROOT') . 'Product.php');
require_once(getenv('CLASS_ROOT') . 'Advertiser.php');

//require_once('generateimage.php');
$review_image_root = getenv('DOCUMENT_ROOT') . '/' . getenv('REVIEW_IMAGE_ROOT');
$product_image_root = getenv('DOCUMENT_ROOT') . '/' . getenv('PRODUCT_IMAGE_ROOT');
error_reporting(E_ALL);

?>


Sorry for this, I understand if this is way too much hassle for anyone. Help would be appreciated though
Conor
Link to comment
Share on other sites

[!--quoteo(post=356105:date=Mar 17 2006, 10:54 PM:name=dromorkid)--][div class=\'quotetop\']QUOTE(dromorkid @ Mar 17 2006, 10:54 PM) [snapback]356105[/snapback][/div][div class=\'quotemain\'][!--quotec--]
$review_image_root = getenv('DOCUMENT_ROOT') . '/' . getenv('REVIEW_IMAGE_ROOT');
$product_image_root = getenv('DOCUMENT_ROOT') . '/' . getenv('PRODUCT_IMAGE_ROOT');
error_reporting(E_ALL);
[/quote]

If the script is procesing fine and you have ran die scripts throughout the script to see what is not functioning then it has to do with your above directories...plus I didn't see a tmp directory defined. Something like below may help you out but a lot of what I have learned has been trial and error :) Just play around with a bit and you'll get it. Run a search on php.net because you may find some examples in the manual. Wish I could help more but I'm just a nOOb :)
$product_image_root = $_SERVER['DOCUMENT_ROOT'].'/Images/';
Link to comment
Share on other sites

The images are not actually being stored in the database, rather the location of them is being stored. The image files are being stored on the file system:

[code]$uploadfile = $product_image_root . $_FILES['image']['name'];
//move the file to it's new location in the file system
if (move_uploaded_file($_FILES['image']['tmp_name'], $uploadfile)) {
    //if that was successful, set the message
    $uploadstatus = "File " . '"' . $_FILES['image']['name'] . '"' . " successfully uploaded";
}[/code]

As was stated above, the $product_image_root is equal to :

[code]$product_image_root = getenv('DOCUMENT_ROOT') . '/' . getenv('PRODUCT_IMAGE_ROOT');[/code]

and PRODUCT_IMAGE_ROOT is defined else where.

If your having trouble viewing the image, it's probably happening in whatever pages you are using to view the products.
Link to comment
Share on other sites

Thanks, I'm understanding what you're saying, the logic of the whole thing. I know now that the files are not being stored on the database and I'm assuming where I see $_File that that indicates file system. Right? Anyway, to store them on the database what do I need to change this code to in order to store it database? I know this is a pain for you guys, but it'll be a major help to me. I spent eons using asp and now this comes along! Sorry. Oh and I'm pretty sure thats it not viewing the images where the problem is, its the upload as the error message appears after I click 'Submit'.
Conor

Link to comment
Share on other sites

There is a php file generateimage.php The code is as follows, maybe my problem is here? Can you have a look anyone and see, please! This is annoying me so bad.

[code]<?php

define(thumbnailWidth, "150");    

$image = $_GET['image'];

$src = ImageCreateFromJPEG($image);

$width  = ImageSx($src);
$height = ImageSy($src);
$x = thumbnailWidth;
$y = (int)(($x*$height) / $width );

$dst = ImageCreateTrueColor($x,$y);
ImageCopyResampled($dst,$src,0,0,0,0,$x,$y,$width,$height);
header('Content-type: image/png');
ImagePNG($dst);

?>[/code]
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.