PDA

View Full Version : images are uploading with a weird name


gavimobile
09-14-2007, 01:53 PM
hey folks,

here is a script which will insert data into mysql table. seems to be working fine except for the fact that it uploads the picture to a weird name.
it looks like this in my sql db
/tmp/phpBK2tEj
but its actually a picture

heres the script

<?php require_once('../../Connections/connect.php');

if (!$con)
{
die('Could not connect: ' . mysql_error());
}mysql_select_db("baronre_baron", $con);mysql_query("INSERT into propertyadd (AgentID, City, Neighborhood, Price, Bed, Bath, LivingSpace, OutdoorSpace, LotSize, Status, Type, Elevator, Parking, Garden, Terrace, Garage, Description, Picture1, Picture2, Picture3, Picture4, Picture5, Picture6 )
values ('$AgentID', '$City', '$Neighborhood', '$Price', '$Bed', '$Bath', '$LivingSpace', '$OutdoorSpace', '$LotSize', '$Status', '$Type', '$Elevator', '$Parking', '$Garden', '$Terrace', '$Garage', '$Description', '$Picture1', '$Picture2', '$Picture3', '$Picture4', '$Picture5', '$Picture6' )");
//print_r($_POST);
if($_POST["action"] == "Upload Image")
{
unset($imagename);
if(!isset($_FILES) && isset($HTTP_POST_FILES))
$_FILES = $HTTP_POST_FILES;
if(!isset($_FILES['Picture1']))
$error["Picture1"] = "An image was not found.";

$imagename = basename($_FILES['Picture1']['name']);
//echo $imagename;
if(empty($imagename))
$error["imagename"] = "The name of the image was not found.";
if(empty($error))
{
$newimage = "images/" . $imagename;
//echo $newimage;
$result = @move_uploaded_file($_FILES['Picture1']['tmp_name'], $newimage);
if(empty($result))
$error["result"] = "There was an error moving the uploaded file.";
}
}
//print_r($_POST);
if($_POST["action"] == "Upload Image")
{
unset($imagename);
if(!isset($_FILES) && isset($HTTP_POST_FILES))
$_FILES = $HTTP_POST_FILES;
if(!isset($_FILES['Picture2']))
$error["Picture2"] = "An image was not found.";

$imagename = basename($_FILES['Picture2']['name']);
//echo $imagename;
if(empty($imagename))
$error["imagename"] = "The name of the image was not found.";
if(empty($error))
{
$newimage = "images/" . $imagename;
//echo $newimage;
$result = @move_uploaded_file($_FILES['Picture2']['tmp_name'], $newimage);
if(empty($result))
$error["result"] = "There was an error moving the uploaded file.";
}
}

//print_r($_POST);
if($_POST["action"] == "Upload Image")
{
unset($imagename);
if(!isset($_FILES) && isset($HTTP_POST_FILES))
$_FILES = $HTTP_POST_FILES;
if(!isset($_FILES['Picture3']))
$error["Picture3"] = "An image was not found.";

$imagename = basename($_FILES['Picture3']['name']);
//echo $imagename;
if(empty($imagename))
$error["imagename"] = "The name of the image was not found.";
if(empty($error))
{
$newimage = "images/" . $imagename;
//echo $newimage;
$result = @move_uploaded_file($_FILES['Picture3']['tmp_name'], $newimage);
if(empty($result))
$error["result"] = "There was an error moving the uploaded file.";
}
}

//print_r($_POST);
if($_POST["action"] == "Upload Image")
{
unset($imagename);
if(!isset($_FILES) && isset($HTTP_POST_FILES))
$_FILES = $HTTP_POST_FILES;
if(!isset($_FILES['Picture4']))
$error["Picture4"] = "An image was not found.";

$imagename = basename($_FILES['Picture4']['name']);
//echo $imagename;
if(empty($imagename))
$error["imagename"] = "The name of the image was not found.";
if(empty($error))
{
$newimage = "images/" . $imagename;
//echo $newimage;
$result = @move_uploaded_file($_FILES['Picture4']['tmp_name'], $newimage);
if(empty($result))
$error["result"] = "There was an error moving the uploaded file.";
}
}

//print_r($_POST);
if($_POST["action"] == "Upload Image")
{
unset($imagename);
if(!isset($_FILES) && isset($HTTP_POST_FILES))
$_FILES = $HTTP_POST_FILES;
if(!isset($_FILES['Picture5']))
$error["Picture5"] = "An image was not found.";

$imagename = basename($_FILES['Picture5']['name']);
//echo $imagename;
if(empty($imagename))
$error["imagename"] = "The name of the image was not found.";
if(empty($error))
{
$newimage = "images/" . $imagename;
//echo $newimage;
$result = @move_uploaded_file($_FILES['Picture5']['tmp_name'], $newimage);
if(empty($result))
$error["result"] = "There was an error moving the uploaded file.";
}
}

//print_r($_POST);
if($_POST["action"] == "Upload Image")
{
unset($imagename);
if(!isset($_FILES) && isset($HTTP_POST_FILES))
$_FILES = $HTTP_POST_FILES;
if(!isset($_FILES['Picture5']))
$error["Picture5"] = "An image was not found.";

$imagename = basename($_FILES['Picture6']['name']);
//echo $imagename;
if(empty($imagename))
$error["imagename"] = "The name of the image was not found.";
if(empty($error))
{
$newimage = "images/" . $imagename;
//echo $newimage;
$result = @move_uploaded_file($_FILES['Picture6']['tmp_name'], $newimage);
if(empty($result))
$error["result"] = "There was an error moving the uploaded file.";
}
}

?>
<form method="POST" enctype="multipart/form-data" name="image_upload_form" action="<?php $_SERVER['PHP_SELF']; ?>">

MY FORM HERE all variables are correct

</form>
<?php
if(is_array($error))
{
while(list($key, $val) = each($error))
{
echo $val;
echo "<br>\n";
}
}
?>

davidj
09-14-2007, 02:25 PM
did you write this or copy it??

gavimobile
09-14-2007, 03:10 PM
dj i did both, what ive been doing lately is reading the php manual, i take whats written in the exampleof the manual, i connect it to my database
than i make modifications and i add minor things for it to work with my colgouge

davidj
09-14-2007, 03:45 PM
here is an upload function i wrote

drop this in a file on its own in PHP tags


function upload_mi($fileName){
$target_path = "tmpfiles/"; //<< change this to suit where you want to put the uploaded file in your directory structure
$target_path = $target_path . basename($fileName);
if(move_uploaded_file($_FILES['file']['tmp_name'], $target_path)) {

return $message = "File uploaded successfully";

} else{

return $message = "There was an error uploading the file, please try again!";

}
}

here is the call in the script in top PHP of the file
remember to include the file above.

/// perform uploader ///////////////
////////////////////////////////////
if($_FILES['file']['name'] !=''){
$fileName = $_FILES['file']['name'];

$message = upload_mi($fileName);
}
////////////////////////////////////
////////////////////////////////////

and here is the html ( you will need to seperate these out into a table)

<form id="form1" name="form1" method="post" action="mi.php" enctype="multipart/form-data">
<input name="file" type="file" class="formfld1" id="file" />
<input type="hidden" name="MAX_FILE_SIZE" value="100000" />
<input name="button2" type="submit" class="formbutton2" id="button2" />
</form>

gavimobile
09-14-2007, 05:34 PM
dj ! ur code works great.
i believe its very similar to the script which i posted.
however my posted script has an aditional 6 file fields,
and a bunch of text fields AND when i clicked the submit
button, not only will the files be uploded, but all my information
was getting added in to my php/mysql database.

ok
so lets say i started from new and i choose to use ur script. if i wanted to make 2 upload fields now instead of 1. how would i go about this?
i also want this page to be inserting the file location into a mysql field.

its set as a varchar 255 char

here is the current script and dj, i followed ur directions without any question :-D

<?php require_once('../../Connections/connect.php');
include('include_file.php');
/*---------perform uploader---------*/
if($_FILES['file']['name'] !=''){
$fileName = $_FILES['file']['name'];
$message = upload_mi($fileName);
}
/*----------------------------------*/
?>
<html>
<head>
</head>
<body>
<form id="form1" name="form1" method="post" action="<?php $_SERVER['PHP_SELF']; ?>" enctype="multipart/form-data">
<table>
<tr>
<td>
<input name="file" type="file" class="formfld1" id="file" />
<input type="hidden" name="MAX_FILE_SIZE" value="100000" />
<input name="button2" type="submit" class="formbutton2" id="button2" />
</td>
</tr>
</table>
</form>
</body>
</html>

davidj
09-15-2007, 07:23 AM
so lets say i started from new and i choose to use ur script. if i wanted to make 2 upload fields now instead of 1. how would i go about this?

you want to upload more than 1 file at a time?


i also want this page to be inserting the file location into a mysql field.

Will the file location change every time?

gavimobile
09-15-2007, 08:44 AM
no dj the location will stay the same,

all pictures will be uploaded to a folder called images/

and yes i want more than 1 field. i want several fields

and when i clicksubmit, it will also save data in to mysql db

as well.

davidj
09-15-2007, 12:34 PM
ok

the include file...


<?php
function upload_mi($fileNames){
//<< change this to suit where you want to put the uploaded file in your directory structure
$target_path = "tmpfiles/";
// count array size
$count = count($fileNames);
/// loop through array ///
for($i=0; $i<$count; $i++){

// set key var to target array keys
$key = $i;
//
$files = $fileNames[$key];
// set target path
$path = $target_path.$files ;
// create var to target super global var files
$f = $i+1;

$fld = "file".$f;
// perform upload
if(move_uploaded_file($_FILES[$fld]['tmp_name'], $path)){

return $message = "Successful";
// add your $message var here
// add your Insert SQL here

}else{

return $message = "Failed";
// add your failed message here

};

}
?>



the main file (complete)


<?php
include('include_file.php');
/*---------perform uploader---------*/
if($_FILES['file1']['name'] !=''){

// add fields here if you want more
$field1 = $_FILES['file1']['name'];
$field2 = $_FILES['file2']['name'];
$field3 = $_FILES['file3']['name'];
///////////////////////////////////

/// add vars from above into this array if your adding fields
$fileNames = array($field1,$field2,$field3);

/// call function
$message = upload_mi($fileNames);
}
/*----------------------------------*/
?>
<html>
<head>
</head>
<body>
<form id="form1" name="form1" method="post" action="<?php $_SERVER['PHP_SELF']; ?>" enctype="multipart/form-data">
<table>
<tr>
<td>
<p>
<!-- remember to use the file-n naming convention when adding fields -->
<input name="file1" type="file" class="formfld1" id="file1" />
</p>
<p>
<!-- remember to use the file-n naming convention when adding fields -->
<input name="file2" type="file" class="formfld1" id="file2" />
</p>
<p>
<!-- remember to use the file-n naming convention when adding fields -->
<input name="file3" type="file" class="formfld1" id="file3" />
</p>
<p>
<input type="hidden" name="MAX_FILE_SIZE" value="100000" />
<input name="button2" type="submit" class="formbutton2" id="button2" />
<?php echo $message; ?>
</p></td>
</tr>
</table>
</form>
</body>
</html>

gavimobile
09-16-2007, 09:48 AM
dj i believe the include file was missing a } at the end.
also it seems that 1 of the fields only are uploading,
even thought there are 3 fields.
in monitoring my domain folders adn i notice after the submit 1
picture is geting added each time.

davidj
09-16-2007, 10:41 AM
ok here is the include

dont know what happened there


<?php
function upload_mi($fileNames){
//<< change this to suit where you want to put the uploaded file in your directory structure
$target_path = "tmpfiles/";
// count array size
$count = count($fileNames);

/// loop through array ///
for($i=0; $i<$count; $i++){


// set key var to target array keys
$key = $i;
//
$files = $fileNames[$key];
// set target path
$path = $target_path.$files ;
// create var to target super global var files
$f = $i+1;

$fld = "file".$f;

// perform upload
move_uploaded_file($_FILES[$fld]['tmp_name'], $path);


}
}
?>

gavimobile
09-16-2007, 12:48 PM
dj it works great!. thank you very much.

if i wanted this now to add the file location in myqsl db,

which name or id needs to be changed from this code.

does it even need to be changed?

for exmaple i have 6 'varchar 255' fields in mysql db
Picture1, Picture2, etc.

if this is the file field from the form what do i change to name or the id?
or are both unimportant?
<input name="file1" type="file" class="formfld1" id="Picture1" />

gavimobile
09-16-2007, 02:05 PM
also if it was possible, if u could show me a quick sample insert query that is included with this file field script.
i have a basic idea of how the insert query should look, but i have trouble intergrating it.. not sure where to put it.

davidj
09-16-2007, 03:45 PM
if i wanted this now to add the file location in myqsl db,

which name or id needs to be changed from this code.


the location doesnt change so why do you need to get it from the db?

also keep the name and id the same as i use the id to reference the field in my script...


<input name="file1" type="file" class="formfld1" id="Picture1" />


should be ....


<input name="file1" type="file" class="formfld1" id="file1" />

davidj
09-16-2007, 03:47 PM
also if it was possible, if u could show me a quick sample insert query that is included with this file field script.
i have a basic idea of how the insert query should look, but i have trouble intergrating it.. not sure where to put it.

check out my insert tutorials

that will show you how it works

(i knew id get you hand coding even if it was kicking and screaming)

gavimobile
09-16-2007, 08:29 PM
im currently working on the script, so just a quick q.

if we call the form filefields file1 and not Picture1
how will it know which insert field to save the file location in mysql
im not sure if i explained myself enough. i currently have a mysqldb
which i set aside 6 varchars of 255 char to save the file location.
the are named in the database Picture1, Picture2 etc.

i assume i need to change something im just not sure what?

thanks and im already learning!!! i cought that error of urs before i feel like the pro now!!!

gavimobile
09-16-2007, 08:38 PM
tell me if im right!
where it says file1 that stays, and where it says name thats where the
mysql field name goes eg Picture1
right?
/*---------perform uploader---------*/
if($_FILES['file1']['name'] !=''){

davidj
09-16-2007, 08:57 PM
no

thats wrong

forget picture1 use file1 instead

dont touch the $_FILES['file1']['name'] super global variable. name is a property of that variable. file1 is the name of the field and this must stay as it is

all you need to do is write your SQL so it inserts the data into the correct field

gavimobile
09-16-2007, 10:28 PM
dj this is where i passed out,

assuming the include_file.php is correct, now the form file i added a require
command to pull a seperate page which i called insertquery. the page looks like this. and it uploaded all the pictures to the directory, but it did NOT save the file location at all in the database.

<?php
require_once("Connections/connect.php");
$AgentID = $POST['AgentID'];
$City = $POST['City'];
$Neighborhood = $POST['Neighborhood'];
$Price = $POST['Price'];
$Bed = $POST['AgentID'];
$Bath = $POST['AgentID'];
$LivingSpace = $POST['AgentID'];
$OutdoorSpace = $POST['AgentID'];
$LotSize = $POST['AgentID'];
$Status = $POST['AgentID'];
$Type = $POST['Type'];
$Elevator = $POST['Elevator'];
$Parking = $POST['Parking'];
$Garden = $POST['Garden'];
$Terrace = $POST['Terrace'];
$Garage = $POST['Garage'];
$Description = $POST['Description'];
$file1 = $POST['Picture1'];
$file2 = $POST['Picture2'];
$file3 = $POST['Picture3'];
$file4 = $POST['Picture4'];
$file5 = $POST['Picture5'];
$file6 = $POST['Picture6'];
$submit = $POST[submit];
if ($AgentID && $City && $Neighborhood && $Price && $Bed && $Bath && $LivingSpace && $OutdoorSpace && $LotSize && $Status && $Type && $Elevator && $Parking && $Garden && $Terrace && $Garage && $Description && $file1 && $file2 && $file3 && $file4 && $file5 && $file6 && $submit){
$query = sprintf("INSERT into propertyadd (AgentID, City, Neighborhood, Price, Bed, Bath, LivingSpace, OutdoorSpace, LotSize, Status, Type, Elevator, Parking, Garden, Terrace, Garage, Description, Picture1, Picture2, Picture3, Picture4, Picture5, Picture6 )
values ('$AgentID', '$City', '$Neighborhood', '$Price', '$Bed', '$Bath', '$LivingSpace', '$OutdoorSpace', '$LotSize', '$Status', '$Type', '$Elevator', '$Parking', '$Garden', '$Terrace', '$Garage', '$Description', '$file1', '$file2', '$file3', '$file4', '$file5', '$file6' )");
mysql_query($query)or die(mysql_error());
} elseif($submit){
echo "one of your fields are empty!" ;
}
?>

davidj
09-16-2007, 10:35 PM
will look at this tomorrow

gavimobile
09-17-2007, 07:05 AM
here is the form file just incase.
:-)

<?php
include('include_file.php');
/*---------perform uploader---------*/
if($_FILES['file1']['name'] !=''){
// add fields here if you want more
$field1 = $_FILES['file1']['name'];
$field2 = $_FILES['file2']['name'];
$field3 = $_FILES['file3']['name'];
$field4 = $_FILES['file4']['name'];
$field5 = $_FILES['file5']['name'];
$field6 = $_FILES['file6']['name'];
///////////////////////////////////
/// add vars from above into this array if your adding fields
$fileNames = array($field1,$field2,$field3,$field4,$field5,$fie ld6);

/// call function
$message = upload_mi($fileNames);
}
include('insertquery.php');
?>

<html>
<head>
</head>
<body>
<form id="form1" name="form1" method="post" action="<?php $_SERVER['PHP_SELF']; ?>" enctype="multipart/form-data">

<table>
<tr>
<td>
<input name="file1" type="file" class="formfld1" id="file1" />
<input name="file2" type="file" class="formfld1" id="file2" />
<input name="file3" type="file" class="formfld1" id="file3" />
<input name="file4" type="file" class="formfld1" id="file4" />
<input name="file5" type="file" class="formfld1" id="file5" />
<input name="file6" type="file" class="formfld1" id="file6" />
<input type="hidden" name="MAX_FILE_SIZE" value="100000" />
<input name="button2" type="submit" class="formbutton2" id="button2" />

<?php echo $message; ?>

</td>
</tr>
</table>
</form>
</body>
</html>

gavimobile
09-17-2007, 07:20 AM
found sounthing,
i wrote the variable in the query as $file1 and not $field1 here is the new query i have. code still uploads but doesnt save text in mysql.
waiting eagerly for ur reply.



<?php
require_once("Connections/connect.php");
$AgentID = $POST['AgentID'];
$City = $POST['City'];
$Neighborhood = $POST['Neighborhood'];
$Price = $POST['Price'];
$Bed = $POST['AgentID'];
$Bath = $POST['AgentID'];
$LivingSpace = $POST['AgentID'];
$OutdoorSpace = $POST['AgentID'];
$LotSize = $POST['AgentID'];
$Status = $POST['AgentID'];
$Type = $POST['Type'];
$Elevator = $POST['Elevator'];
$Parking = $POST['Parking'];
$Garden = $POST['Garden'];
$Terrace = $POST['Terrace'];
$Garage = $POST['Garage'];
$Description = $POST['Description'];
$field1 = $POST['Picture1'];
$field2 = $POST['Picture2'];
$field3 = $POST['Picture3'];
$field4 = $POST['Picture4'];
$field5 = $POST['Picture5'];
$field6 = $POST['Picture6'];
$submit = $POST[submit];
if (
$AgentID &&
$City &&
$Neighborhood &&
$Price &&
$Bed &&
$Bath &&
$LivingSpace &&
$OutdoorSpace &&
$LotSize &&
$Status &&
$Type &&
$Elevator &&
$Parking &&
$Garden &&
$Terrace &&
$Garage &&
$Description &&
$field1 &&
$field2 &&
$field3 &&
$field4 &&
$field5 &&
$field6 &&
$submit){
$query = sprintf("INSERT into propertyadd (
AgentID ,
City ,
Neighborhood ,
Price ,
Bed ,
Bath ,
LivingSpace ,
OutdoorSpace ,
LotSize ,
Status ,
Type ,
Elevator ,
Parking ,
Garden ,
Terrace ,
Garage ,
Description ,
Picture1 ,
Picture2 ,
Picture3 ,
Picture4 ,
Picture5 ,
Picture6 )
values ('
$AgentID' ,
'$City' ,
'$Neighborhood' ,
'$Price' ,
'$Bed' ,
'$Bath' ,
'$LivingSpace' ,
'$OutdoorSpace' ,
'$LotSize' ,
'$Status' ,
'$Type' ,
'$Elevator' ,
'$Parking' ,
'$Garden' ,
'$Terrace' ,
'$Garage' ,
'$Description' ,
'$field1' ,
'$field2' ,
'$field3' ,
'$field4' ,
'$field5' ,
'$field6' )");
mysql_query($query)or die(mysql_error());
}elseif($submit){
echo "one of your fields are empty!" ;
}
?>

davidj
09-17-2007, 07:21 AM
<?php
function upload_mi($fileNames){
//<< change this to suit where you want to put the uploaded file in your directory structure
$target_path = "tmpfiles/";
// count array size
$count = count($fileNames);

/// loop through array ///
for($i=0; $i<$count; $i++){


// set key var to target array keys
$key = $i;
//
$files = $fileNames[$key];
// set target path
$path = $target_path.$files ;
// create var to target super global var files
$f = $i+1;

$fld = "file".$f;

// perform upload
move_uploaded_file($_FILES[$fld]['tmp_name'], $path);

/// add SQL here (INSERT SCRIPT) ////////////////

}
}
?>

gavimobile
09-17-2007, 12:29 PM
dj,

here is the include file.
it still is uploading all 6 pictures but it doesnt save the file location to my sql
a hint at least or what can i take out that doesnt belong, what am i missing that i forgot to add!!! i can feeel im soo close!

<?php
require_once("Connections/connect.php");
function upload_mi($fileNames){
//<< change this to suit where you want to put the uploaded file in your directory structure
$target_path = "Images/design/images/";
// count array size
$count = count($fileNames);
/// loop through array ///
for($i=0; $i<$count; $i++){


// set key var to target array keys
$key = $i;
//
$files = $fileNames[$key];
// set target path
$path = $target_path.$files ;
// create var to target super global var files
$f = $i+1;

$fld = "file".$f;

// perform upload
move_uploaded_file($_FILES[$fld]['tmp_name'], $path);

$AgentID = $POST['AgentID'];
$City = $POST['City'];
$Neighborhood = $POST['Neighborhood'];
$Price = $POST['Price'];
$Bed = $POST['AgentID'];
$Bath = $POST['AgentID'];
$LivingSpace = $POST['AgentID'];
$OutdoorSpace = $POST['AgentID'];
$LotSize = $POST['AgentID'];
$Status = $POST['AgentID'];
$Type = $POST['Type'];
$Elevator = $POST['Elevator'];
$Parking = $POST['Parking'];
$Garden = $POST['Garden'];
$Terrace = $POST['Terrace'];
$Garage = $POST['Garage'];
$Description = $POST['Description'];
$field1 = $POST['Picture1'];
$field2 = $POST['Picture2'];
$field3 = $POST['Picture3'];
$field4 = $POST['Picture4'];
$field5 = $POST['Picture5'];
$field6 = $POST['Picture6'];
$submit = $POST[submit];
if (
$AgentID &&
$City &&
$Neighborhood &&
$Price &&
$Bed &&
$Bath &&
$LivingSpace &&
$OutdoorSpace &&
$LotSize &&
$Status &&
$Type &&
$Elevator &&
$Parking &&
$Garden &&
$Terrace &&
$Garage &&
$Description &&
$field1 &&
$field2 &&
$field3 &&
$field4 &&
$field5 &&
$field6 &&
$submit){
$query = sprintf("INSERT into propertyadd (
AgentID ,
City ,
Neighborhood ,
Price ,
Bed ,
Bath ,
LivingSpace ,
OutdoorSpace ,
LotSize ,
Status ,
Type ,
Elevator ,
Parking ,
Garden ,
Terrace ,
Garage ,
Description ,
Picture1 ,
Picture2 ,
Picture3 ,
Picture4 ,
Picture5 ,
Picture6 )
values ('
$AgentID' ,
'$City' ,
'$Neighborhood' ,
'$Price' ,
'$Bed' ,
'$Bath' ,
'$LivingSpace' ,
'$OutdoorSpace' ,
'$LotSize' ,
'$Status' ,
'$Type' ,
'$Elevator' ,
'$Parking' ,
'$Garden' ,
'$Terrace' ,
'$Garage' ,
'$Description' ,
'$field1' ,
'$field2' ,
'$field3' ,
'$field4' ,
'$field5' ,
'$field6' )");
mysql_query($query)or die(mysql_error());
}elseif($submit){
echo "one of your fields are empty!" ;
}


}
}
?>

gavimobile
09-17-2007, 05:30 PM
dj, same thing, files are uploaded but mysql doesnt insert anything.



<?php
require_once("Connections/connect.php");
function upload_mi($fileNames){
//<< change this to suit where you want to put the uploaded file in your directory structure
$target_path = "Images/design/images/";
// count array size
$count = count($fileNames);
/// loop through array ///
for($i=0; $i<$count; $i++){


// set key var to target array keys
$key = $i;
//
$files = $fileNames[$key];
// set target path
$path = $target_path.$files ;
// create var to target super global var files
$f = $i+1;

$fld = "file".$f;

// perform upload
move_uploaded_file($_FILES[$fld]['tmp_name'], $path);
move_uploaded_file($_FILES[$fld]['tmp_name'], $path);

$AgentID = $POST['AgentID'];
$City = $POST['City'];
$Neighborhood = $POST['Neighborhood'];
$Price = $POST['Price'];
$Bed = $POST['AgentID'];
$Bath = $POST['AgentID'];
$LivingSpace = $POST['AgentID'];
$OutdoorSpace = $POST['AgentID'];
$LotSize = $POST['AgentID'];
$Status = $POST['AgentID'];
$Type = $POST['Type'];
$Elevator = $POST['Elevator'];
$Parking = $POST['Parking'];
$Garden = $POST['Garden'];
$Terrace = $POST['Terrace'];
$Garage = $POST['Garage'];
$Description = $POST['Description'];
$field1 = $POST['Picture1'];
$field2 = $POST['Picture2'];
$field3 = $POST['Picture3'];
$field4 = $POST['Picture4'];
$field5 = $POST['Picture5'];
$field6 = $POST['Picture6'];
$submit = $POST[submit];
if (
$AgentID &&
$City &&
$Neighborhood &&
$Price &&
$Bed &&
$Bath &&
$LivingSpace &&
$OutdoorSpace &&
$LotSize &&
$Status &&
$Type &&
$Elevator &&
$Parking &&
$Garden &&
$Terrace &&
$Garage &&
$Description &&
$field1 &&
$field2 &&
$field3 &&
$field4 &&
$field5 &&
$field6 &&
$submit){
$query = sprintf("INSERT into propertyadd (
AgentID ,
City ,
Neighborhood ,
Price ,
Bed ,
Bath ,
LivingSpace ,
OutdoorSpace ,
LotSize ,
Status ,
Type ,
Elevator ,
Parking ,
Garden ,
Terrace ,
Garage ,
Description ,
Picture1 ,
Picture2 ,
Picture3 ,
Picture4 ,
Picture5 ,
Picture6 )
values ('
$AgentID' ,
'$City' ,
'$Neighborhood' ,
'$Price' ,
'$Bed' ,
'$Bath' ,
'$LivingSpace' ,
'$OutdoorSpace' ,
'$LotSize' ,
'$Status' ,
'$Type' ,
'$Elevator' ,
'$Parking' ,
'$Garden' ,
'$Terrace' ,
'$Garage' ,
'$Description' ,
'$field1' ,
'$field2' ,
'$field3' ,
'$field4' ,
'$field5' ,
'$field6' )");
mysql_query($query)or die(mysql_error());
}elseif($submit){
echo "one of your fields are empty!" ;
}

}
}
?>

gavimobile
09-17-2007, 07:51 PM
oops i thought u posted . i just realized that i did 2 replies

davidj
09-17-2007, 08:23 PM
ok

you have 1 row per record in your db

you want to fill in just the 1 row after the upload? which includes the image names

gavimobile
09-17-2007, 08:58 PM
i am trying to complete 2 things in 1 script.
upload files, and save file location "C:/picture.jpg" in my mysql
fields which i set aside for saving file locations.

davidj
09-18-2007, 05:41 AM
i need to see a structure of you db

gavimobile
09-18-2007, 06:02 AM
CREATE TABLE `propertyadd` (
`Id` int(11) NOT NULL auto_increment,
`AgentID` int(11) default NULL,
`City` varchar(25) default NULL,
`Neighborhood` varchar(25) default NULL,
`Price` decimal(12,0) default NULL,
`Bed` decimal(2,1) default NULL,
`Bath` decimal(2,1) default NULL,
`LivingSpace` int(11) default NULL,
`OutdoorSpace` int(11) default NULL,
`LotSize` int(11) default NULL,
`Status` varchar(11) default NULL,
`Type` varchar(25) default NULL,
`Elevator` varchar(25) default NULL,
`Parking` varchar(25) default NULL,
`Garden` varchar(11) default NULL,
`Terrace` varchar(11) default NULL,
`Garage` varchar(11) default NULL,
`Description` text,
`Picture1` varchar(255) default NULL,
`Picture2` varchar(255) default NULL,
`Picture3` varchar(255) default NULL,
`Picture4` varchar(255) default NULL,
`Picture5` varchar(255) default NULL,
`Picture6` varchar(255) default NULL,
PRIMARY KEY (`Id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=175 ;

is this what u need?

davidj
09-18-2007, 10:47 AM
ok
i see what you want

are you validating against all fields or can someone just upload
image1.jpg, image2.jpg, none, none,none,none

or is this valid

image1.jpg, none, image3.jpg, none,none,image6.jpg

gavimobile
09-18-2007, 01:12 PM
well here is my plan.

the fields DO NOT need to be validated.
however,
if none of the file fields were filled in, than
can Picture1, Picture2, Picture3 etc. echo a default picture which i manually added to the $target_path = "Images/design/images/"; directory.
eg. default_Pic.jpg

davidj
09-18-2007, 07:22 PM
we need to chat about this as its a bit complicated


<?php
function upload_mi($fileNames){
//<< change this to suit where you want to put the uploaded file in your directory structure
$target_path = "tmpfiles/";
// count array size
$count = count($fileNames);

/// loop through array ///
for($i=0; $i<$count; $i++){


// set key var to target array keys
$key = $i;
//
$files = $fileNames[$key];
// set target path
$path = $target_path.$files ;
// create var to target super global var files
$f = $i+1;

$fld = "file".$f;

// perform upload
move_uploaded_file($_FILES[$fld]['tmp_name'], $path);

/// insert once in the loop
if($i < 2){

$query = "INSERT INTO TABLE (field, field, field, field, picture1)Values('$value','$value','$value','$value ','$files')";
mysql_query($query) or die('Error, insert query failed');

$lastID = mysql_insert_id();

}

// now just update the record each time it loops
if($i > 1){

$query = sprintf("UPDATE TABLE SET picture%s = '%s' where id = '%s'", $i, $files[$i], $lastID);
mysql_query($query) or die('Error, update query failed');

}
}
}
?>

gavimobile
09-18-2007, 09:45 PM
Error, insert query failed....

dj before i returned to the form my code was working... sortof, it was uploading the pictures but giving the pics weird names in mysql. also it was inserting records everytime i hit refresh.

whatever

anyways. lets take this 1 step back. what would be done to the include_file,
if i didnt want ANY validations?

btw i would love to hear ur explination on the previous file, doesnt seem to work though. its uploading to the same directory that it was previously no changes were made other than the include_file.php

davidj
09-19-2007, 05:49 AM
post the code you have from the include

gavimobile
09-19-2007, 08:48 AM
this is what i got

<?php
require_once("Connections/connect.php");
function upload_mi($fileNames){
//<< change this to suit where you want to put the uploaded file in your directory structure
$target_path = "Images/design/images/";
// count array size
$count = count($fileNames);

/// loop through array ///
for($i=0; $i<$count; $i++){


// set key var to target array keys
$key = $i;
//
$files = $fileNames[$key];
// set target path
$path = $target_path.$files ;
// create var to target super global var files
$f = $i+1;

$fld = "file".$f;

// perform upload
move_uploaded_file($_FILES[$fld]['tmp_name'], $path);

/// insert once in the loop
if($i < 2){
$query = sprintf("INSERT into propertyadd (
AgentID ,
City ,
Neighborhood ,
Price ,
Bed ,
Bath ,
LivingSpace ,
OutdoorSpace ,
LotSize ,
Status ,
Type ,
Elevator ,
Parking ,
Garden ,
Terrace ,
Garage ,
Description ,
Picture1 ,
Picture2 ,
Picture3 ,
Picture4 ,
Picture5 ,
Picture6 )
values ('
$AgentID' ,
'$City' ,
'$Neighborhood' ,
'$Price' ,
'$Bed' ,
'$Bath' ,
'$LivingSpace' ,
'$OutdoorSpace' ,
'$LotSize' ,
'$Status' ,
'$Type' ,
'$Elevator' ,
'$Parking' ,
'$Garden' ,
'$Terrace' ,
'$Garage' ,
'$Description' ,
'$field1' ,
'$field2' ,
'$field3' ,
'$field4' ,
'$field5' ,
'$field6' )");
mysql_query($query) or die('Error, insert query failed');

$lastID = mysql_insert_id();

}

// now just update the record each time it loops
if($i > 1){

$query = sprintf("UPDATE TABLE SET picture%s = '%s' where id = '%s'", $i, $files[$i], $lastID);
mysql_query($query) or die('Error, update query failed');

}
}
}
?>

davidj
09-19-2007, 09:41 AM
will have to build an example of your environment

this will have to wait until the weekend

gavimobile
09-19-2007, 09:54 AM
thanks dj for all ur help,


patiently waiting :-)

gavimobile
09-21-2007, 03:42 PM
dj this was just a friendly reminder. thanks again for all your help, i do realize its from your own good and nothing else :-D have a gr8 weekend

gavimobile
10-13-2007, 11:50 PM
dj, ive accidently put together something which works.
if its possible, could you just show me how to make this into multiple fields.
right now there is only 1 picture field. it uploads both to the database and to the folder i set aside for image uploading. so if i wanted to do this to multiple fields. what changes would be made?

<?php
include('../Connections/connect.php');
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("baronre_baron", $con);
$uid =$_POST['uid'];
$pwd =$_POST['pwd'];
$fname =$_POST['fname'];
$lname =$_POST['lname'];
$email =$_POST['email'];
$phone =$_POST['phone'];
$type =$_POST['type'];
$Photo =$_FILES['Photo']['name'];
$error =$_FILES['Photo']['error'];
$size =$_FILES['Photo']['size'];
$Newsletter =$_POST['Newsletter'];

if ($error)
{
echo "Return Code: " . $error . "<br />";
}
else
{
echo "file has been uploaded sucessfully,<br>To add a new picture, <a href='index.php'>Click Here</a><br><br>";


if (file_exists("../Images/design/images/" . $Photo))
{
echo $Photo . " already exists. ";
}
else
{
mysql_query("INSERT INTO users (uid, pwd, fname, lname, email, phone, type, Photo, Newsletter) VALUES (
'$uid',
'$pwd',
'$fname',
'$lname',
'$email',
'$phone',
'$type',
'$Photo',
'$Newsletter')");
move_uploaded_file($_FILES["Photo"]["tmp_name"],

"../Images/design/images/" . $Photo);
}
}

mysql_close($con)
?>

davidj
10-14-2007, 11:06 AM
just add additions to these


$Photo =$_FILES['Photo']['name'];
$error =$_FILES['Photo']['error'];
$size =$_FILES['Photo']['size'];




$Photo =$_FILES['Photo']['name'];
$error =$_FILES['Photo']['error'];
$size =$_FILES['Photo']['size'];

$Photo2 =$_FILES['Photo2']['name'];
$error2 =$_FILES['Photo2']['error'];
$size2 =$_FILES['Photo2']['size'];


then add the vars to the database insert statement

you would have to check which vars have values first

gavimobile
10-14-2007, 03:26 PM
david thanks for the quick reply. i wasnt sure if i was suppose to do something with the errors in the else, if part for examplenow it looks like this

if ($error1)
{
echo "Return Code: " . $error1 . "<br />";
}
else

and i thought i needed to add another one further in the page somewhere like this

if ($error2)
{
echo "Return Code: " . $error2 . "<br />";
}
else

anyways the code worked after several atempts. for thoese who want. dj thanks for the help :-)

<?php
include('../Connections/connect.php');
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("baronre_baron", $con);
$AgentID =$_POST['AgentID'];
$ListType =$_POST['ListType'];
$City =$_POST['City'];
$Neighborhood =$_POST['Neighborhood'];
$Price =$_POST['Price'];
$Bed =$_POST['Bed'];
$Bath =$_POST['Bath'];
$LivingSpace =$_POST['LivingSpace'];
$OutdoorSpace =$_POST['OutdoorSpace'];
$LotSize =$_POST['LotSize'];
$Status =$_POST['Status'];
$Type =$_POST['Type'];
$Elevator =$_POST['Elevator'];
$Parking =$_POST['Parking'];
$Garden =$_POST['Garden'];
$Terrace =$_POST['Terrace'];
$Garage =$_POST['Garage'];
$Description =$_POST['Description'];
$Picture1 =$_FILES['Picture1']['name'];
$error =$_FILES['Picture1']['error'];
$size =$_FILES['Picture1']['size'];
$Picture2 =$_FILES['Picture2']['name'];
$error2 =$_FILES['Picture2']['error'];
$size2 =$_FILES['Picture2']['size'];
$Picture3 =$_FILES['Picture3']['name'];
$error3 =$_FILES['Picture3']['error'];
$size3 =$_FILES['Picture3']['size'];
$Picture4 =$_FILES['Picture4']['name'];
$error4 =$_FILES['Picture4']['error'];
$size4 =$_FILES['Picture4']['size'];
$Picture5 =$_FILES['Picture5']['name'];
$error5 =$_FILES['Picture5']['error'];
$size5 =$_FILES['Picture5']['size'];
$Picture6 =$_FILES['Picture6']['name'];
$error6 =$_FILES['Picture6']['error'];
$size6 =$_FILES['Picture6']['size'];
if ($error)
{
echo "Return Code: " . $error . "<br />";
}
else
{
echo "file has been uploaded sucessfully,<br>To add a new picture, <a href='index.php'>Click Here</a><br><br>";
if (file_exists("../Images/design/images/" . $Picture1))
{
echo $Picture1 . " already exists. ";
}
else
{
mysql_query("INSERT INTO propertyadd (AgentID, ListType, City, Neighborhood, Price, Bed, Bath, LivingSpace, OutdoorSpace, LotSize, Status, Type, Elevator, Parking, Garden, Terrace, Garage, Description, Picture1, Picture2, Picture3, Picture4, Picture5, Picture6) VALUES ('$AgentID', '$ListType', '$City', '$Neighborhood', '$Price', '$Bed', '$Bath', '$LivingSpace', '$OutdoorSpace', '$LotSize', '$Status', '$Type', '$Elevator', '$Parking', '$Garden', '$Terrace', '$Garage', '$Description', '$Picture1', '$Picture2', '$Picture3', '$Picture4', '$Picture5', '$Picture6')");
move_uploaded_file($_FILES["Picture1"]["tmp_name"],
"../Images/design/images/" . $Picture1);
}
}

mysql_close($con)
?>

gavimobile
10-14-2007, 05:36 PM
yes dj, i was right. the script is saving all the information now, but only 1 picture is being uploaded. how do i make this script work for more than 1 field?

if ($error)
{
echo "Return Code: " . $error . "<br />";
}
else
{
echo "file has been uploaded sucessfully,<br>To add a new picture, <a href='index.php'>Click Here</a><br><br>";
if (file_exists("../Images/design/images/" . $Picture1))
{
echo $Picture1 . " already exists. ";
}
else
{
mysql_query("INSERT INTO propertyadd (AgentID, ListType, City, Neighborhood, Price, Bed, Bath, LivingSpace, OutdoorSpace, LotSize, Status, Type, Elevator, Parking, Garden, Terrace, Garage, Description, Picture1, Picture2, Picture3, Picture4, Picture5, Picture6) VALUES ('$AgentID', '$ListType', '$City', '$Neighborhood', '$Price', '$Bed', '$Bath', '$LivingSpace', '$OutdoorSpace', '$LotSize', '$Status', '$Type', '$Elevator', '$Parking', '$Garden', '$Terrace', '$Garage', '$Description', '$Picture1', '$Picture2', '$Picture3', '$Picture4', '$Picture5', '$Picture6')");
move_uploaded_file($_FILES["Picture1"]["tmp_name"],
"../Images/design/images/" . $Picture1);
}
}

davidj
10-14-2007, 06:02 PM
first stop echo-ing vars out within your php

best practise is to assign messages and errors to a var and echo the var inside the HTML some place

To upload multi images at the same time you will have to create a function which loops. This needs thought though.


are you using an auto increment id