help with php form with a twist

Discussion in 'PHP' started by onlyican.com, Dec 3, 2005.

  1. #1
    I am fairly new to all this coding, please help me

    I have a PHP form on my website, and i would like to add a field for people to be able to send me images documents ect.

    I have a field for the images to be uploaded, click send, i get an email saying the name of the images, but the images is nowhere, not on my website or in the email. I would like the file to be sent to me.

    Here is the code from the page with the form, minus all the htm codes.

    
    <?php
    
    function showForm() {
    echo "<html><head>\n"
     ."<title>Add Your Add</title>\n"
     ."</head><body>\n"
     ."<font face='arial' size='1'>\n"
     ."<h3>Add Your Add</h3>\n"
     ."<form method='post' action='thank_you.php' enctype='mutipart/form-data'>\n"
     ."<table width='100%' border='0' cellspacing='0' cellpadding='4'>\n"
     ."<tr>\n"
     ."<td width='25%'><font face='arial' size='0'>Your Name:</font></td>\n"
     ."<td width='75%'><input type='text' name='name' size='40'></td>\n"
     ."</tr><tr>\n"
     ."<td width='25%'><font face='arial' size='0'>Your Email:</font></td>\n"
     ."<td width='75%'><input type='text' name='email' size='40'></td>\n"
     ."</tr><tr>\n"
     ."<td width='25%'><font face='arial' size='0'>Your Website Address:</font></td>\n"
     ."<td width='75%'><input type='text' name='url' size='40'></td>\n"
      ."</tr><tr>\n"
      ."<td width='25%'><font face='arial' size='0'>What Page do you want your add on:</font></td>\n"
     ."<td width='75%'><select name='page'><option>Please Select</option><option>-------------</option><option>Short Jokes</option><option>Long Jokes</option><option>Pictures</option><option>Videos</option><option>Documents</option><option>Adult Section</option><option>Adult Jokes</option><option>Adult Pictures</option><option>Adult Videos</option><option>Adult Documents</option><option>Send Your Stuff</option><option>Links</option><option>Add your Add</option><option>Feedback</option></select></td>\n"
     ."</tr><tr>\n"
     ."<input type='file' name='myfile'>\n"
     ."</tr><tr>\n"
     ."</tr><tr>\n"
     ."</tr><tr>\n"
     ."</tr><tr>\n"
     ."</tr></table>\n"
     ."<input type='submit' value='Send' onClick='return checkForm();'>\n"
     ."<input type='reset' value='Reset'>\n"
     ."</form></body></html>\n";
    }
    
     showForm();
    
    ?>
    
    PHP:
    And here is the "Thank You" Page

    
    <?php
    $to = "jokes@onlyican.com";
    $subject = "Add your add";
    
    
     $body = "Someone wants to add there add to jokes.onlyican.com:\n\r";
    
    foreach($_POST as $key => $value)
    {
        // add each var:
        $body .= "$key = $value \n\r";
    
    }
    
    foreach($_GET as $key => $value)
    {
        // add each var:
        $body .= "$key = $value \n\r";
    	
    	$_FILES['myfile'];
    
    }
    
    if (mail($to, $subject, $body))
    {
       echo("<p>Thank you ". $name ." for completing the form to add your add to <a href='http://www.jokes.onlyican.com'>jokes.onlyican.com</a><br><br>I will check your website, ". $url .", and send you an email.<br><br>Best Regards<br><br>Jamie<br>Site owner.<br><br>");
       echo("Go back to the <a href='index.php'>Home Page</a>");
    }
    else
    {
       echo("<p>Message delivery failed...</p>");
       echo("Please go back and try again or email me at <a href='mailto:jokes@onlyican.com'>jokes@onlyican.com'>");
    }
    ?>
    
    PHP:
    What have i missed, and please talk to me like i am 3 years old.
     
    onlyican.com, Dec 3, 2005 IP
  2. HN Will

    HN Will Guest

    Messages:
    111
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #2
    You'll need to move the files from the tmp directory to a directory on your server or they will get deleted.

    http://us2.php.net/features.file-upload

    this link will tell you everything that I would suggest
     
    HN Will, Dec 3, 2005 IP
  3. onlyican.com

    onlyican.com Peon

    Messages:
    206
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #3
    tmp directory, there aint one, I want the files to be emailed to me, with the form, the form gets emailed to me, with the name of the file, but not the file.
     
    onlyican.com, Dec 3, 2005 IP
  4. HN Will

    HN Will Guest

    Messages:
    111
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #4
    ?

    where do you want the uploaded file to be stored?
     
    HN Will, Dec 3, 2005 IP
  5. onlyican.com

    onlyican.com Peon

    Messages:
    206
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #5
    I want the file to be emailed to me, with the rest of the form, i do not want the files to be published automaticly online.
     
    onlyican.com, Dec 3, 2005 IP
  6. HN Will

    HN Will Guest

    Messages:
    111
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #6
    I would recommend storing the page in a non-published folder and then emailing yourself the link
     
    HN Will, Dec 3, 2005 IP
  7. onlyican.com

    onlyican.com Peon

    Messages:
    206
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #7
    SO how do i make it so the form is still emailed to me, but the file moves to a folder, called "lock" or something
     
    onlyican.com, Dec 4, 2005 IP
  8. onlyican.com

    onlyican.com Peon

    Messages:
    206
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Nearly got it

    I got a code, which will email me the form, excellent
    I got a code which will upload the file to the folder. OK

    Now if I out them together , I get a an email with the form details, but an error on the image

    here are the codes


    
    <?php
    
    function showForm() {
    echo "<html><head>\n"
     ."<title>Add Your Add</title>\n"
     ."</head><body>\n"
     ."<font face='arial' size='1'>\n"
     ."<h3>Add Your Add</h3>\n"
     ."<form method='post' action='thank_you.php' name='msgform' enctype='multipart/form-data'>\n"
     ."<table width='100%' border='0' cellspacing='0' cellpadding='4'>\n"
     ."<tr>\n"
     ."<td width='25%'><font face='arial' size='0'>Your Name:</font></td>\n"
     ."<td width='75%'><input type='text' name='name' size='40'></td>\n"
     ."</tr><tr>\n"
     ."<td width='25%'><font face='arial' size='0'>Your Email:</font></td>\n"
     ."<td width='75%'><input type='text' name='email' size='40'></td>\n"
     ."</tr><tr>\n"
     ."<td width='25%'><font face='arial' size='0'>Your Website Address:</font></td>\n"
     ."<td width='75%'><input type='text' name='url' size='40'></td>\n"
      ."</tr><tr>\n"
      ."<td width='25%'><font face='arial' size='0'>What Page do you want your add on:</font></td>\n"
     ."<td width='75%'><select name='page'><option>Please Select</option><option>-------------</option><option>Short Jokes</option><option>Long Jokes</option><option>Pictures</option><option>Videos</option><option>Documents</option><option>Adult Section</option><option>Adult Jokes</option><option>Adult Pictures</option><option>Adult Videos</option><option>Adult Documents</option><option>Send Your Stuff</option><option>Links</option><option>Add your Add</option><option>Feedback</option></select></td>\n"
     ."</tr><tr>\n"
     ."<input type='hidden' name='MAX_FILE_SIZE' value='100000'>Choose a file to upload:<input name='uploadedfile' type='file'><br>\n"
     ."</tr><tr>\n"
     ."</tr><tr>\n"
     ."</tr><tr>\n"
     ."</tr><tr>\n"
     ."</tr></table>\n"
     ."<input type='submit' value='Send' value='Upload File' onClick='return checkForm();'>\n"
     ."<input type='reset' value='Reset'>\n"
     ."</form></body></html>\n";
    }
    
     showForm();
    
    ?>
    
    PHP:

    The Thank You Page
    
    <?php
    // In PHP versions earlier than 4.1.0, $HTTP_POST_FILES should be used instead
    // of $_FILES.
    
    $uploaddir = '/var/www/uploads/';
    $uploadfile = $uploaddir . basename($_FILES['userfile']['name']);
    
    echo '<pre>';
    if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
       echo "File is valid, and was successfully uploaded.\n";
    } else {
       echo "Possible file upload attack!\n";
    }
    
    echo 'Here is some more debugging info:';
    print_r($_FILES);
    
    print "</pre>";
    ?>
    <?php
    
    $to = "jokes@onlyican.com";
    $subject = "Add your add";
    
    
     $body = "Someone wants to add there add to jokes.onlyican.com:\n\r";
    
    foreach($_POST as $key => $value)
    {
        // add each var:
        $body .= "$key = $value \n\r";
    
    }
    
    foreach($_GET as $key => $value)
    {
        // add each var:
        $body .= "$key = $value \n\r";
    
    }
    
    if (mail($to, $subject, $body))
    {
       echo("<p>Thank you ". $name ." for completing the form to add your add to <a href='http://www.jokes.onlyican.com'>jokes.onlyican.com</a><br><br>I will check your website, ". $url .", and send you an email.<br><br>Best Regards<br><br>Jamie<br>Site owner.<br><br>");
       echo("Go back to the <a href='index.php'>Home Page</a>");
    }
    else
    {
       echo("<p>Message delivery failed...</p>");
       echo("Please go back and try again or email me at <a href='mailto:jokes@onlyican.com'>jokes@onlyican.com'>");
    }
    ?>
    PHP:
     
    onlyican.com, Dec 4, 2005 IP