 |
|

06-27-2006, 08:04 AM
|
 |
SEO GUY Moderator
|
|
Join Date: May 2004
Location: In perpetual style
Posts: 3,305
|
|
|
php upload form + remove spaces
I have a form here: http://www.wildfiremarketinggroup.com/form1.html
And have added a bit of code that I thought was correct (line 3) to replace spaces with and underscore because if someone uploaded a file with a space in the name, the link to it on the server got broken. In any case, it doesn't work.
Here is my php:
Code:
<?php
include("global.inc.php");
$temp = str_replace(" "_"", $temp);
echo $temp;
$errors=0;
$error="The following errors occured while processing your form input.<ul>";
pt_register('POST','Contactname');
pt_register('POST','Phonenumber');
pt_register('POST','Emailaddress');
$File1=$HTTP_POST_FILES['File1'];
$File2=$HTTP_POST_FILES['File2'];
$File3=$HTTP_POST_FILES['File3'];
$File4=$HTTP_POST_FILES['File4'];
$File5=$HTTP_POST_FILES['File5'];
$File6=$HTTP_POST_FILES['File6'];
$Comments=$HTTP_POST_FILES['Comments'];
if($Contactname=="" || $Phonenumber=="" || $Emailaddress=="" ){
$errors=1;
$error.="<li>You did not enter one or more of the required fields. Please go back and try again.";
}
if($HTTP_POST_FILES['File1']['tmp_name']==""){ }
else if(!is_uploaded_file($HTTP_POST_FILES['File1']['tmp_name'])){
$error.="<li>The file, ".$HTTP_POST_FILES['File1']['name'].", was not uploaded!";
$errors=1;
}
if($HTTP_POST_FILES['File2']['tmp_name']==""){ }
else if(!is_uploaded_file($HTTP_POST_FILES['File2']['tmp_name'])){
$error.="<li>The file, ".$HTTP_POST_FILES['File2']['name'].", was not uploaded!";
$errors=1;
}
if($HTTP_POST_FILES['File3']['tmp_name']==""){ }
else if(!is_uploaded_file($HTTP_POST_FILES['File3']['tmp_name'])){
$error.="<li>The file, ".$HTTP_POST_FILES['File3']['name'].", was not uploaded!";
$errors=1;
}
if($HTTP_POST_FILES['File4']['tmp_name']==""){ }
else if(!is_uploaded_file($HTTP_POST_FILES['File4']['tmp_name'])){
$error.="<li>The file, ".$HTTP_POST_FILES['File4']['name'].", was not uploaded!";
$errors=1;
}
if($HTTP_POST_FILES['File5']['tmp_name']==""){ }
else if(!is_uploaded_file($HTTP_POST_FILES['File5']['tmp_name'])){
$error.="<li>The file, ".$HTTP_POST_FILES['File5']['name'].", was not uploaded!";
$errors=1;
}
if($HTTP_POST_FILES['File6']['tmp_name']==""){ }
else if(!is_uploaded_file($HTTP_POST_FILES['File6']['tmp_name'])){
$error.="<li>The file, ".$HTTP_POST_FILES['File6']['name'].", was not uploaded!";
$errors=1;
}
if($HTTP_POST_FILES['Comments']['tmp_name']==""){ }
else if(!is_uploaded_file($HTTP_POST_FILES['Comments']['tmp_name'])){
$error.="<li>The file, ".$HTTP_POST_FILES['Comments']['name'].", was not uploaded!";
$errors=1;
}
if(!eregi("^[a-z0-9]+([_\\.-][a-z0-9]+)*" ."@"."([a-z0-9]+([\.-][a-z0-9]+)*)+"."\\.[a-z]{2,}"."$",$Emailaddress)){
$error.="<li>Invalid email address entered";
$errors=1;
}
if($errors==1) echo $error;
else{
$image_part = date("h_i_s")."_".$HTTP_POST_FILES['File1']['name'];
$image_list[3] = $image_part;
copy($HTTP_POST_FILES['File1']['tmp_name'], "files/".$image_part);
$image_part = date("h_i_s")."_".$HTTP_POST_FILES['File2']['name'];
$image_list[4] = $image_part;
copy($HTTP_POST_FILES['File2']['tmp_name'], "files/".$image_part);
$image_part = date("h_i_s")."_".$HTTP_POST_FILES['File3']['name'];
$image_list[5] = $image_part;
copy($HTTP_POST_FILES['File3']['tmp_name'], "files/".$image_part);
$image_part = date("h_i_s")."_".$HTTP_POST_FILES['File4']['name'];
$image_list[6] = $image_part;
copy($HTTP_POST_FILES['File4']['tmp_name'], "files/".$image_part);
$image_part = date("h_i_s")."_".$HTTP_POST_FILES['File5']['name'];
$image_list[7] = $image_part;
copy($HTTP_POST_FILES['File5']['tmp_name'], "files/".$image_part);
$image_part = date("h_i_s")."_".$HTTP_POST_FILES['File6']['name'];
$image_list[8] = $image_part;
copy($HTTP_POST_FILES['File6']['tmp_name'], "files/".$image_part);
$image_part = date("h_i_s")."_".$HTTP_POST_FILES['Comments']['name'];
$image_list[9] = $image_part;
copy($HTTP_POST_FILES['Comments']['tmp_name'], "files/".$image_part);
$where_form_is="http".($HTTP_SERVER_VARS["HTTPS"]=="on"?"s":"")."://".$SERVER_NAME.strrev(strstr(strrev($PHP_SELF),"/"));
$message="Contact name: ".$Contactname."
Phone number: ".$Phonenumber."
Email address: ".$Emailaddress."
File 1: ".$where_form_is."files/".$image_list[3]."
File 2: ".$where_form_is."files/".$image_list[4]."
File 3: ".$where_form_is."files/".$image_list[5]."
File 4: ".$where_form_is."files/".$image_list[6]."
File 5: ".$where_form_is."files/".$image_list[7]."
File 6: ".$where_form_is."files/".$image_list[8]."
Comments: ".$where_form_is."files/".$image_list[9]."
";
$message = stripslashes($message);
mail("print@wildfiremarketinggroup.com","File upload form",$message,"From: $Email");
header("Refresh: 0;url=http://www.wildfiremarketinggroup.com");
?><?php
}
?>
Any ideas? Also - the script needs to be able to upload large files (>10mb). Is this a setting on the server that needs to be changed or am I taking the wrong approach here?
|

