简体   繁体   English

用户不想要照片时邮件附件出现错误

[英]Getting error with mail attachment when user does not want photo

I've got a cake order form for my website and part of it allows the user to decide whether or not they want a photo on their cake. 我的网站上有一个蛋糕订购单,其中的一部分可以让用户决定是否要在蛋糕上拍张照片。 When they complete the form and it is confirmed the order is created on my sql table and they are sent an email confirmation, in the email is an attached pdf and the photo they want (if they choose to have one). 当他们填写完表格并确认订单后,便在我的sql表上创建了订单,并向他们发送了一封电子邮件确认信,电子邮件中附有pdf文件以及他们想要的照片(如果他们选择有一张)。 This works if they do want a photo but I get an error if they decide they don't want one. 如果他们确实想要一张照片,这是可行的,但是如果他们决定不想要一张照片,我会报错。

This is the html for the photo part of the form: 这是表单照片部分的html:

<p>Would you like a photo on your cake?(£7)</p>
<div>
<label class="checkbox-inline"></label>
    <input type="radio" name="photo[]" class="photo" id="photo[]" value="7">Yes
</label>
<label class="checkbox-inline">
<input type="radio" name="photo[]" class="photo" id="photo[]" value="0"/>No
</div>

</label>
<br /><br />
<div class="7 box" style="display: none;">
<input type="file" name="image" id="image" onchange="readURL(this);" accept="image/*"/>
 <img id="cakePhoto" src="#" />

 </div>

 <div class="0 box" style="display: none;">
 <p>You do not want a photo on your cake</p>
 </div>

The reason for the div classes is because one will appear depending on what the user decides, if they choose yes then the file input appears, but if they choose no then the "You do not want a photo on your cake" appears 之所以使用div类,是因为将根据用户的决定显示div类,如果他们选择“是”,则会出现文件输入,但是如果他们选择“否”,则会出现“您不希望蛋糕上有照片”

I am using $_SESSION for storing the values as I need them on a couple different pages but this is how I am storing the photo uploaded by the user: 我在几个不同的页面上使用$ _SESSION来存储值,因为我需要它们,但这就是我存储用户上传的照片的方式:

$filename    = $_FILES["image"]["tmp_name"];
  $destination = "uploads/" . $_FILES["image"]["name"]; 
 move_uploaded_file($filename, $destination); //save uploaded picture to directory

    $_SESSION['photo'] = $destination;

For sending the email I am attaching the photo like so: 为了发送电子邮件,我将照片附加如下:

$mail->addAttachment($_SESSION['photo']);

Like I said this works if the user has decided to upload file but if they do not then I get the following error: 就像我说的那样,如果用户决定上传文件,这是可行的,但是如果他们不这样做,那么我将收到以下错误:

Could not access file: uploads/ 无法访问文件:上传/

Is there a way I can get around this? 有办法解决这个问题吗? I was thinking of perhaps something like an if statement for if the photo exists then attach it but I'm really not sure how I would go about doing this. 我当时在想一个if语句,如果有的话就附上照片,但是我真的不确定如何去做。 Any help would be greatly appreciated 任何帮助将不胜感激

Edit: I've tried the suggestion given by droopsnoot but it is still not working so I've played about with it and tried testing it by doing the following: 编辑:我已经尝试了droopsnoot给出的建议,但是它仍然没有起作用,所以我尝试了一下,并尝试通过以下操作对其进行测试:

if (isset($_FILES["image"]["tmp_name"])) { 
  $filename    = $_FILES["image"]["tmp_name"];
  $destination = "uploads/" . $_FILES["image"]["name"]; 
  move_uploaded_file($filename, $destination); //save uploaded picture to directory
  $_SESSION['photo'] = $destination;
} else {
unset($_SESSION['photo']);
}

if (isset($_SESSION['photo'])) { 
echo ($_SESSION['photo']);
} else {
    echo "No photo chosen";
}

If no is selected for choosing an image it still shows: uploads/ so this would suggest that unset is not working, what could be done to sort this? 如果未选择“否”以选择图像,则该图像仍会显示:“ uploads /”(上载/),因此这表明unset不起作用,该怎么办才能对其进行排序?

You should always check whether or not an image has been uploaded (like you should check all other form variables) and only try to move the image, and use it as an attachment, if there is one. 您应该始终检查图像是否已上传(就像应该检查所有其他表单变量一样),并且仅尝试移动图像并将其用作附件(如果有)。

You're getting that error message because the code, without an image name, is referring to the entire directory, which you can't use in the same way. 之所以收到该错误消息,是因为没有图像名称的代码引用的是整个目录,您不能以相同的方式使用该目录。 You should only set the session variable (or however you refer to the image file) if one is uploaded, and only add it as an attachment if it's present. 如果上传了会话变量,则仅应设置会话变量(或引用图像文件),如果存在,则仅将其添加为附件。

So 所以

if (isset($_FILES["image"]["tmp_name"} { 
  $filename    = $_FILES["image"]["tmp_name"];
  $destination = "uploads/" . $_FILES["image"]["name"]; 
  move_uploaded_file($filename, $destination); //save uploaded picture to directory
  $_SESSION['photo'] = $destination;
} else {
unset($_SESSION['photo']);
}

and

if (isset($_SESSION['photo'])) { 
  $mail->addAttachment($_SESSION['photo']);
}

use 采用

if (isset($_SESSION['photo']) && file_exists($_SESSION['photo'])) {
    $mail->addAttachment($_SESSION['photo']);
}

So after doing some more research into this I found that a number of people have had issues with using isset when determining whether an image has been uploaded or not. 因此,在对此进行了更多研究之后,我发现许多人在确定是否已上传图像时在使用isset时遇到了问题。 Seems that it still thinks that it is set even when null so a solution that I found that now works for me is as follows: 似乎它仍然认为即使为null也会被设置,因此我发现现在可以使用的解决方案如下:

For finding out if a photo has been uploaded or not: 要确定照片是否已上传:

if (!file_exists($_FILES['image']['tmp_name']) || !is_uploaded_file($_FILES['image']['tmp_name'])) {
    unset($_SESSION['photo']);

}else{
  $filename    = $_FILES["image"]["tmp_name"];
  $destination = "uploads/" . $_FILES["image"]["name"]; 
  move_uploaded_file($filename, $destination); //save uploaded picture to directory
  $_SESSION['photo'] = $destination;

}

and then in the mail part for uploading I have kept it the same as to the solution droopsnoot kindly provided: 然后在要上传的邮件部分中,我将其与droopsnoot提供的解决方案相同:

if (isset($_SESSION['photo'])) { 
  $mail->addAttachment($_SESSION['photo']);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM