我正在使用类似于下面的代码来检索文件的路径,稍后我们将其用作 php 中电子邮件消息类的输出。
//To this folder I'm uploading in another module
$folder='uploads/';
//get the server address.. it's a vpn accessible ip in our case
$dev_baseurl= 'http://192.168.xxx.xyz';
//在这种情况下对我有用的功能*(如果有更好的方法,请随时添加评论)
function check_address($current_dir,$https_check)
{
//$https_check, user defined var for
$conflen=strlen($current_dir);
//im using this and don't ask me why :P
$conflen1=0;
$B=substr(__FILE__,0,strrpos(__FILE__,'/'));
$A=substr($_SERVER['DOCUMENT_ROOT'], strrpos($_SERVER['DOCUMENT_ROOT'], $_SERVER['PHP_SELF']));
$C=substr($B,strlen($A));
$posconf=strlen($C)-$conflen1-1;
$D=substr($C,1,$posconf);
if (isset($_SERVER['HTTPS'])) $https_check=1;
switch($https_check)
{
case 0:
$host='http://'.$_SERVER['SERVER_NAME'].'/' .$D.'';
break;
case 1:
$host='https://'.$_SERVER['SERVER_NAME'].'/'.$D.'';
break;
}
//output the address
return $host;
}
尝试获取远程托管文件的路径
$dev2_url=check_address('/',1);
$dev2_url="{$dev2_url}";
/* That should be the output we are looking for
$dev_url="{$dev_baseurl}".str_replace($_SERVER['DOCUMENT_ROOT'], '', dirname($_SERVER['SCRIPT_FILENAME']))."/{$folder}";
*/
...我的 php 中的一些其他逻辑
//$body is a message string that we are sending with a class call as a parameter later
foreach( $_POST['attachments'] as $a => $avalue)
{
if (is_file($avalue))
{
$mime_type[$a] = substr(strrchr($avalue, '.'), 1);
$handle=fopen($avalue, 'rb');
$bs_name=basename($avalue);
$filesize=filesize($avalue);
$f_contents=fread($handle, $filesize);
fclose($handle);
//get_mime_type2 检查文件中的文件类型,与本例无关,但它确实用于输出 if(isset($mime_type[$a]))$mime_type[$a] = get_mime_type2($mime_type[$a ]);
$body.= '<li><ul><li>Filename: ' . $bs_name .$eol;
$body.= '<li>FileType: '.$mime_type[$a].$eol;
//In this line I use the calculated path to pass it as an output to an HTML based version of an email
$body.= "<li>Path: <a href=\"{$dev2_url}/{$avalue}\" title=\"Open w/ browser {FileName: $bs_name} | Size: {$filesize}\" target=\"_parent\">" .$avalue . "</a><li>Approx.File-Size:{$filesize} bytes</ul>";
}
}
$body.='</ul>';
所以请注意这一行:...
$body.= "<li>Path: <a href=\"{$dev2_url}/{$avalue}\" title=\"Open w/ browser {FileName: $bs_name} | Size: {$filesize}\" target=\"_parent\">" .$avalue . "</a><li>Approx.File-Size:{$filesize} bytes</ul>";
.. $dev2_url 用于查找文件的路径,我们附加文件名,这应该会处理它。我知道应该有一种更优雅的方式来完成操作所需的内容,但是如果没有真实的案例场景来说明它可以用于什么,我只能分享我自己的实现。
这给了我一个方便的输出使用.. IE:
//http://192.168.xx.xyZ is actually a real IP address
http://192.168.xx.xyZ/ProjectX/protos/jquery-menu2/includes/uploads/t_1.txt
用于附加到电子邮件的每个文件的输出