PHP - 无法移动文件
PHP - Can't move file
我正在寻找使用 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
}
}
谢谢你的帮助
更改服务器文件权限到 777 然后使用这个link希望你的问题能得到解决。
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
}
}
使用这个 Link : Click Here
第一个问题是您使用 $_POST
处理上传的文件。应该是 $_FILES
.
第二个问题是您应该在 move_uploaded_file
函数中使用 tmp_name
索引。
第三个问题是,如果您上传多个文件,您的 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
}
}
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!";
}
}
move_uploaded_file() 函数将上传的文件移动到新位置。如果目标文件已经存在,它将被覆盖。使用 move_uploaded_file 中的 tmp_name 索引 function.If 文件被移动到新位置它将回显成功,否则失败。
我正在寻找使用 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
}
}
谢谢你的帮助
更改服务器文件权限到 777 然后使用这个link希望你的问题能得到解决。
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
}
}
使用这个 Link : Click Here
第一个问题是您使用 $_POST
处理上传的文件。应该是 $_FILES
.
第二个问题是您应该在 move_uploaded_file
函数中使用 tmp_name
索引。
第三个问题是,如果您上传多个文件,您的 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
}
}
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!";
}
}
move_uploaded_file() 函数将上传的文件移动到新位置。如果目标文件已经存在,它将被覆盖。使用 move_uploaded_file 中的 tmp_name 索引 function.If 文件被移动到新位置它将回显成功,否则失败。