在用户个人资料上显示的图像路径

问题描述:

我已经设法找到我需要的代码,但只是有一些问题需要回答,以便我理解,因为我有点困惑。我知道下面的代码允许我上传图片,但是我无法得到我的头像是如何显示个人资料图片的图片,我的意思是我将使用哪条路径为个人用户定位图片。我只需要这个解释给我,让我明白在用户个人资料上显示的图像路径

非常感谢我使用的代码上传图像低于

<!--- set the full path to the images folder ---> 
<cfset mediapath = expandpath('./images')> 

<!--- set the desired image height ----> 
<cfset thumbsize = 75> 

<!--- set the desired image width ---> 
<cfset imagesize = 320> 


<cfif structKeyExists(form,"fileUpload") and len(form.fileUpload)> 
    <cffile action="upload" 
    filefield="FileUpload" 
    destination="#MediaPath#" 
    nameconflict="makeunique"> 

    <!--- read the image ----> 
    <cfimage name="uploadedImage" 
    source="#MediaPath#/#file.serverFile#" > 

    <!--- figure out which way to scale the image ---> 
    <cfif uploadedImage.width gt uploadedImage.height> 
     <cfset thmb_percentage = (thumbsize/uploadedImage.width)> 
     <cfset percentage = (imagesize/uploadedImage.width)> 
     <cfelse> 
     <cfset thmb_percentage = (thumbsize/uploadedImage.height)> 
     <cfset percentage = (imagesize/uploadedImage.height)> 
    </cfif> 

    <!--- calculate the new thumbnail and image height/width ---> 
    <cfset thumbWidth = round(uploadedImage.width * thmb_percentage)> 
    <cfset thumbHeight = round(uploadedImage.height * thmb_percentage)> 

    <cfset newWidth = round(uploadedImage.width * percentage)> 
    <cfset newHeight = round(uploadedImage.height * percentage)> 

    <!--- see if we need to resize the image, maybe it is already smaller than our desired size ---> 
    <cfif uploadedImage.width gt imagesize> 
     <cfimage action="resize" 
     height="#newHeight#" 
     width="#newWidth#" 
     source="#uploadedImage#" 
     destination="#MediaPath#/#file.serverFile#" 
     overwrite="true"/> 
    </cfif> 

    <!--- create a thumbnail for the image ---> 
    <cfimage action="resize" 
    height="#thumbHeight#" 
    width="#thumbWidth#" 
    source="#uploadedImage#" 
    destination="#MediaPath#/thumbs/#file.serverFile#" 
    overwrite="true"/> 

    <cfoutput> 
     <img src="images/thumbs/#file.serverFile#" height="#thumbHeight#" width="#thumbWidth#" align="left" hspace="10"><br> 

     Original Image: #uploadedImage.width#x#uploadedImage.height#<br> 
     Resized Image: #newWidth#x#newHeight#<br> 
     Thumbnail: #thumbWidth#x#thumbHeight#<br><br> 
     <a href="images/#file.serverFile#">See Image</a><br> 

    </cfoutput> 
</cfif> 

<form action="" method="post" enctype="multipart/form-data"> 
    <label for="fileUpload">Choose Image: </label> 
    <input type="file" name="fileUpload"> 
    <input type="submit" value="Upload Image"> 
</form> 
+1

您的代码存在一个很大的安全问题。首先,不要上传到网络可访问的目录中。其次,如果图像处理失败,则没有清理过程。所以,我可以上传一个cfm或其他任何文件。然后图像处理器会失败,然后我会有一个cfm,我可以在你的服务器*问,这可能会造成各种破坏。 –

+0

你会推荐什么,我现在正在寻找一个很好的教程 – Dipster777

+0

我只是建议你将文件加载到非Web可访问的临时目录。验证文件并在那里更改它。完成后,将其移动到最大位置。 –

要保存的图像到一个名为“/图片”文件夹,所以路径只是/images/{file name}。您需要确保:a)您在某处保留文件名;或者2)在您已有的一些信息(例如用户名或ID)后命名该文件(但我建议不要使用该ID)

+0

好吧,所以我基本上强制重命名文件到用户名图像,我wouldnt做任何事情在数据库中,我会或更好地寻找选择,将该图像绑定到数据库中的用户。 – Dipster777

+0

这完全取决于你。我可能会将文件名存储在数据库中,但不需要这样做。 –

+0

多数民众赞成在此非常感谢 – Dipster777