我正在寻找使用PHPMailer发送附件的方法。 要发送邮件,我需要对其进行存储,因此我会在此过程中自动创建一个文件夹,并将这些上传的文件保存在此文件夹中。
已正确创建文件夹,但文件未在文件夹内移动。我已经尝试过使用move_uploaded_file
和copy
,但这是行不通的。
如果有人可以告诉我这是怎么回事...
if (!empty($_POST['uploaded_file']) & isset($_POST['uploaded_file'])){
// Creatinf folder ploads if not exist
$path_upload = "uploads";
createFolderIfNotExist($path_upload);
// create folder with company name if not exist
$path_file = $path_upload . '/mail_upload';
createFolderIfNotExist($path_file);
// create folder with date + id_user
$path_file .= "/".date("Ymd").$user->getId();
createFolderIfNotExist($path_file);
foreach ($_POST['uploaded_file'] as $attachment2) {
move_uploaded_file($attachment2, "../".$path_file."/".$attachment2);
$pj = "/".$path_file."/".$attachment2;
// Attachments
$mail->addAttachment($pj); // Optional name
}
}
谢谢您的帮助
答案 0 :(得分:3)
第一个问题是您正在使用$_POST
处理上传的文件。它应该是$_FILES
。第二个问题是您应该在tmp_name
函数中使用move_uploaded_file
索引。第三个问题是,如果您要上传多个文件,则foreach
循环是不正确的。好像您正在将文件上传到$path_file
目录之外。因此,请尝试以下操作:
if (!empty($_FILES) & isset($_FILES['uploaded_file']['tmp_name'])) {
// Create folder uploads if not exists
$path_upload = 'uploads';
createFolderIfNotExist($path_upload);
// create folder with company name if not exists
$path_file = $path_upload . '/mail_upload';
createFolderIfNotExist($path_file);
// create folder with date + id_user
$path_file .= '/' . date('Ymd') . $user->getId();
createFolderIfNotExist($path_file);
foreach ($_FILES['uploaded_file']['name'] as $key => $attachment2) {
move_uploaded_file($_FILES['uploaded_file']['tmp_name'][$key], $path_file . '/' . $attachment2);
$pj = '/' . $path_file . '/' . $attachment2;
// Attachments
$mail->addAttachment($pj); // Optional name
}
}
答案 1 :(得分:0)
在使用此链接后,将服务器文件权限更改为777,希望您的问题能得到解决。
if (!empty($_FILES) & isset($_FILES['uploaded_file']['tmp_name'])) {
// Create folder uploads if not exists
$path_upload = 'uploads';
if (!file_exists($path_upload)) {
mkdir($path_upload, 0777, true);
}
// create folder with company name if not exists
$path_file = $path_upload . '/mail_upload';
if (!file_exists($path_file)) {
mkdir($path_file, 0777, true);
}
// create folder with date + id_user
$path_file .= '/' . date('Ymd') . $user->getId();
createFolderIfNotExist($path_file);
if (!file_exists($path_file)) {
mkdir($path_file, 0777, true);
}
foreach ($_FILES['uploaded_file']['name'] as $key => $attachment2) {
move_uploaded_file($_FILES['uploaded_file']['tmp_name'][$key], $path_file . '/' . $attachment2);
$pj = '/' . $path_file . '/' . $attachment2;
// Attachments
$mail->addAttachment($pj); // Optional name
}
}
使用此链接:Click Here
答案 2 :(得分:0)
if (!empty($_FILES) & isset($_FILES['uploaded_file']['tmp_name'])) {
if (move_uploaded_file($_FILES['uploaded_file']['tmp_name'], "/documents/new/")) {
print "Uploaded successfully!";
} else {
print "Upload failed!";
}
}
答案 3 :(得分:0)
move_uploaded_file()函数将上载的文件移动到新位置。如果目标文件已经存在,它将被覆盖。在move_uploaded_file函数中使用tmp_name索引。如果文件移动到新位置,它将回显成功,否则失败。