警告:file_get_contents(image.jpg)[function.file-get-contents]:


Warning: file_get_contents(image.jpg) [function.file-get-contents]: failed to open stream: No such file or directory in php

1。在这里,我试图使用php-mail()将我的jpg图像文件作为附件发送到邮件作用

2.我的邮件附件用word文档(.doc作为附件)发送得很好。但是,throws当我尝试将.jpg作为附件发送时出错。我使用的代码:

<?php
/*If there is no error, send the email*/
if(isset($_POST['submit-button-name'])) {       
  $uname=$_POST['uname'];                           
  $to = $_POST['mailid'];
  $mobileno=$_POST['mobile'];
  $location=$_POST['location'];
  $from = "Urname <urname@domainname.com>";
  $subject = $_POST['uname']." testing";
  $separator = md5(time());
  /* carriage return type (we use a PHP end of line constant)*/
    $eol = PHP_EOL;
  /*attachment name*/
    $filename = "image.jpg";
    $attachment = chunk_split(base64_encode(file_get_contents('image.jpg')));
  /*main header*/
    $headers  = "From: ".$from.$eol;
    $headers .= "MIME-Version: 1.0".$eol; 
    $headers .= "Content-Type: multipart/mixed; boundary='"".$separator."'"";
  /*no more headers after this, we start the body!*/
    $body = "--".$separator.$eol;
    $body .= "Content-Transfer-Encoding: 7bit".$eol.$eol;
    $body .= "Check out the attachment.".$eol;
 /*message*/
    $body .= "--".$separator.$eol;
    $body .= "Content-Type: text/html; charset='"iso-8859-1'"".$eol;
    $body .= "Content-Transfer-Encoding: 8bit".$eol.$eol;
    $body .= $message.$eol;
 /*attachment*/
    $body .= "--".$separator.$eol;
$body .= "Content-Type: image/jpg; name='"".$filename."'"".$eol; 
$body .= "Content-Transfer-Encoding: base64".$eol;
$body .= "Content-Disposition: attachment; file='"".$filename."'"".$eol.$eol;
$body .= $attachment.$eol;
$body .= "--".$separator."--";
 /*send message*/
if (mail($to, $subject, $body, $headers)) {
    $mail_sent=true;
    echo "<font color='white'>Mail sent</font>";
    $frm="urname@domainname.com";
    $subj="Acknowledgement mail for Brochure form";
    $msg = $_POST['username'] . ",'n'nThank you for your recent enquiry.";
    $body = "Name: ".$_POST['uname'] ."'n'nEmail: ".$_POST['mailid'] ."'n'nMobile: 
        ".$_POST['mobile'] ."'n'nLocation: ".$_POST['location'];
    $headers = 'From: '.'careers@domainname.com';
    mail($frm,$subj,$body,$headers);
} else {
    $mail_sent=false;
}
if($mail_sent == true)
{
/*to clear the post values*/
  $uname="";
  $to="";
  $mobileno="";
  $location="";
}
else{
  echo "Error,Mail not sent";
}
    }
    ?>

3.错误被提出为:

警告:file_get_contents(image.jpg)[function.file-get-contents]:未能
开放流:在第48行的footer.php中没有这样的文件或目录

4.我过去经常收到邮件,邮件正文("查看附件")附件为image.jpg,包含0k(未打开任何图像)。

5.请帮忙。提前谢谢。这很紧急。

我认为内容类型是jpeg而不是jpg。

Content-Type: image/jpeg;

在添加附件的部分。

还有线路:

$body .= "Content-Disposition: attachment; file='"".$filename."'"".$eol.$eol;

$body .= "--".$separator."--";

可能应该是:

$body .= "Content-Disposition: attachment;".$eol.$eol;

$body .= "--".$separator."--".$eol;

通过给出-file_get_contents("绝对路径");我使用了$_SERVER['DOCUMENT_ROOT']。'filename';它成功地实现了。此外,我将image.jpg更改为image/jpeg。谢谢大家。

我在代码的这一行中为我的图像提供了完整的绝对路径,从而解决了我的问题:

$attachment = chunk_split(base64_encode(file_get_contents('http://wordpress1/wp-content/themes/MyTheme/VyTCDC-brochure.jpg')));

所以,你可以走完整的路。现在,它就像一种魅力。