/    Sign up×
Community /Pin to ProfileBookmark

php add two files

Hi

The below code loads a file to a folder and stores the file name in a mysql database. How would I change it so that I can add two files at the same time. I have to file fields on the form.

thank you for any help

[code=php]
if (isset($_FILES[‘file’])) {

$path = ‘../adImages/’;

if (is_uploaded_file($_FILES[‘file’][‘tmp_name’])) {

if ($_FILES[‘file’][‘size’]>$max_file_size) {

$msg = “Image Exceeds maximum file size”;

header(“location: $url?msg=$msg”);
exit;

}

if (($_FILES[‘file’][‘type’]==”image/gif”) || ($_FILES[‘file’][‘type’]==”image/pjpeg”) || ($_FILES[‘file’][‘type’]==”image/jpeg”) || ($_FILES[‘file’][‘type’]==”image/png”)) {

if (file_exists($path . $_FILES[‘file’][‘name’])) {

$msg .= “The file already exists, change your file name”;

header(“location: $url?msg=$msg”);
exit;

}

$res = copy($HTTP_POST_FILES[‘file’][‘tmp_name’], $path.$user_name.”-“.$HTTP_POST_FILES[‘file’][‘name’]);

if (!$res) {

$msg .= “upload failed!”;

header(“location: $url?msg=$msg”);

exit;

}

} else {

header(“location: $url?msg=$msg”);

$msg .=”Wrong file type, Must be jpeg, gif or png file types”; exit; }

$image = $user_name.”-“.$HTTP_POST_FILES[‘file’][‘name’];

}
[/code]

to post a comment
PHP

5 Comments(s)

Copy linkTweet thisAlerts:
@NightShift58Mar 12.2007 — Assuming the post variable name is defined as an array in the upload form (and it should be), you will need to cycle through the $_FILES array.[code=php]FOREACH ($_FILES['uploadFile']['tmp_name'] as $i => $val) :
... etc ...
ENDFOREACH;[/code]
There was an almost identical post yesterday that you may want to read:

http://www.webdeveloper.com/forum/showpost.php?p=726231&postcount=10
Copy linkTweet thisAlerts:
@kprocauthorMar 12.2007 — This is what I have this far, its not working and gives this error message


Parse error: parse error, unexpected $end in C:Program FilesxampphtdocsetreasuresportaladvertisementRequest.php on line 194

Once I get this o work I run into the problem how to send each unique file name to the mysql database
[code=php]
if (isset($_FILES['uploadFile'])) {

$path = '../advertiseCenter/rawImages/';
foreach ($_FILES['uploadFile']['tmp_name'] as $i => $val) :
if (is_uploaded_file($_FILES['uploadFile']['tmp_name'])) {

if ($_FILES['uploadFile']['size']>$max_file_size) {

$msg = "Image Exceeds maximum file size";

header("location: $url?msg=$msg");
exit();

}

if (($_FILES['uploadFile']['type']=="image/gif") || ($_FILES['uploadFile']['type']=="image/pjpeg") || ($_FILES['uploadFile']['type']=="image/jpeg") || ($_FILES['uploadFile']['type']=="image/png")) {

if (file_exists($path.$banner_userName . $_FILES['uploadFile']['name'])) {


$msg .= "The file already exists, change your file name";

header("location: $url?msg=$msg");
exit();

}

$res = copy($HTTP_POST_FILES['uploadFile']['tmp_name'], $path.$banner_userName."-".$HTTP_POST_FILES['uploadFile']['name']);

if (!$res) {

$msg .= "upload failed!";

header("location: $url?msg=$msg");

exit();

}

} else {

header("location: $url?msg=$msg");

$msg .="Wrong file type, Must be jpeg, gif or png file types"; exit(); }

$image = $banner_userName."-".$HTTP_POST_FILES['uploadFile']['name'];

}
endforeach;
/////////////////////////////////

$query_send_banner_request = ("INSERT INTO banner_requests (company, phone, contact, email, address, website, message, image1, image2, create_date)VALUES('$company','$phone','$contact', '$email', '$address', '$website','$message','$image1', '$image2', now())");

$send_banner_request = mysql_query($query_send_banner_request)or die("SQL Error: $query_send_banner_request<br>" . mysql_error());

}
?>
[/code]
Copy linkTweet thisAlerts:
@kprocauthorMar 12.2007 — this is the complete code for the page. I fixed the error message that I posted earlier, but the files are not uploading and not sure how to handle the unique file names
[code=php]
<?php
/* Check User Script */
session_start(); // Start Session
include ("../Connections/db.php");
//check if user is already logged in
/*if (!isset($_SESSION['user_id'])){

$msg = 'You tryed to access a members only page. Login or become a registered member to access that page!';

header("Location: ../index.php?msg=".$msg);
}*/
// make a note of the current working directory, relative to root.
$directory_self = str_replace(basename($_SERVER['PHP_SELF']), '', $_SERVER['PHP_SELF']);

$url = 'http://' . $_SERVER['HTTP_HOST'] . $directory_self . 'advertisementRequest.php';
///////////////////////////////

$max_file_size = 12500000;
$banner_userName = 'proctk';

if(isset($_POST['Submit'])){
$company = $_POST['company'];
$phone = $_POST['phone'];
$contact = $_POST['contact'];
$email = $_POST['email'];
$address = $_POST['address'];
$website = $_POST['website'];
$message = $_POST['message'];

//////////////////////////

/*FOREACH ($_FILES['uploadFile']['tmp_name'] as $i => $val) :
IF ( move_uploaded_file ($_FILES['uploadFile']['tmp_name'][$i],
"../advertiseCenter/rawImages/{$_FILES['uploadFile']['name'][$i]}") ) :
print '<p> The file has been successfully uploaded </p>';
ELSE :
IF ($_FILES['uploadFile']['error'][$i] == 1) :
print '<p> The file is bigger than this PHP installation allows</p>';
ELSEIF ($_FILES['uploadFile']['error'][$i] == 2) :
print '<p> The file is bigger than this form allows</p>';
ELSEIF ($_FILES['uploadFile']['error'][$i] == 3) :
print '<p> Only part of the file was uploaded</p>';
ELSEIF ($_FILES['uploadFile']['error'][$i] == 4) :
print '<p> No file was uploaded</p>';
ELSE :
print '<p> Something else went wrong message</p>';
ENDIF;
ENDIF;
ENDFOREACH;*/
///////////////

/////////////////////////////////
if (isset($_FILES['uploadFile'])) {

$path = '../advertiseCenter/rawImages/';
foreach ($_FILES['uploadFile']['tmp_name'] as $i => $val) :
if (is_uploaded_file($_FILES['uploadFile']['tmp_name'])) {

if ($_FILES['uploadFile']['size']>$max_file_size) {

$msg = "Image Exceeds maximum file size";

header("location: $url?msg=$msg");
exit();

}

if (($_FILES['uploadFile']['type']=="image/gif") || ($_FILES['uploadFile']['type']=="image/pjpeg") || ($_FILES['uploadFile']['type']=="image/jpeg") || ($_FILES['uploadFile']['type']=="image/png")) {

if (file_exists($path.$banner_userName . $_FILES['uploadFile']['name'])) {


$msg .= "The file already exists, change your file name";

header("location: $url?msg=$msg");
exit();

}

$res = copy($HTTP_POST_FILES['uploadFile']['tmp_name'], $path.$banner_userName."-".$HTTP_POST_FILES['uploadFile']['name']);

if (!$res) {

$msg .= "upload failed!";

header("location: $url?msg=$msg");

exit();

}

} else {

header("location: $url?msg=$msg");

$msg .="Wrong file type, Must be jpeg, gif or png file types"; exit();

}

$image = $banner_userName."-".$HTTP_POST_FILES['uploadFile']['name'];

}

endforeach;
/////////////////////////////////

$query_send_banner_request = ("INSERT INTO banner_requests (company, phone, contact, email, address, website, message, image1, image2, create_date)VALUES('$company','$phone','$contact', '$email', '$address', '$website','$message','$image1', '$image2', now())");

$send_banner_request = mysql_query($query_send_banner_request)or die("SQL Error: $query_send_banner_request<br>" . mysql_error());

}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Etreasures - Feedback Form</title>

<link href="../design/default.css" rel="stylesheet" type="text/css" />


</head>

<body>

<div id="outer">
<?php include('../design/banner.php'); ?>
<div id="sub">
<div id="center">
<form name="Send_banner" method="post" enctype="multipart/form-data" action="<?php echo $PHP_SELF; ?>" >
<input type="hidden" name="MAX_FILE_SIZE" value="52000" />
<table class="forms">
<tr>
<td colspan="2" class="header">Submit Advertisment Details </td>
</tr>
<tr>
<td class="left"><div align="right">Company Name </div></td>
<td class="rightTwoColumn"><input name="company" type="text" id="company" /></td>
</tr>
<tr>
<td class="left"><div align="right">Phone Number </div></td>
<td class="rightTwoColumn"><input name="phone" type="text" id="phone" /></td>
</tr>
<tr>
<td class="left"><div align="right">Contact</div></td>
<td class="rightTwoColumn"><input name="contact" type="text" id="contact" /></td>
</tr>
<tr>
<td class="left"><div align="right">Email Address </div></td>
<td class="rightTwoColumn"><input name="email" type="text" id="email" /></td>
</tr>
<tr>
<td class="left"><div align="right">Address</div></td>
<td class="rightTwoColumn"><input name="address" type="text" id="address" /></td>
</tr>
<tr>
<td class="left"><div align="right">Website</div></td>
<td class="rightTwoColumn"><input name="website" type="text" id="website" /></td>
</tr>

<tr>
<td class="left"><input name="uploadFile[]" type="file" id="file" size="12" /></td>
<td class="center"><input name="uploadFile[]" type="file" class="rightTwoColumn" size="12" /></td>
</tr>
<tr>
<td style="text-align:center; padding:.2em" colspan="2" class="treasureborder"><textarea name="message" cols="40" rows="10" id="message"></textarea></td>
</tr>
<tr>
<td><input type="submit" name="Submit" value="Submit" /></td>
</tr>
</table>
</form>
</div>
<div id="left">

<?php include('../design/leftlinks.php'); ?>

</div>
</div>
<div id="right">
<div style="text-align:center">

</div>
</div>
<div id="footer">
<?php include('../design/footer.php'); ?>
</div>
</div>

</div>





</body>
</html>

[/code]
Copy linkTweet thisAlerts:
@kprocauthorMar 12.2007 — I got it to work but cannot figure out how to deal with the two file names that need to be added to the mysql table

////////

This solved the problem

$image1 = $banner_userName."-".$HTTP_POST_FILES['uploadFile']['name'][0];

$image2 = $banner_userName."-".$HTTP_POST_FILES['uploadFile']['name'][1];
Copy linkTweet thisAlerts:
@NightShift58Mar 12.2007 — Prior to executing the SQL statement:[code=php]$image1 = $path . $banner_userName . "-" . $_FILES['uploadFile']['name'][0];
$image2 = $path . $banner_userName . "-" . $_FILES['uploadFile']['name'][1];[/code]
×

Success!

Help @kproc spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.21,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...