PHP - 无法移动文件

我正在寻找使用 PHPMailer 发送附件。要发送邮件,我需要存储它,因此在此过程中我会自动创建一个文件夹,并且我想将这些上传的文件保存在该文件夹中。


文件夹已正确创建,但文件未在文件夹内移动。我试过使用move_uploaded_fileandcopy但这不起作用。


如果有人能告诉我这里出了什么问题...


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

                }

            }

谢谢你的帮助


眼眸繁星
浏览 157回答 3
3回答

BIG阳

第一个问题是您正在处理带有$_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    }}

江户川乱折腾

使用此链接后将服务器文件权限更改为 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    }}

慕侠2389804

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!";   }}
打开App,查看更多内容
随时随地看视频慕课网APP