Page 1 of 1

PHP image upload to directory

Posted: Sun Apr 24, 2011 3:09 am
by Andorphin
Hi All

Been desperate to crack this nut for a few years now, but seem to be failing epically!

I have been building a property/real estate web site for a long time, but I have no training and have been learning as I go. I have a script written that allows me to upload data and store it in my database, what I was looking to do was add a section that allows me to upload multiple images at the same time, and store the location to another table in the same database.

So what I was looking to do was remove all the...lets say rubbish to be nice...and see where it takes us.

I apologize in advance for the lack of santized code...I am coding locally only, in an admin area, and will rectify before launching the site...just trying to get the bones of the script in place.

Ok, first we have my form as shown here, warts and all.

Code: Select all

<?php
/**
 * Create Listing
 */
 
?>

<div id="admin" class="intern">
    <div class="banner">
      <div class="logbanner">CREATE LISTING</div>
    </div>
	<div class="padding">
	<form action="adminprocess.php" method="POST" enctype="multipart/form-data">
		<table border="0" cellpadding="2" cellspacing="2" width="700">
			<tr>
				<td align="right"><label class="text">Property Title:&nbsp;</label></td>
				<td><input type="text" name="property_title" maxlength="50" value="" size="50"></td>
			</tr>
			<tr>
				<td align="right"><label class="text">Selling Agent:&nbsp;</label></td>
				<td><input type="text" name="agent" maxlength="30" value="<?php echo $session->username; ?>"></td>
			</tr>
			<tr>
				<td align="right"><label class="text">Style:&nbsp;</label></td>
				<td>
					<select id="property_type" name="property_type">
						<option value="Not Selected" selected>Not Selected</option>
						<option value="Detached">Detached</option>
						<option value="Semi-Deatched">Semi-Detached</option>
						<option value="End Terrace">End Terrace</option>
						<option value="Mid Terrace">Mid Terrace</option>
						<option value="Flat">Flat</option>
						<option value="Ground Floor Flat">Ground Floor Flat</option>
						<option value="4 in a Block">4 in a Block</option>
						<option value="Bungalow">Bungalow</option>
						<option value="Apartment">Apartment</option>
						<option value="Studio">Studio</option>
						<option value="Maisonette">Maisonette</option>
					</select>
				</td>
			</tr>
			<tr>
				<td align="right"><label class="text">Address:&nbsp;</label></td>
				<td><input type="text" name="address_one" maxlength="30" value="" size="50"></td>
			</tr>
			<tr>
				<td align="right"><label class="text">Address:&nbsp;</label></td>
				<td><input type="text" name="address_two" maxlength="30" value="" size="50"></td>
			</tr>
			<tr>
				<td align="right"><label  class="text">Town:&nbsp;</label></td>
				<td><input type="text" name="town" maxlength="30" value="" size="50"></td>
			</tr>
			<tr>
				<td align="right"><label class="text">County:&nbsp;</label></td>
				<td>
					<select id="county" name="county">
						<option value="Fife" selected>Fife</option>
					</select>
				</td>
			</tr>
			<tr>
				<td align="right"><label class="text">Post Code:&nbsp;</label></td>
				<td><input type="text" name="postcode" maxlength="30" value="" size="15"></td>
			</tr>
			<tr>
				<td align="right"><label class="text">Price:&nbsp;</label></td>
				<td>
					<select id="price_cond" name="price_cond">
						<option value="Not Selected" selected>Not Selected</option>
						<option value="Offers Over">Offers Over</option>
						<option value="Fixed Price">Fixed Price</option>
					</select>
						<input type="text" name="price" maxlength="30" value="" size="32">
				</td>
			</tr>
			<tr>
				<td align="right"><label class="text">Property Status:&nbsp;</label></td>
				<td>
					<select id="status" name="status">
						<option value="Not Selected" selected>Not Selected</option>
						<option value="Offers Over">For Sale</option>
						<option value="Fixed Price">Sold Subject to Signed Missives</option>
						<option value="Fixed Price">Sold Subject to Survey</option>
						<option value="Fixed Price">Price Reduced</option>
						<option value="Fixed Price">Sold</option>
						<option value="Fixed Price">Re-Listed</option>
					</select>
				</td>
			</tr>
			<tr>
				<td align="right"><label class="text">Description Header:&nbsp;</label></td>
				<td><input type="text" name="deschead" maxlength="30" value="" size="50"></td>
			</tr>
			<tr>
				<td align="right"><label class="text">Description:&nbsp;</label></td>
				<td><textarea cols="40" rows="8" type="text" name="desc" maxlength="1000" value=""></textarea></td>
			</tr>
			
			
			
			
			
