Jump to content
Sign in to follow this  
webchambers1

attach file from folder in server

Recommended Posts

Hi, I have a form where a user selects a file to attach to the email. At the moment when you select a file it uploads from the user device.

How do i change this so that a user can attach a file from a folder on the server. For example the folder name is uploadinvoice

so when the user selects the browse button to attach a file it opens up the uploadinvoice folder on the server so the user can select the file from there ?

 

Thanks 

 

coding i have at moment

function ValidateEmail($email)

{

   $pattern = '/^([0-9a-z]([-.\w]*[0-9a-z])*@(([0-9a-z])+([-\w]*[0-9a-z])*\.)+[a-z]{2,6})$/i';

   return preg_match($pattern, $email);

}

if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['formid']) && $_POST['formid'] == 'form1')

{

   $mailto = $_POST['youremail'];

   $mailfrom = isset($_POST['myemail']) ? $_POST['myemail'] : $mailto;

   $subject = 'Message';

   $message = 'Message';

   $success_url = './test.php';

   $error_url = '';

   $eol = "\n";

   $error = '';

   $internalfields = array ("submit", "reset", "send", "filesize", "formid", "captcha_code", "recaptcha_challenge_field", "recaptcha_response_field", "g-recaptcha-response");

   $boundary = md5(uniqid(time()));

   $header  = 'From: '.$mailfrom.$eol;

   $header .= 'Reply-To: '.$mailfrom.$eol;

   $header .= 'MIME-Version: 1.0'.$eol;

   $header .= 'Content-Type: multipart/mixed; boundary="'.$boundary.'"'.$eol;

   $header .= 'X-Mailer: PHP v'.phpversion().$eol;

   try

   {

      if (!ValidateEmail($mailfrom))

      {

         $error .= "The specified email address (" . $mailfrom . ") is invalid!\n<br>";

         throw new Exception($error);

      }

      $message .= $eol;

      foreach ($_POST as $key => $value)

      {

         if (!in_array(strtolower($key), $internalfields))

         {

            if (!is_array($value))

            {

               $message .= ucwords(str_replace("_", " ", $key)) . " : " . $value . $eol;

            }

            else

            {

               $message .= ucwords(str_replace("_", " ", $key)) . " : " . implode(",", $value) . $eol;

            }

         }

      }

      $body  = 'This is a multi-part message in MIME format.'.$eol.$eol;

      $body .= '--'.$boundary.$eol;

      $body .= 'Content-Type: text/plain; charset=ISO-8859-1'.$eol;

      $body .= 'Content-Transfer-Encoding: 8bit'.$eol;

      $body .= $eol.stripslashes($message).$eol;

      if (!empty($_FILES))

      {

         foreach ($_FILES as $key => $value)

         {

             if ($_FILES[$key]['error'] == 0)

             {

                $body .= '--'.$boundary.$eol;

                $body .= 'Content-Type: '.$_FILES[$key]['type'].'; name='.$_FILES[$key]['name'].$eol;

                $body .= 'Content-Transfer-Encoding: base64'.$eol;

                $body .= 'Content-Disposition: attachment; filename='.$_FILES[$key]['name'].$eol;

                $body .= $eol.chunk_split(base64_encode(file_get_contents($_FILES[$key]['tmp_name']))).$eol;

             }

         }

      }

      $body .= '--'.$boundary.'--'.$eol;

      if ($mailto != '')

      {

         mail($mailto, $subject, $body, $header);

      }

      header('Location: '.$success_url);

   }

   catch (Exception $e)

   {

      $errorcode = file_get_contents($error_url);

      $replace = "##error##";

      $errorcode = str_replace($replace, $e->getMessage(), $errorcode);

      echo $errorcode;

   }

   exit;

}
 

Share this post


Link to post
Share on other sites

The approach to select a server file will be completely different.  One possible strategy would be to do something like the following.

  1. Make a GET request to /files which returns [{"id": 123, "name": "some_file.txt"}, {"id": 321, "name": "some_other_file.txt"}, ...].
  2. User selects a file and store the value on the client.  If you are using XMLHttpRequest, it will be easier since it just be stored in the DOM, however, other options will work and maybe just delete it every time the page is accessed so you don't send old files.
  3. When the user sends the message off, include the ID with the data.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.