如何获取每个上传文件的URL并通过电子邮件发送? [英] How can I get the URL of each uploaded file and send through email?

查看:106
本文介绍了如何获取每个上传文件的URL并通过电子邮件发送?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

概述: 我有一个包含5个文件上传字段的表单.一个php脚本处理数据并发送2封电子邮件(一封给管理员,一封确认给用户),并将数据附加到服务器上的.csv文件中.

OVERVIEW: I have a form that includes 5 file upload fields. A php script processes the data and sends out 2 emails (one to the admin and a confirmation receipt to the user) AND appends the data to a .csv file on the server.

问题: 如何将上传文件的URL放入一个变量中,该变量可用于填充电子邮件和.csv文件.

QUESTION: How do I get the URL of the uploaded files in to a variable that I can use to populate the email and .csv file.

一切正常,除了我需要指向电子邮件和.csv文件中包含的每个上传文件的链接.经过几天的尝试,我似乎无法弄清楚那部分内容.

Everything is working great except I need a link to each of the the uploaded files included in the emails and in the .csv file. I cannot seem to figure that part out after a couple days of trying.

简化的HTML表单:

<HTML><head><title>MultiFile Upload and Send Email</title></head><body>         
<form method='post' action='php/multiUploadTestProcess.php' multipart='' enctype='multipart/form-data'>
    <label for ='exhName'>Exhibitor Name:</label>
    <input type='text' name='exhName' multiple class='form-control'><br><br>
    Select File 1: <input type='file' name='img[]' multiple class='form-control' size='10'><br><br>
    Select File 2: <input type='file' name='img[]' multiple class='form-control' size='10'><br><br>
    Select File 3: <input type='file' name='img[]' multiple class='form-control' size='10'><br><br>
    Select File 4: <input type='file' name='img[]' multiple class='form-control' size='10'><br><br>
    Select File 5: <input type='file' name='img[]' multiple class='form-control' size='10'><br><br>
    <input type='submit' value='upload'>
</form>

PHP:注意:我已经删除了所有的验证/清理工作,删除了.csv附加内容,并提交了第二封电子邮件.我假设一旦我们可以将链接链接到一封电子邮件中,其余的将几乎相同.

THE PHP: NOTE: I've removed all the validation/sanitizing, removed .csv appending, and the second email submission. I'm assuming once we can get the links in to one email the rest will be pretty much the same.

<?php
$exhName = $_POST['exhName'];
$exhNameNoSpace = str_replace(" ","-", $exhName);
$img = $_FILES['img'];
$to = 'DESTINATION-EMAIL@DOMAIN.com';
$subject = 'New File Uploads';
$email = 'ReplyYToEMAIL@DOMAIN.com';

if(!empty($img))
{
    /* reArrayFiles changes the array pattern for PHP's $_FILES (see function at end)  */
    $img_desc = reArrayFiles($img);
    print_r($img_desc);

    /* RENAME EACH FILE to current date, exhibitor name w/o spaces, and random integer string (ensuring no file overwrites). Then MOVE FILE to uploads */
    foreach($img_desc as $val)
    {
        $newname = date('m-d-Y',time()).'-'.$exhNameNoSpace.'-'.mt_rand().'.jpg';
        move_uploaded_file($val['tmp_name'],'../uploads/'.$newname);
        /* THIS FOLLOWING LINE WAS MY ATTEMPT AT GETTING A LINK TO EACH UPLOADED FILE BUT IS NOT WORKING. AND THE VARIABLE $newname HERE IS ADDING A NEW RANDOM INT TO THE FILENAME - NOT THE ONE THE FILE WAS SAVED WITH. */
        $filePath = '**Full Path To Directory**'.$newname;
    }
    /* SEND EMAIL TO ADMIN */
            $emailText = "=============================================\nYou have a new Market Place Application\n".
            "Here are the details:\n=============================================\n\n Exhibitor Name: $exhName \n\n Upload File 1: $filePath \n\n Upload File 2: $filePath \n\n ###";

            // All the neccessary headers for the email.
            $headers = array('Content-Type: text/plain; charset="UTF-8";',
                'From: ' . $email,
                'Reply-To: ' . $email,
                'Return-Path: ' . $email,
            );
            $emailText = wordwrap($emailText, 70);
            // Send email
            mail($to, $subject, $emailText, implode("\n", $headers));
    /* END SEND EMAIL TO ADMIN */
}
function reArrayFiles($file)
{
    $file_ary = array();
    $file_count = count($file['name']);
    $file_key = array_keys($file);

    for($i=0;$i<$file_count;$i++)
    {
        foreach($file_key as $val)
        {
            $file_ary[$i][$val] = $file[$val][$i];
        }
    }
    return $file_ary;
}   
?>