<!-- I want the images to be added here -->





			<tr>
				<td align="right"><label class="text">Reception Rooms:&nbsp;</label></td>
				<td>
					<select id="recrooms" name="recrooms">
						<option value="0" selected>Not Selected</option>
						<option value="1">1</option>
						<option value="2">2</option>
						<option value="3">3</option>
						<option value="4">4</option>
						<option value="5">5</option>
						<option value="6">6</option>
						<option value="7">7+</option>
					</select>
				</td>
			</tr>
			<tr>
				<td align="right"><label class="text">Bedrooms:&nbsp;</label></td>
				<td>
					<select id="bedrooms" name="bedrooms">
						<option value="0" selected>Not Selected</option>
						<option value="1">1</option>
						<option value="2">2</option>
						<option value="3">3</option>
						<option value="4">4</option>
						<option value="5">5</option>
						<option value="6">6</option>
						<option value="7">7+</option>
					</select>
				</td>
			</tr>
			<tr>
				<td align="right"><label class="text">Bathrooms:&nbsp;</label></td>
				<td>
					<select id="bathrooms" name="bathrooms">
						<option value="0" selected>Not Selected</option>
						<option value="1">1</option>
						<option value="2">2</option>
						<option value="3">3</option>
						<option value="4">4</option>
						<option value="5">5</option>
						<option value="6">6</option>
						<option value="7">7+</option>
					</select>
				</td>
			</tr>
			<tr>
				<td align="right"><label class="text">Garden:&nbsp;</label></td>
				<td>
					<select id="garden" name="garden">
						<option value="Not Selected" selected>Not Selected</option>
						<option value="Yes">Yes</option>
						<option value="No">No</option>
						<option value="Shared">Shared</option>
					</select>
				</td>
			</tr>
			<tr>
				<td align="right"><label class="text">Garage:&nbsp;</label></td>
				<td>
					<select id="garage" name="garage">
						<option value="Not Selected" selected>Not Selected</option>
						<option value="Yes">Yes</option>
						<option value="No">No</option>
						<option value="Shared">Shared</option>
					</select>
				</td>
			</tr>
			<tr>
				<td align="right"><label class="text">Additional Info:&nbsp;<br />** Not seen by site users **</label></td>
				<td><textarea cols="40" rows="8" type="text" name="addinfo" maxlength="1000" value=""></textarea></td>
			</tr>
			<tr>
				<td align="right"><label class="text">&nbsp;</label></td>
				<td>
					<input type="hidden" name="subcreatelisting" value="1">
					<input type="submit" value="Create Listing">
				</td>
			</tr>
		</table>
	</form> 
<?php echo $form->error("creListing"); ?>
</div>
</div>
next I have my process page which has the following function included...

Code: Select all

function procCreateListing(){

      global $session, $database, $form;
         $q = "INSERT INTO ".TBL_PROP."(prop_id, prop_agent, prop_type, prop_add_1, prop_add_2, prop_town, prop_county, prop_pc, prop_price, prop_price_cond, prop_rec_rooms, prop_bedrooms, prop_bathrooms, prop_status, prop_garden, prop_garage, prop_description_header, prop_description, prop_add_info, prop_add_date) VALUES ('".$_POST['property_title']."', '".$_POST['agent']."', '".$_POST['property_type']."', '".$_POST['address_one']."', '".$_POST['address_two']."', '".$_POST['town']."', '".$_POST['county']."', '".$_POST['postcode']."', '".$_POST['price']."', '".$_POST['price_cond']."', '".$_POST['recrooms']."', '".$_POST['bedrooms']."', '".$_POST['bathrooms']."', '".$_POST['status']."', '".$_POST['garden']."', '".$_POST['garage']."', '".$_POST['deschead']."', '".$_POST['desc']."', '".$_POST['addinfo']."', '".$_POST['date']."')";
         $database->query($q);


   }
