Phpmailer Could not access file

Hi all,
i have spent almost a day to find the problem. still i could not. please help.
The email delivery works charm. but attachment is giving the “Could not access file” error.


ini_set("display_errors", "true");

//include ($_SERVER["DOCUMENT_ROOT"] . "/config.php");

$validExtensions = array("jpeg", "jpg", "png", "pdf", "doc", "docx"); // valid extensions
$path = "uploads/"; // upload directory

    //echo "yes";
       echo "<div class='alert alert-danger' role='alert'>Name is required.</div>";
       echo "<div class='alert alert-danger' role='alert'>Phone Number is Required.</div>";
       echo "<div class='alert alert-danger' role='alert'Email is Required.</div>";
   //Handle upload//
   $file_name = $_FILES["file_upload"]["name"];
   $tmp = $_FILES["file_upload"]["tmp_name"];

   // Get the uploaded file's extension
   $ext = strtolower(pathinfo($file_name, PATHINFO_EXTENSION));
   // Generate a unique file name using the rand() function
   $file_1 = rand(1000, 1000000) . $file_name;

   // Check if the file format is valid
   if (in_array($ext, $validExtensions)) { 
     $path = $path . $file_1; // meaning : "/uploads" . uploaded file name
     if (move_uploaded_file($tmp, $path)) {
        //echo "done";

     echo "invalid";
   /* =========Mail Part========= */
    $subject="New Message from Website Contact From";
    $message = "
    				   <tr><td><span><h3>A New message has been received via website contact form. The Details are as followes : </h3></span></td></tr>				   
    				   <tr><td><span>Name : $name </span></td></tr>
    				   <tr><td><span>Telephone : $tel </span></td></tr>
    				   <tr><td><span>Email : $email </span></td></tr>
    				   <tr><td><span>Interest : $interest </span></td></tr>		
    				   <tr><td><span>Age Range : $age_range </span></td></tr>	
    				   <tr><td><span>Education : $education </span></td></tr>	
    				   <tr><td><span>Residency : $residency </span></td></tr>					   
    require_once "include/phpmailer/src/PHPMailer.php";
    require_once "include/phpmailer/src/SMTP.php";
    require_once "include/phpmailer/src/Exception.php";

    $mail = new PHPMailer\PHPMailer\PHPMailer();
    //$mail = new PHPMailer(true);                              
    try {
        //Server settings
        $mail->SMTPDebug = 2;                                 
        $mail->setFrom("", "Webmaster");          //This is the email your form sends From
        $mail->addAddress("", "Akram"); // Add a recipient address
        //$mail->addAddress("");               // Name is optional
        //$mail->addReplyTo(" . $email . ", " . $email . ");
        echo $path . "<br>";
        $mail->addAttachment(" . $path . ");         // Add attachments
        //$mail->addAttachment("uploads/754127Chicken-Supreme-Pizza.png", "");    // Optional name

        $mail->isHTML(true);                                  // Set email format to HTML
        $mail->Subject = "New Message from Website Contact From";
        $mail->Body    = " . $message . ";
        //$mail->AltBody = "This is the body in plain text for non-HTML mail clients";
        echo "Message has been sent";
    } catch (Exception $e) {
        echo "Message could not be sent.";
        echo "Mailer Error: " . $mail->ErrorInfo;
    /* ========= /Mail Part========= */



Notes :

  • the first line of the above image shows echo of the path of dynamically uploaded file.
  • tried chmod 0777 on the file before the attachment line. but no luck.
  • If i hard code the attachment path it works.
$mail->addAttachment("uploads/180774BBQ-Texas-Pizza.png", ""); 

even i tried with full url. but same result. first line show file path is working fine when past on the browser. but in php it says could not access file.


This using a space, a dot, another space, the $path, followed by a space, a dot, and a space as the value. Just use the variable $path as the parameter.

Oh maan…, Thanks a lot.


Worked Charm!