非常感谢您的帮助!

推荐答案

  1. 如果要将文件附加到电子邮件,则可能需要服务器上的完整路径,而不是外部URL.看看 realpath PHP函数,然后尝试包装路径将文件保存到此函数调用中,因此使其类似于:

  1. If you want to attach the file to your emails, you may need the fullpath on the server rather than external URL. Have a look at realpath PHP function and try to wrap the path you saved the file into this function call, so making it something like:

$newname = date('m-d-Y',time()).'-'.$exhNameNoSpace.'-'.mt_rand().'.jpg';
move_uploaded_file($val['tmp_name'],'../uploads/'.$newname);
/* THIS FOLLOWING LINE WAS MY ATTEMPT AT GETTING A LINK TO EACH UPLOADED FILE BUT IS NOT WORKING. AND THE VARIABLE $newname HERE IS ADDING A NEW RANDOM INT TO THE FILENAME - NOT THE ONE THE FILE WAS SAVED WITH. */
$filePath = realpath('../uploads/'.$newname);

$filepath之后的

应包含服务器上已上传文件的完整路径.

after this $filepath should contain the full path to the uploaded file on the server.

  1. 如果您希望在邮件中添加带有外部URL的链接,则确实需要完整的URL,但这取决于域名和上载文件夹相对于域根(/)的位置.它应该类似于 http://example.com/uploads/11 -16-2018-name-6534.jpg ,假设您可以通过网络服务器访问上传文件夹,则可以通过测试通过浏览器对生成的文件名的访问来解决.确定网址后,您可以将域名和路径保存到配置文件中,也可以检查$_SERVER['SERVER_NAME']是否包含域名,然后在路径后附加域名.
  1. If you wish to add a link with an external URL into your mails, you really need the full URL, but it depends on the domain name and the location of uploads folder relatively to the root (/) of your domain. It should be something like http://example.com/uploads/11-16-2018-name-6534.jpg and you can figure it out by testing the access to the generated filenames via your browser, assuming your uploads folder is accessible through the webserver. Once you figure out the URL you may either save the domain and path into your config file or you may check if $_SERVER['SERVER_NAME'] contains the domain name and append it with the path then.

要使用电子邮件文本中的URL,您需要修改代码,例如:

In order to use the URLs in the email text you need to modify your code like:

$emailText = "=============================================\nYou have a new Market Place Application\nHere are the details:\n=============================================\n\n Exhibitor Name: $exhName \n\n ";
$i = 1;
foreach($img_desc as $val)
{
    $newname = date('m-d-Y',time()).'-'.$exhNameNoSpace.'-'.mt_rand().'.jpg';
    move_uploaded_file($val['tmp_name'],'../uploads/'.$newname);
    /* THIS FOLLOWING LINE WAS MY ATTEMPT AT GETTING A LINK TO EACH UPLOADED FILE BUT IS NOT WORKING. AND THE VARIABLE $newname HERE IS ADDING A NEW RANDOM INT TO THE FILENAME - NOT THE ONE THE FILE WAS SAVED WITH. */
    $filePath = '**Full Path To Directory**'.$newname;
    $emailText .= "Upload File " . $i . ": " . $filePath . "\n\n";
    $i++;
}

// All the neccessary headers for the email.
$headers = array('Content-Type: text/plain; charset="UTF-8";',
            'From: ' . $email,
            'Reply-To: ' . $email,
            'Return-Path: ' . $email,
);
$emailText = wordwrap($emailText, 70);
// Send email
mail($to, $subject, $emailText, implode("\n", $headers));

这篇关于如何获取每个上传文件的URL并通过电子邮件发送?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