View Full Version : php upload form + remove spaces


jlknauff
06-27-2006, 08:04 AM
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:

<?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?

ixpleo
06-27-2006, 12:24 PM
on your call to str_replace(), the underscore should be enclosed in the 2nd pair of quotes like this$temp = str_replace(" ", "_", $temp);

Paz
06-27-2006, 01:18 PM
Hmmmm... it says "You must spread some Reputation around before giving it to ixpleo again." ... someone help me out... :D

jlknauff
06-27-2006, 11:15 PM
This is what i have now...still no dice :( (http://www.wildfiremarketinggroup.com/form1.html)

<?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)/","&nbsp;<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
}
?>

ixpleo
06-28-2006, 03:57 PM
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

// 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!

jlknauff
07-01-2006, 11:20 PM
Hmmm...tried it but no matter what, it says that the required fields were not filled in.

ixpleo
07-03-2006, 09:05 AM
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

// 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.

jlknauff
07-11-2006, 07:04 AM
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?

jlknauff
07-11-2006, 07:10 AM
I'm also getting a header error of some sort.

ixpleo
07-11-2006, 07:19 AM
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 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: 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 toif(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 return "files/".$name; with 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.

jlknauff
07-11-2006, 07:49 AM
syntax error ;)

phoztech
07-14-2006, 06:12 AM
i would take out spaces and underscores...


$urlname=preg_replace('/\s/','',$artRow['subject']);
$urlname=preg_replace('/\W/','',$urlname);
$urlname = substr_replace($urlname, "", 26);

kristopher
12-06-2006, 08:47 AM
Hi everyone, I need ur help, I want to learn and use PHP NUKE with mysql on my localhost computer, but i dont know how to start with. Please Help me out.

my favorite links (http://www.justfunnycartoons.com/jfca/index.php)

RyanSmith
12-06-2006, 09:21 AM
Well your first step is going to be to install Apache (or IIS), PHP and MySQL on your local machine. I like to manually install each, but you can check out http://www.apachefriends.org/en/xampp-windows.html
They have them all bundled with an installer that should get you up and running fast.

Next you will need to install PHP Nuke. I believe that they have an install page that will do most of the work for you as long as you have root access to MySQL. So remember when you install MySQL to take careful note of what you used for the root password.

Next, ask my any specific questions you may have. I will try to answer them.