and I have 2 tables in mysql

prop_listing and imagebin

Code: Select all

CREATE TABLE `kea_userclass`.`prop_listing` (
`prop_id` INT( 10 ) NOT NULL AUTO_INCREMENT ,
`prop_agent` VARCHAR( 40 ) NOT NULL ,
`prop_type` VARCHAR( 20 ) NOT NULL ,
`prop_add_1` VARCHAR( 50 ) NOT NULL ,
`prop_add_2` VARCHAR( 50 ) NOT NULL ,
`prop_town` VARCHAR( 30 ) NOT NULL ,
`prop_county` VARCHAR( 25 ) NOT NULL ,
`prop_pc` VARCHAR( 8 ) NOT NULL ,
`prop_price` INT( 10 ) NOT NULL ,
`prop_price_cond` VARCHAR( 20 ) NOT NULL ,
`prop_rec_rooms` INT( 2 ) NOT NULL ,
`prop_bedrooms` INT( 3 ) NOT NULL ,
`prop_bathrooms` INT( 2 ) NOT NULL ,
`prop_status` VARCHAR( 20 ) NOT NULL ,
`prop_garage` INT( 1 ) NOT NULL ,
`prop_garden` INT( 1 ) NOT NULL ,
`prop_description_header` VARCHAR( 50 ) NOT NULL ,
`prop_description` VARCHAR( 2000 ) NOT NULL ,
`prop_add_info` VARCHAR( 2000 ) NOT NULL ,
`prop_add_date` DATE NOT NULL ,
PRIMARY KEY ( `prop_id` ) 
) ENGINE = MYISAM ;

CREATE TABLE `imagebin` (
`id` int( 11 ) NOT NULL AUTO_INCREMENT ,
`item_id` int( 11 ) NOT NULL ,
`image` varchar( 255 ) NOT NULL ,
PRIMARY KEY ( `id` ) ) ENGINE = MYISAM DEFAULT CHARSET = latin1;

 
first of all thanks for getting this far, I know it is a lot of reading, and a lot to ask for, but now I am stuck...
  1. [li]I need a section on my form that would allow me to upload 8 images, and store them in a directory folder[/li]
    [li]I need the location saved into my imagebin table[/li]
    [li]thats it just now[/li]
Many thanks for reading, and I hope someone can take me under their wing and help me passed this hurdle

Re: PHP image upload to directory

Posted: Fri Apr 29, 2011 5:27 pm
by incubi
Hi Andorphin,
I need a section on my form that would allow me to upload 8 images, and store them in a directory folder
Have you looked at http://php.net/manual/en/features.file- ... ltiple.php? If you need help with it let me know.
I need the location saved into my imagebin table
To do this just get the path to the file. You know the file name. So you can use dirname() here's how its used. Test it and mod it for your full path with file name. I would add that the sample below gets the path to where the php file it is in is located. So what ever directory you put this code in is the dir it will return.

Code: Select all

<?php
$dir = dirname(__FILE__);
echo "<p>Path to this dir: " . $dir . "</p>";
?>
However, you know that path or wouldn't be uploading files to a directory so just use the base path you picked to upload the files to then add the file name to the end of it. :) Again try to get it to work and if you can't get it post what you did and I'll help.


Lee

Re: PHP image upload to directory

Posted: Tue May 03, 2011 4:39 pm
by Andorphin
Hey Lee...sorry for not getting back quicker, net went down over bank holiday weekend.

I have this which does half of what I want:

Code: Select all

   function procCreateListing(){

      global $session, $database, $form;
         $q = "INSERT INTO ".TBL_PROP."(prop_id, prop_agent, prop_type, prop_add_1, prop_add_2, prop_town, prop_county, prop_pc, prop_price, prop_price_cond, prop_rec_rooms, prop_bedrooms, prop_bathrooms, prop_status, prop_garden, prop_garage, prop_description_header, prop_description, prop_add_info, prop_add_date) VALUES ('".$_POST['property_title']."', '".$_POST['agent']."', '".$_POST['property_type']."', '".$_POST['address_one']."', '".$_POST['address_two']."', '".$_POST['town']."', '".$_POST['county']."', '".$_POST['postcode']."', '".$_POST['price']."', '".$_POST['price_cond']."', '".$_POST['recrooms']."', '".$_POST['bedrooms']."', '".$_POST['bathrooms']."', '".$_POST['status']."', '".$_POST['garden']."', '".$_POST['garage']."', '".$_POST['deschead']."', '".$_POST['desc']."', '".$_POST['addinfo']."', '".$_POST['date']."')";
         $database->query($q);
 

		/*** define the upload directory where images to be found ***/
		$imgdir = '../includes/ListingImages';
		$imgdirr = 'includes/ListingImages';
		for ($i=0; $i < 8; $i++) {
			$uploaded_files = $_FILES['propImage'. $i]['name'];
				// strip slashes from file name
			$uploaded_files = stripslashes($uploaded_files);
			$uploaded_files = str_replace("'","",$uploaded_files);
			copy($_FILES['propImage'. $i]['tmp_name'], $imgdir . '/' . $uploaded_files);
			
			$path = $imgdirr . "/" . $uploaded_files;
			$qry = "INSERT INTO ".TBL_IMG."(item_id, image) VALUES ('1', '$path')";
			$database->query($qry);
			}
         header("Location: ".$session->referrer);
 }
my form is :

Code: Select all

<form action="adminprocess.php" method="POST" enctype="multipart/form-data">
Blah Blah form stuff here...
			<tr>
				<td align="right"><label class="text">Add Image:&nbsp;</label></td>
				<td><input type="file" name="propImage0"></td>
			</tr>
			<tr>
				<td align="right"><label class="text">Add Image:&nbsp;</label></td>
				<td><input type="file" name="propImage1"></td>
			</tr>
			<tr>
				<td align="right"><label class="text">Add Image:&nbsp;</label></td>
				<td><input type="file" name="propImage2"></td>
			</tr>
			<tr>
				<td align="right"><label class="text">Add Image:&nbsp;</label></td>
				<td><input type="file" name="propImage3"></td>
			</tr>
			<tr>
				<td align="right"><label class="text">Add Image:&nbsp;</label></td>
				<td><input type="file" name="propImage4"></td>
			</tr>
			<tr>
				<td align="right"><label class="text">Add Image:&nbsp;</label></td>
				<td><input type="file" name="propImage5"></td>
			</tr>
			<tr>
				<td align="right"><label class="text">Add Image:&nbsp;</label></td>
				<td><input type="file" name="propImage6"></td>
			</tr>
			<tr>
				<td align="right"><label class="text">Add Image:&nbsp;</label></td>
				<td><input type="file" name="propImage7"></td>
			</tr>
More stuff here
</form>
and my image db is as follows:

CREATE TABLE `imagebin` (
`id` int( 11 ) NOT NULL AUTO_INCREMENT ,
`item_id` int( 11 ) NOT NULL ,
`image` varchar( 255 ) NOT NULL ,
PRIMARY KEY ( `id` ) ) ENGINE = MYISAM DEFAULT CHARSET = latin1;

I have been toying around to get this project finished but have some other issues.
I have 1 form posting data to 2 databases, how do I link the 2 together so they relate to each other?
and how do I amend my code to ignore blank image fields on my form as just now my code fills my database with blank paths...

Re: PHP image upload to directory

Posted: Wed May 04, 2011 9:31 am
by incubi
I have 1 form posting data to 2 databases, how do I link the 2 together so they relate to each other?
For this you could use join over two databases like this

SELECT
c.customername,
o.order_date
FROM
db1.tbl_customers c LEFT JOIN
db2.tbl_orders o ON o.customer_id = c.id

There's a bunch of info and examples on this if you google


When you say "blank paths in the database" what do you mean? Do you mean empty field in the db?
Or are you getting a value in the db field you don't want?


Lee