06-27-2006, 12:23 PM
|
 |
SEO Junior
|
|
Join Date: Jun 2006
Posts: 22
|
|
on your call to str_replace(), the underscore should be enclosed in the 2nd pair of quotes like this
PHP Code:
$temp = str_replace(" ", "_", $temp);
|

06-27-2006, 01:18 PM
|
 |
SEO GUY Moderator
|
|
Join Date: Sep 2004
Location: Antalya, Turkey
Posts: 4,111

|
|
Hmmmm... it says "You must spread some Reputation around before giving it to ixpleo again." ... someone help me out... 
__________________
10.3 million entries for Hotels in Turkey but I'm still chipping away.
|

06-27-2006, 11:15 PM
|
 |
SEO GUY Moderator
|
|
Join Date: May 2004
Location: In perpetual style
Posts: 3,305
|
|
This is what i have now...still no dice  ( http://www.wildfiremarketinggroup.com/form1.html)
PHP Code:
<?php
include("global.inc.php");
$temp = str_replace(" ", "_", $temp);
echo $temp;
$errors=0;
$error="The following errors occured while processing your form input.<ul>";
pt_register('POST','Name');
pt_register('POST','Phone');
pt_register('POST','Email');
$File1=$HTTP_POST_FILES['File1'];
$File2=$HTTP_POST_FILES['File2'];
$File3=$HTTP_POST_FILES['File3'];
$File4=$HTTP_POST_FILES['File4'];
$File5=$HTTP_POST_FILES['File5'];
$File6=$HTTP_POST_FILES['File6'];
pt_register('POST','Comments');
$Comments=preg_replace("/(\015\012)|(\015)|(\012)/"," <br />", $Comments);if($Name=="" || $Phone=="" || $Email=="" ){
$errors=1;
$error.="<li>You did not enter one or more of the required fields. Please go back and try again.";
}
if($HTTP_POST_FILES['File1']['tmp_name']==""){ }
else if(!is_uploaded_file($HTTP_POST_FILES['File1']['tmp_name'])){
$error.="<li>The file, ".$HTTP_POST_FILES['File1']['name'].", was not uploaded!";
$errors=1;
}
if($HTTP_POST_FILES['File2']['tmp_name']==""){ }
else if(!is_uploaded_file($HTTP_POST_FILES['File2']['tmp_name'])){
$error.="<li>The file, ".$HTTP_POST_FILES['File2']['name'].", was not uploaded!";
$errors=1;
}
if($HTTP_POST_FILES['File3']['tmp_name']==""){ }
else if(!is_uploaded_file($HTTP_POST_FILES['File3']['tmp_name'])){
$error.="<li>The file, ".$HTTP_POST_FILES['File3']['name'].", was not uploaded!";
$errors=1;
}
if($HTTP_POST_FILES['File4']['tmp_name']==""){ }
else if(!is_uploaded_file($HTTP_POST_FILES['File4']['tmp_name'])){
$error.="<li>The file, ".$HTTP_POST_FILES['File4']['name'].", was not uploaded!";
$errors=1;
}
if($HTTP_POST_FILES['File5']['tmp_name']==""){ }
else if(!is_uploaded_file($HTTP_POST_FILES['File5']['tmp_name'])){
$error.="<li>The file, ".$HTTP_POST_FILES['File5']['name'].", was not uploaded!";
$errors=1;
}
if($HTTP_POST_FILES['File6']['tmp_name']==""){ }
else if(!is_uploaded_file($HTTP_POST_FILES['File6']['tmp_name'])){
$error.="<li>The file, ".$HTTP_POST_FILES['File6']['name'].", was not uploaded!";
$errors=1;
}
if(!eregi("^[a-z0-9]+([_\\.-][a-z0-9]+)*" ."@"."([a-z0-9]+([\.-][a-z0-9]+)*)+"."\\.[a-z]{2,}"."$",$Email)){
$error.="<li>Invalid email address entered";
$errors=1;
}
if($errors==1) echo $error;
else{
$image_part = date("h_i_s")."_".$HTTP_POST_FILES['File1']['name'];
$image_list[3] = $image_part;
copy($HTTP_POST_FILES['File1']['tmp_name'], "files/".$image_part);
$image_part = date("h_i_s")."_".$HTTP_POST_FILES['File2']['name'];
$image_list[4] = $image_part;
copy($HTTP_POST_FILES['File2']['tmp_name'], "files/".$image_part);
$image_part = date("h_i_s")."_".$HTTP_POST_FILES['File3']['name'];
$image_list[5] = $image_part;
copy($HTTP_POST_FILES['File3']['tmp_name'], "files/".$image_part);
$image_part = date("h_i_s")."_".$HTTP_POST_FILES['File4']['name'];
$image_list[6] = $image_part;
copy($HTTP_POST_FILES['File4']['tmp_name'], "files/".$image_part);
$image_part = date("h_i_s")."_".$HTTP_POST_FILES['File5']['name'];
$image_list[7] = $image_part;
copy($HTTP_POST_FILES['File5']['tmp_name'], "files/".$image_part);
$image_part = date("h_i_s")."_".$HTTP_POST_FILES['File6']['name'];
$image_list[8] = $image_part;
copy($HTTP_POST_FILES['File6']['tmp_name'], "files/".$image_part);
$where_form_is="http".($HTTP_SERVER_VARS["HTTPS"]=="on"?"s":"")."://".$SERVER_NAME.strrev(strstr(strrev($PHP_SELF),"/"));
$message="Name: ".$Name."
Phone: ".$Phone."
Email: ".$Email."
File 1: ".$where_form_is."www.wildfiremarketinggroup.com/files/".$image_list[3]."
File 2: ".$where_form_is."www.wildfiremarketinggroup.com/files/".$image_list[4]."
File 3: ".$where_form_is."www.wildfiremarketinggroup.com/files/".$image_list[5]."
File 4: ".$where_form_is."www.wildfiremarketinggroup.com/files/".$image_list[6]."
File 5: ".$where_form_is."www.wildfiremarketinggroup.com/files/".$image_list[7]."
File 6: ".$where_form_is."www.wildfiremarketinggroup.com/files/".$image_list[8]."
Comments: ".$Comments."
";
$message = stripslashes($message);
mail("print@wildfiremg.com","File upload form",$message,"From: $Email");
header("Refresh: 0;url=http://www.wildfiremg.com");
?><?php
}
?>
|

06-28-2006, 03:57 PM
|
 |
SEO Junior
|
|
Join Date: Jun 2006
Posts: 22
|
|
i'm not exactly sure what $temp is holding...? Where is $temp set?
I wrote some new code for you...yours was hard to follow and your error checking was easily bypassed by putting a space into the formfields. Now I can't guarantee this will work right off the bat. This hasn't even been tested...IMO, file uploads can be hit or miss depending on the server and their settings. copy() doesn't work well on some servers and is better replaced with move_uploaded_file(). I also stripped some of your code out...once it's working you can add in your regular expressions, etc.
either way, give this code a try and mess around with it some as you may need to do some tweaking to get it to work. If it's still not working, let me know and I can try to help.
PHP Code:
<?php
// Include Functions
include("global.inc.php");
// Set Error Variable
$error = "";
// Upload Function
function UploadFile($file)
{
$name = date("h_i_s") . "_" . str_replace(" ", "_", $file["name"]);
if(move_uploaded_file($file["tmp_name"], "files/".$name) )
{
return "files/".$name;
}
else
{
return false;
}
}
// Handle Empty variables
if( (isset($_POST["Name"])) || (strlen(trim($_POST["Name"])) == 0) )
{
$error .= "<li>You must enter your name</li>";
}
if( (isset($_POST["Phone"])) || (strlen(trim($_POST["Phone"])) == 0) )
{
$error .= "<li>You must enter your phone</li>";
}
if( (isset($_POST["Email"])) || (strlen(trim($_POST["Email"])) == 0) )
{
$error .= "<li>You must enter your email</li>";
}
// Print Error
if($error != "")
{
print "The following errors occured while processing your form input.\n";
print "<ul class=\"error\">\n";
print "<li>You did not enter one or more of the required fields. Please go back and try again.";
print $error;
print "</ul>\n";
}
else
{
// Handle Files
for($i=0; $i<=6; $i++)
{
$key = "File".$i;
if($_FILES[$key])
{
if( $up_files[] = UploadFile($_FILES[$key]) )
{
print "File " . $i . " was successfully uploaded<br />\n";
}
else
{
print "File " . $i . " was not uploaded<br />\n";
}
}
}
// Set Variables for email
$msg = "Name: ".$_POST["Name"]."\n";
$msg .= "Phone: " . $_POST["Phone"] . "\n";
$msg .= "Email: " . $_POST["Email"] . "\n";
// Loop through uploaded files. Add to $msg
for($i=0; $i<count($up_files); $i++)
{
$msg .= "File " . $i . ": " . $up_files[$i] . "\n";
}
$msg .= "Comments: " . $_POST["Comments"];
$headers = "From: " . $_POST["Email"];
// Send Email
mail("print@wildfiremg.com", "File Upload Form", $msg, $headers);
// Redirect User
header("Location: http://www.wildfiremg.com");
}
?>
Good luck!
Last edited by ixpleo : 06-29-2006 at 01:49 PM.
|

07-01-2006, 11:19 PM
|
 |
SEO GUY Moderator
|
|
Join Date: May 2004
Location: In perpetual style
Posts: 3,305
|
|
|
Hmmm...tried it but no matter what, it says that the required fields were not filled in.
|

07-03-2006, 09:05 AM
|
 |
SEO Junior
|
|
Join Date: Jun 2006
Posts: 22
|
|
whoops...
I forgot an exclamation mark before those isset()s so that it will test
if field is NOT set
without the exclamation, it was checking if field is set, and the second part of the if returns true if it isn't set, so regardless, it was always evaluating to true. I also realized I needed to start counting the loops at 1, so I made that change.
Go ahead and try this code...I tested and it worked great. Just make sure your "files" directory is writeable. Also, if you're running anything older than PHP 4.1.0, you'll need to replace $_POST with $HTTP_POST_VARS and $_FILES with $HTTP_POST_FILES.
PHP Code:
<?php
// Include Functions
include("global.inc.php");
// Set Error Variable
$error = "";
// Upload Function
function UploadFile($file)
{
$name = date("h_i_s") . "_" . str_replace(" ", "_", $file["name"]);
if(move_uploaded_file($file["tmp_name"], "files/".$name) )
{
return "files/".$name;
}
else
{
return false;
}
}
// Handle Empty variables
if( (!isset($_POST["Name"])) || (strlen(trim($_POST["Name"])) == 0) )
{
$error .= "<li>You must enter your name</li>";
}
if( (!isset($_POST["Phone"])) || (strlen(trim($_POST["Phone"])) == 0) )
{
$error .= "<li>You must enter your phone</li>";
}
if( (!isset($_POST["Email"])) || (strlen(trim($_POST["Email"])) == 0) )
{
$error .= "<li>You must enter your email</li>";
}
// Print Error
if($error != "")
{
print "The following errors occured while processing your form input.\n";
print "<ul class=\"error\">\n";
print "<li>You did not enter one or more of the required fields. Please go back and try again.";
print $error;
print "</ul>\n";
}
else
{
// We need this array to start at 1
// So set a value for zero
$up_files[0] = "";
// Handle Files
for($i=1; $i<=6; $i++)
{
$key = "File".$i;
if(isset($_FILES[$key]))
{
if( $up_files[] = UploadFile($_FILES[$key]) )
{
print "File " . $i . " was successfully uploaded<br />\n";
}
else
{
print "File " . $i . " was not uploaded<br />\n";
}
}
}
// Set Variables for email
$msg = "Name: ".$_POST["Name"]."\n";
$msg .= "Phone: " . $_POST["Phone"] . "\n";
$msg .= "Email: " . $_POST["Email"] . "\n";
// Loop through uploaded files. Add to $msg
for($i=1; $i<=count($up_files); $i++)
{
$msg .= "File " . $i . ": " . $up_files[$i] . "\n";
}
$msg .= "Comments: " . $_POST["Comments"];
$headers = "From: " . $_POST["Email"];
// Send Email
mail("print@wildfiremg.com", "File Upload Form", $msg, $headers);
// Redirect User
header("Location: http://www.wildfiremg.com");
}
?>
One last suggestion I have for you is to be able to call you form with a class or function, so when you output error messages on the process page, you could say $f->DisplayForm($errors); and it would display the form with the bulleted errors on top, and also the values filled in so the user doesn't have to start over. Let me know if you need help with this.
Last edited by ixpleo : 07-03-2006 at 12:26 PM.
|

07-11-2006, 07:04 AM
|
 |
SEO GUY Moderator
|
|
Join Date: May 2004
Location: In perpetual style
Posts: 3,305
|
|
|
Well, it seems to work with small files, so I think the errors saying fields were left empty is actually related to large files causing a problem (which is related to the server config rather than the script). However - on my old script, the email contained links to the files so that the recipiant could simply click on it and download the file...how can I add that functionality to this script?
|

07-11-2006, 07:09 AM
|
 |
SEO GUY Moderator
|
|
Join Date: May 2004
Location: In perpetual style
Posts: 3,305
|
|
|
I'm also getting a header error of some sort.
|

07-11-2006, 07:19 AM
|
 |
SEO Junior
|
|
Join Date: Jun 2006
Posts: 22
|
|
The large files shouldn't give you validation errors that we set in the script. If the script is saying that you left the name field empty, etc, that shouldn't have any relation to the large files (unless the large file causes a timeout or something, and PHP never got to validating those fields).
If you're uploading in the 10meg range, you may need to make changes in your PHP configuration settings ( http://www.radinks.com/upload/config.php - see link if you have access to change). If you don't have physical control over the server, you may be limited to smaller filesizes. To check your server's PHP settings, just create a file info.php like this
PHP Code:
<?php phpinfo() ?>
and see what the limits are.
As for the header error: you can't send headers if you've already returned output to the browser. The output is coming from this section:
PHP Code:
if(isset($_FILES[$key]))
{
if( $up_files[] = UploadFile($_FILES[$key]) )
{
print "File " . $i . " was successfully uploaded<br />\n";
}
else
{
print "File " . $i . " was not uploaded<br />\n";
}
}
}
Just change to
PHP Code:
if(isset($_FILES[$key]))
{
if( $up_files[] = UploadFile($_FILES[$key]) )
{
// do nothing
}
else
{
$error = "File " . $i . " was not uploaded<br />\n";
}
}
}
And finally, for the email link to the file, in the upload function, replace
PHP Code:
return "files/".$name;
with
PHP Code:
return "http://www.yourdomain.com/files/".$name;
(replacing yourdomain.com with your domain, and any additional directories to get to the files folder).
Let me know how that works out.
|
 |
|
| Thread Tools |
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|
|