The more the news system now supports online uploads to insert pictures in order to achieve better results. But the problem also came, and some pictures passed up, found that the picture is not correct, it can only be deleted in the editor, or, in addition to the system in the process of problems, resulting in the production of garbage pictures. To prevent garbage pictures and attachments from appearing in this process, many people have studied a lot of methods, such as the Dynamic Network forum, on the upload of each file, in the database has a corresponding record, so to occupy a table to store, and if you add data to the database when the system appears abnormal, It is also impossible to avoid the creation of such rubbish. After some of my practice, to study my methods, and now contribute to the treatise for everyone.
My method process is this: when the author of the article is logged into the system, the system to create a temporary working folder, such as "editor" the user ID is 5 then I set up a TEMP5 temporary working directory, when he added the article, The uploaded images and other attachments are not stored in the directory where they are actually to be displayed, but in the temporary working directory. At the same time in order to facilitate management, I suggest that each piece of news to build a directory to store these pictures, when the article submitted, by the system analysis of the picture address, the article inside some of the pictures transferred to these corresponding directories inside.
When the news or article changes, first of all the pictures under this folder to enter the temporary working directory, while the article inside the picture path to replace, save the time is also and add when the same process. When the article is deleted, but also to delete this directory, this can ensure that in addition, modify, delete the process of the production of garbage pictures. When the user is logged out, the system can delete the corresponding working directory, so that it can be completely done without garbage production.
See the above text description may be a lot of people to dizziness, then look at the specific implementation of the process it (because I am more familiar with ASP, so I use ASP to implement it, with other platforms can also be achieved). First let me introduce myself to write a class, to analyze and transfer the picture, detailed description Please see my another article: use ASP to automatically resolve the picture address in the Web page, and save it to the local server (Http://www.csdn.net/develop/read_ article.asp?id=15585)
Class Blacksmith
' The Class ' blacksmith ' is Created by Linzhang Chen
' It could use-for-copy images form other server which contain in the Web
Dim size,baseurl,basefilename,tofolder,servername,processstr,firstoldimg,firstnewimg
Public Function saveimage (from, ToFile)
Dim Geturl, Objstream, imgs,s
If size = "" Then
Size = 0
End If
Geturl = Trim (from)
IMGs = Gethttppage (Geturl)
s = size * 512
If Len (IMGs) > S Then
Set objstream = CreateObject ("ADODB. Stream ")
objStream.Type = 1
objStream.Open
Objstream.write IMGs
Objstream.savetofile ToFile, 2
objStream.Close
Set objstream = Nothing
SaveImage = True
Else
SaveImage = False
End If
End Function
Private Function gethttppage (URL)
On Error Resume Next
Dim http
Set http = CreateObject ("MSXML2.") XMLHTTP ")
http. Open ' get ', url, False
Http.send
If http.readystate <> 4 Then
Exit Function
End If
Gethttppage = Http.responsebody
Set http = Nothing
If err.number <> 0 Then Err.Clear
End Function
Private Function Getimgs (str)
Getimgs = ""
Set OBJREGEXP1 = New RegExp
Objregexp1.ignorecase = True
Objregexp1.global = True
Objregexp1.pattern = "http://.+?"""
Set mm = Objregexp1.execute (str)
For each Match1 in mm
Getimgs = Getimgs & "| |" & Left (Match1.value, Len (match1.value)-1)
Next
End Function
Function str2img ()
Dim servername, objRegExp, STRs, Matches, Retstr, arrimg, newimg, I, fname, states, Arrnew, Arrall
If baseurl<> "" Then
If Right (BaseURL, 1) <> "/" Then
BaseURL = BaseURL & "/"
End If
End If
If Right (tofolder,1) <> "\" Then
tofolder=tofolder& "\"
End If
Set objRegExp = New RegExp
Objregexp.ignorecase = True
Objregexp.global = True
Objregexp.pattern = ""
STRs = Trim (PROCESSSTR)
Set matches = Objregexp.execute (STRs)
For the Match in matches
Retstr = retstr & Getimgs (Match.value)
Next
arrimg = Split (retstr, "| |")
Allimg = ""
Newimg = ""
For i = 1 to UBound (arrimg)
If arrimg (i) <> "" and InStr (allimg, arrimg (i)) < 1 Then
fname1 = BaseURL & CStr (Basefilename & I & Mid (arrimg (i), InStrRev (Arrimg (i), "."))
fname = Tofolder & CStr (Basefilename & I & Mid (arrimg (i), InStrRev (Arrimg (i), "."))
states = SaveImage (Arrimg (i), fname)
If states = True Then
allimg = allimg & "| |" & ARRIMG (i)
newimg = newimg & "| |" & FNAME1
End If
End If
Next
Arrnew = Split (newimg, "| |")
Arrall = Split (allimg, "| |")
For i = 1 to UBound (arrnew)
If I=1 Then
Firstoldimg=arrall (1)
Firstnewimg=arrnew (1)
End If
STRs = Replace (STRs, Arrall (i), arrnew (i))
Next
Str2img = STRs
End Function
End Class
The first step when users log in: Because there is a workspace, therefore, it is best not to allow multiple users to use the same account or the time someone logged out, will cause other people's work lost, here is the most important deal is the account login to this account lockout does not allow duplicate login (mainly by the FSO and database to achieve, I will not say more).
When processing file upload, you can use the old farmer's no component upload, the picture uploaded to the workspace. and return the picture code to the editor, when submitted, will be the following code to process these pictures, I here based on the news or the ID of the article to create a folder:
Sql= "SELECT top 1 form news where ID is null"
Set Rs=server.createobject ("Adodb.recordset")
Rs.Open sql,conn,1,3
Rs.addnew
RS ("userid") =session ("myID") ' because it's new, add one to get the ID
Rs.update
Newid=rs ("NewSID") ' In most cases, this can get the ID, but for the sake of insurance, it's best to judge
Rs.close
Set rs=nothing
If newsid= "" Then
Set Rs=conn.execute ("Select top 1 newsid from news where userid=" & Sessions ("myID") & "ORDER by NewSID Desc")
Newsid=rs ("NewsID")
End If
Basefoder=server.mappath ("photo") ' Suppose the picture is stored in the photo below the current directory
Set Fso=server.createobject ("Scripting.FileSystemObject")
FiLePaTh = basefoder& "/" &newsid
Fso.createfolder (FILEPATH)
' Assuming that the content of the news will be available in the contents of the variable content to call my class blacksmith to deal with the content of the analysis article, the transfer of the image processing
Set Bs=new Blacksmith
Bs.size=1
Bs.baseurl= "photo/" &newsid "Add a directory address to the picture
Bs.basefilename= "Mynews" to prefix the picture
Bs.servername = ""
Bs.tofolder=filepath
Bs.processstr=content
Content=bs.str2img
Set bs=nothing
' Next is the process of keeping the news content, and I'm going to omit it, and the other systems should be the same.
When dealing with changes in the news, use:
' Create working directory
Set Fso=server.createobject ("Scripting.FileSystemObject")
FiLePaTh = Server.MapPath ("Temp" &session ("myID"))
If not FSO. FolderExists (FiLePaTh) Then
Fso.createfolder (FILEPATH)
End If
FiLePaTh = basefoder& "/" &newsid
If FSO. FolderExists (FP) Then
On Error Resume Next
Fso.copyfile fp& "\*.*",filepath& "" "to the directory under the full copy of all the files to the working directory, to prevent changes are not saved, so do not delete the original content
If Err.num>0 then Err.Clear
End If
Set fso=nothing
End If
"The original file content processing, change the image path inside, for example, this content is still stored in content
Content=replace (Trim (content, "photo/" &newsID& "/mynews", "Temp" &session ("myID") & "/mynews") ' This is just a relatively simple replacement, believe that the opportunity to cause false replacement should be quite small
The process of saving and adding the process is the same, the difference is that the original directory to empty the file, I do not say more here.
When the news is deleted, remember to delete the folder that corresponds to the ID, and empty the working directory when the user log out.
Well, my whole idea is like this, say what is not fine, but it in the actual application, a year down and did not produce any meaning often, so I put it out, welcome everyone and I cross flow: e_mail:clzwin@sina.com