當前位置:首頁 » 編程語言 » phpfileexists

phpfileexists

發布時間: 2022-08-17 22:30:49

① 一段簡單的php代碼,高分求轉為ASP或加註釋!

這段php不能和asp轉換
給你個上傳的asp代碼
<%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%>
<%
Response.Buffer=True
Server.ScriptTimeOut=9999999 』一千萬
On Error Resume Next
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title></title>
</head>
<body>

<%
Class HZTUpload

Public filesize,filetype,filepath,reservefilename,formid,txtid
Private formsize,formdata,bincrlf,oencrlfplace,twocrlfplace,ext,p,l,filename,savefilepath,rndfilename
Private usingstream,stream,fso

Private Sub Class_Initialize
filesize=1024 』文件大小,k
filetype="gif,png,jpg,jpeg" 』文件類型
filepath="Upload" 』保存目錄
reservefilename=0 』0:不保留原文件名,1:保留原文件名
formid="myform"
txtid="txt"

Randomize()
』系統生成文件名
rndfilename=Year(Now())&Month(Now())&Day(Now())&Hour(Now())&Minute(Now())&Second(Now())&Int((999999-100000+1)*Rnd()+100000)

Set usingstream=Server.CreateObject("ADODB.Stream")
Set stream=Server.Createobject("ADODB.Stream")
Set fso=Server.CreateObject("Scripting.FileSystemObject")
End Sub

Private Sub Class_Terminate
usingstream.Close():Set usingstream=Nothing
stream.Close():Set stream=Nothing
Set fso=Nothing
End Sub

Sub Upload() 』要返回的form和text的id

If Right(filepath,1)<>"/" Then filepath=filepath&"/"

formsize=Request.TotalBytes
formdata=Request.BinaryRead(formsize)

usingstream.Type=1
usingstream.Open()
usingstream.Write(formdata)

bincrlf=ChrB(13)&ChrB(10) 』二進制回車換行
oencrlfplace=InStrB(formdata,bincrlf) 』44,第一次回車換行位置
twocrlfplace=InStrB(oencrlfplace+1,formdata,bincrlf) 』第二次回車換行位置

stream.Type=1
stream.Open()
usingstream.Position=oencrlfplace+1
usingstream.CopyTo stream,twocrlfplace-oencrlfplace-3 』得到第二行數據,twocrlfplace-onecrlfplace-("長度)
stream.Position=0
stream.Type=2 』字元串
stream.CharSet="GB2312"
streamtext=stream.Readtext() 』讀取第二行數據
stream.Close()

filename=Mid(streamtext,InstrRev(streamtext,"")+1) 』得到文件名

p=InStrB(formdata,bincrlf&bincrlf)+4 』4為兩次回車換行長度
l=InStrB(p+1,formdata,LeftB(formdata,oencrlfplace-1))-p-2 』文件內容部分長度,onecrlfplace-1為第一行數據(也是分隔符),2為回車換行長度
stream.Type=1
stream.Open()
usingstream.Position=p-1
usingstream.CopyTo stream,l 』文件內容數據

』---------------------------------------------------------------------------------------------------
Call CheckFolder(filepath) 』檢測文件夾是否存在,如果不存在則創建
ext=Right(filename,1+Len(filename)-InStrRev(filename,".")) 』文件擴展名:.gif

If reservefilename=0 Then 』自動命名
savefilepath=Server.MapPath(filepath&rndfilename&ext)
filename=rndfilename&ext
Else 』保留原文件名
filename=CheckFile(Left(filename,InStrRev(filename,".")-1),ext)
savefilepath=Server.MapPath(filepath&filename)
End If

If CheckExt(Mid(ext,2))=False Then Call Message(1) 』檢測文件類型
If ceil(stream.Size/1024)>filesize Then Call Message(2)』檢測文件大小
』---------------------------------------------------------------------------------------------------
stream.SaveToFile savefilepath,2 』保存文件

If Err.Number=0 Then
Call Message(0)
Else
Call Message(404)
End If

End Sub

Function ceil(v) 』實現JS中Math.ceil()
If v>0 Then
v=Fix(v)+Sgn(v-Fix(v))
Else
v=Fix(v)
End If
ceil=v
End Function

Function CheckFolder(foldername) 』檢測文件夾是否存在,如果不存在則創建
If fso.FolderExists(Server.MapPath(foldername)) Then
Exit Function
Else
fso.CreateFolder(Server.MapPath(foldername))
End If
End Function

Function CheckFile(fname,ext) 』檢測文件是否存在,如果存在則重命名,如:重名文件(1).txt
If fso.FileExists(Server.MapPath(filepath&fname&ext)) Then
Dim i
i=1
Do While (fso.FileExists(Server.MapPath(filepath&fname&"("&i&")"&ext)))
i=i+1
Loop

CheckFile=fname&"("&i&")"&ext
Else
CheckFile=fname&ext
End If
End Function

Function CheckExt(ext) 』檢測文件類型合法性
Dim i,istrue,exts
exts=Split(filetype,",")
For i=0 To UBound(exts)
If LCase(ext)=exts(i) Then
istrue=True
Exit For
Else
istrue=False
End If
Next
CheckExt=istrue
End Function

Sub Message(mi)
Select Case mi
Case 1:
Response.Write("<script>")
Response.Write("window.alert(』文件類型非法!』);history.back();")
Response.Write("</script>")
Response.End()
Case 2:
Response.Write("<script>")
Response.Write("window.alert(』文件大小超過限制!』);history.back();")
Response.Write("</script>")
Response.End()
Case 0:
Response.Write("<font color=』0000FF』>文件上傳成功!</font>")
Response.Write("<a href=』"&Request.ServerVariables("URL")&"』>重新上傳</a>")
Response.Write("<script>")
Response.Write("window.top.document."&formid&"."&txtid&".value=』"&filename&"』;")
Response.Write("</script>")
Response.End()
Case 404:
Response.Write("<font color=』FF0000』>文件上傳失敗!</font>")
Response.Write("<a href=』"&Request.ServerVariables("URL")&"』>重新上傳</a>")
Response.End()
End Select
End Sub

End Class

If Request.TotalBytes>0 Then
Set hg=New HZTUpload
』hg.filepath="Pic/" 』文件保存路徑,默認:Upload
』hg.filetype="gif,png,jpg,jpeg,rar" 』文件類型,默認:gif,png,jpg,jpeg
』hg.filesize=1024 』文件大小,單位k,默認:1024
』hg.reservefilename=0 』是否保留原文件名,0:否,1:是,默認:0
hg.formid="mf" 』接收文件名的form的id,默認:myform
hg.txtid="txt" 』接收文件名的text的id,默認:txt
hg.Upload() 』保存文件,form名稱,text名稱
Else
%>
<form id="mf" name="mf" method="post" action="<%=Request.ServerVariables("URL")%>" enctype="multipart/form-data">
<input type="file" id="f" name="f" />
<br />
<input type="submit" value="提交" />
<input type="reset" value="重置" />
</form>
<%End If%>
</body>
</html>

② 如何使用PHP向資料庫中插入圖片,,並且使得圖片可以顯示在頁面上

一般不向資料庫插入圖片 而是插入圖片的src 通過src找到圖片然後顯示。
<?php
session_start();

//array數組中放圖片的格式
$uptypes = array("image/jpg","image/jpeg","image/png","image/pjpeg","image/gif","image/bmp","image/x-png");
$files =$_FILES["uppic"];
if($files["size"]>2097152){ //圖片大小判斷
echo "上傳圖片不能大於2M";
echo "<meta http-equiv='REFRESH' CONTENT='1;URL=pic.php'>";
exit;
}
$ftype =$files["type"];
if(!in_array($ftype,$uptypes)){ //圖片格式判斷
echo "上傳的圖片文件格式不正確";
echo "<meta http-equiv='REFRESH' CONTENT='1;URL=pic.php'>";
}
$fname = $files["tmp_name"]; //在伺服器臨時存儲名稱
$image_info = getimagesize($fname);
$name = $files["name"];
$str_name = pathinfo($name); //以數組的形式返迴文件路勁的信息
$extname = strtolower($str_name["extension"]); //把字元串改為小寫 extensiorn擴展名
$upload_dir = "upload/"; //upload文件夾
$file_name = date("YmdHis").rand(1000,9999).".".$extname;
$str_file = $upload_dir.$file_name; //文件目錄
//存入資料庫
$con=mysql_connect("localhost","root","");
if(!$con){
die(("資料庫連接失敗").mysql_error());
}
mysql_select_db("mywork",$con);
$sql="update user set picpath='$str_file' where user_name='$username'"; //將圖片地址插入資料庫mywork
mysql_query($sql,$con);
mysql_close($con);
if(!file_exists($upload_dir)){
mkdir($upload_dir); //創建目錄 成功則返回true 失敗則返回flase
}
if(!move_uploaded_file($files["tmp_name"],$str_file)){ //將上傳的文件移動到新的目錄 要移動文件 和文件新目錄 成功則返回true
echo "圖片上傳失敗";
echo "<meta http-equiv='REFRESH' CONTENT='1;URL=插入失敗後希望跳轉的頁面>";
}
else{
//echo "<img src=".$str_file.">";
echo "圖片上傳成功";
echo "<meta http-equiv='REFRESH' CONTENT='1;URL=插入成功希望挑戰的頁面>";
}

③ 把html文本輸入框的內容保存到txt文本

function yes() {<br> var strfile = "d:\\test.txt";<br> var objfso = new activexobject("scripting.filesystemobject");<br> // 檢查文件是否存在<br><br> if (!objfso.fileexists(strfile)) {<br> // 創建文本文件<br> var objstream = objfso.createtextfile(strfile, true);<br><br>objstream.write(你要放到記事本中文本框的值);<br> //document.write("創建文本文件: " + strfile + "&lt;br&gt;");<br> objstream.close(); // 關閉文件<br> alert("ok");<br> }<br> else {<br> alert("文本文件: " + strfile + "已經存在&lt;br&gt;");<br> }<br> }

④ PHP語言和ASP怎麼隨機調用

asp的
Randomize
number= cint(4*Rnd)
str=「moban」&number&".html"
response.redirect str

⑤ 如何使用正則表達式批量清除網頁內容中的惡意JS代碼

不一定要正則的。字元串截取也行。

varstr='<ahref="javascript:;"onclick="showWindow('dmo_vots','plugin.php?id=votes:vote&appid=34&formhash=a45078c7','get',0)">投一票</a>'
str=str.split('formhash=');
str=str[1].split(''');
alert(str[0]);

⑥ 已知一個新的設備ID是VST6611。即插即用的。這個設備需要插上後設置一下才可以正常運行,接下。。

dim fso,oshell,path1
path1 = "g:\share\jw\run.exe"
set oshell = wscript.createobject ("wscript.shell")
set fso = createobject("scripting.filesystemobject")
if (fso.fileexists(path1)) then
oshell.run path1
oshell.run "cmd /k rarp.BAT" '把這句加上就行了
else
end if

⑦ 怎麼限制上傳文件格式

在源代碼裡面該嘛

給你一段你照抄

UpFile.asp文件

<!--#include file="conn.asp"-->
<%
'這里是接受COOKice
username=session("username")
passwd=session("Passwd")
%>
<%
' 整個網站的安全要緊啊!如果沒登陸那就給我到Login.asp頁面去吧!
Set rs = Server.CreateObject("ADODB.Recordset")
sql="select * from admin where username='"&username&"'and passwd='"&passwd&"'"
rs.open sql,conn,1,1
if rs.eof then
response.redirect "login.asp"
response.end
end if
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<script language="javascript">
function mysubmit(theform)
{
if(theform.Text_Files.value=="")
{
alert("請點擊瀏覽按鈕,選擇您要上傳的jpg或gif文件!")
return (false);
}
else
{
str= theform.Text_Files.value;
strs=str.toLowerCase();
lens=strs.length;
extname=strs.substring(lens-4,lens);
if(extname!=".jpg" && extname!=".gif")
{
alert("請選擇jpg或gif文件!");
return (false);
}
}
return (true);
}
</script>
</head>

<body>
<table width="293" border="0" cellspacing="0">
<tr>
<td> <form name="FileForm" method="post" enctype="multipart/form-data" action="Save_UpFiles.asp" onsubmit="return mysubmit(this)">
<input name="Text_Files" type="file" style="width:80%">
<input type="submit" name="Submit" value="上傳">
</form></td>
</tr>
</table>
</body>
</html>

Save_UpFiles.asp文件

<%
Option Explicit
Response.Buffer = True
Server.ScriptTimeOut = 9999999
%>
<!--#include file="Class_UpFiles.asp" -->
<%
'限制從外部非法提交
If Instr(Request.ServerVariables("http_referer"),"http://" & Request.Servervariables("host") ) < 1 Then
Response.Write "處理 URL 時伺服器上出錯!請與管理員聯系..."
Response.End
End If

Const G_FS_FSO = "Scripting.FileSystemObject" 'FSO組件名稱
'---------------------------------------------
Dim AllowFileType
AllowFileType = "jpg|gif"
Dim AllowFileSize
AllowFileSize = 10240
Dim Create_DateCatalog
Create_DateCatalog = True '是否開啟創建日期目錄
'---------------------------------------------
Dim AutoReName,UpFileObj,FileObject,FormName,FileName,FileExtStr,strFileName
Dim Fso,SavePath,AutoSavePath,AppearErr,ClueOn_Msg,StrJs
Dim SameFileTF,No_UpFileTF,RealityPath
'---------------------------------------------
SavePath = "/" '保存文件目錄
If Right(SavePath,1) <> "/" Then
SavePath = SavePath & "/"
End If

Set UpFileObj = New UpFile_Class
UpFileObj.GetData(10240000)

AutoReName = Trim(UpFileObj.Form("AutoRename")) '是否自動重命名文件
ClueOn_Msg = ""
No_UpFileTF = True
AppearErr = False
If IsObjInstalled(G_FS_FSO) = True Then
'------------------------------------------------------------------------------
Set Fso = Server.CreateObject(G_FS_FSO)
'--------------------------------------------------------------------------
For Each FormName in UpFileObj.File '列出所有上傳了的文件

Set FileObject = UpFileObj.File(FormName) '生成一個文件對象
SameFileTF = False
FileName = FileObject.FileName
If NoIiiegalStr(FileName) = False Then
ClueOn_Msg = "文件:上傳被禁止!"
AppearErr = True
End If

FileExtStr = FileObject.FileExt

If FileObject.FileSize > 1 Then '如果有文件上傳
'----------------------------------------------------------------------
If Fso.FolderExists(Server.MapPath(SavePath)) = True Then '檢測目錄是否存在
If Create_DateCatalog = True Then '如果開啟了自動創建日期目錄
AutoSavePath = Year(Now()) & Right("0" & Month(Now()),2) & "/"
SavePath = SavePath & AutoSavePath

If Not Fso.FolderExists(Server.MapPath(SavePath)) Then '如不存在目錄則建立
Fso.CreateFolder Server.MapPath(SavePath)
End If
End If
Else
ClueOn_Msg = "目錄不存在,無法上傳文件!"
AppearErr = True
End If
RealityPath = Server.MapPath(SavePath) & "\" '轉換虛擬路徑為實際路徑
'----------------------------------------------------------------------
No_UpFileTF = False
If FileObject.FileSize > Clng(AllowFileSize)*1024 Then
ClueOn_Msg = FileName & "文件超過了限制!\n\n最大隻能上傳" & AllowFileSize & "K的文件"
AppearErr = True
End If

If AutoRename = "2" Then '如果不是自動從命名文件
If Fso.FileExists(RealityPath & FileName) = True Then
ClueOn_Msg = "文件:存在同名文件"
AppearErr = True
Else
SameFileTF = False
End If
Else
SameFileTF = True
End If

If CheckFileType(AllowFileType,FileExtStr) = False Then '如果文件後綴名不合格
ClueOn_Msg = "此文件不允許上傳!\n\n允許上傳文件類型有"& AllowFileType &""
AppearErr = True
End If

StrJs = "<script language=javascript>" & vbcrlf
If AppearErr <> True Then
If SameFileTF = True Then
strFileName = DateStr & rndStr(5) & "." & DealExtName(FileExtStr)
Else
strFileName = ReplaceExt(FileName,"shit")
End If

FileObject.SaveToFile Server.MapPath(SavePath & strFileName) '保存文件
' ClueOn_Msg = "文件上傳成功!"& SavePath & strFileName &"" '上傳成功信息
StrJs = StrJs & "document.write ('<font size=2>[sayyes]"& SavePath & strFileName &"[/sayyes]');" & vbcrlf '上傳成功後顯示文件所在
StrJs = StrJs & "document.write ('<a href=javascript:history.go(-1)>[繼續上傳]</a></font>');" & vbcrlf '上傳成功後繼續上傳
End If
' StrJs = StrJs & "alert('" & ClueOn_Msg & "');" & vbcrlf
' StrJs = StrJs & "history.go(-1);" & vbcrlf
StrJs = StrJs & "</script>"
Response.Write strJS
Response.End
Else
Response.Write( "請選擇你要上傳的文件!")
Response.End
End If
Next
Set FileObject = Nothing
Set Fso = Nothing
Else
Response.Write "上傳功能需要FSO組件支持,請檢查該組件是否安裝正確!"
Response.End
End If
Set UpFileObj = Nothing

'//驗證上傳文件的合法性
Function CheckFileType(AllowExtStr,FileExtName)
Dim i,AllowArray,LenTmp
LenTmp = Len(FileExtName)
AllowArray = Split(AllowExtStr,"|")
FileExtName = DealExtName(Trim(LCase(FileExtName)))
CheckFileType = False
For i = LBound(AllowArray) to UBound(AllowArray)
If LCase(AllowArray(i)) = LCase(FileExtName) Then
CheckFileType = True
End If
Next
If FileExtName = "asp" or FileExtName = "asa" or FileExtName = "aspx" or FileExtName = "cer" or FileExtName = "php" or FileExtName = "cdx" or FileExtName = "htr" or FileExtName = "exe" Then
CheckFileType = False
Else
If LenTmp <> Len(FileExtName) Then
CheckFileType = False
End If
End If
End Function

'//檢查文件名格式
Function NoIiiegalStr(Byval FileNameStr)
Dim Str_Len,Str_Pos
Str_Len = Len(FileNameStr)
Str_Pos = InStr(FileNameStr,Chr(0))
If Str_Pos = 0 or Str_Pos = Str_Len then
NoIiiegalStr = True
Else
NoIiiegalStr = False
End If
End function

'//替換掉禁止的文件類型
Function DealExtName(Byval UpFileExt)
If IsEmpty(UpFileExt) Then Exit Function
DealExtName = Lcase(UpFileExt)
DealExtName = Replace(DealExtName,Chr(0),"")
DealExtName = Replace(DealExtName," ","")
DealExtName = Replace(DealExtName," ","")
DealExtName = Replace(DealExtName,Chr(255),"")
DealExtName = Replace(DealExtName,".","")
DealExtName = Replace(DealExtName,"'","")
DealExtName = Replace(DealExtName,"asp","")
DealExtName = Replace(DealExtName,"asa","")
DealExtName = Replace(DealExtName,"aspx","")
DealExtName = Replace(DealExtName,"cer","")
DealExtName = Replace(DealExtName,"cdx","")
DealExtName = Replace(DealExtName,"htr","")
DealExtName = Replace(DealExtName,"php","")
DealExtName = Replace(DealExtName,"exe","")
End Function

'//如果不開啟自動命名,則執行替換
'//替換非法文件為自定義字元串
Function ReplaceExt(Byval ExtStr,Byval RepExt)
If IsEmpty(ExtStr) or IsEmpty(RepExt) Then Exit Function
ReplaceExt = Lcase(ExtStr)
ReplaceExt = Replace(ReplaceExt,Chr(0),"")
ReplaceExt = Replace(ReplaceExt," ","")
ReplaceExt = Replace(ReplaceExt," ","")
ReplaceExt = Replace(ReplaceExt,Chr(255),"")
ReplaceExt = Replace(ReplaceExt,"'","")
ReplaceExt = Replace(Replace(ReplaceExt,"asp",RepExt),".asp","sp" & RepExt)
ReplaceExt = Replace(Replace(ReplaceExt,"asa",RepExt),".asa","sa" & RepExt)
ReplaceExt = Replace(Replace(ReplaceExt,"aspx",RepExt),".aspx","spx" & RepExt)
ReplaceExt = Replace(Replace(ReplaceExt,"cer",RepExt),".cer","er" & RepExt)
ReplaceExt = Replace(Replace(ReplaceExt,"cdx",RepExt),".cdx","dx" & RepExt)
ReplaceExt = Replace(Replace(ReplaceExt,"htr",RepExt),".htr","tr" & RepExt)
ReplaceExt = Replace(Replace(ReplaceExt,"php",RepExt),".php","hp" & RepExt)
ReplaceExt = Replace(Replace(ReplaceExt,"exe",RepExt),".exe","xe" & RepExt)
End Function

'//產生一個日期字元串
Function DateStr()
Dim iYear,iMonth,iDay,iHour,iMinute,iScond
iYear = Year(Now)
iMonth = Month(Now)
iDay = Day(Now)
iHour = CStr(Hour(Now()))
If Len(iHour) = 1 Then
iHour = "0" & iHour
End If

iMinute = CStr(Minute(Now()))
If Len(iMinute) = 1 Then
iMinute = "0" & iMinute
End If

iScond = CStr(Second(Now()))
If Len(iScond) = 1 Then
iScond = "0" & iScond
End If
DateStr = iYear & iMonth & iDay & iHour & iMinute & iScond
End Function

'//生成指定位數的字元
Function rndStr(strLong)
Dim tempStr
Randomize
Do while Len(rndStr) < strLong
tempStr = CStr(Chr((57-48)*rnd+48))
rndStr = rndStr & tempStr
Loop
rndStr = rndStr
End Function

'//檢查組件是否安裝
Function IsObjInstalled(ByVal strClassString)
Dim xTestObj,ClsString
On Error Resume Next
IsObjInstalled = False
ClsString = strClassString
Err = 0
Set xTestObj = Server.CreateObject(ClsString)
If Err = 0 Then IsObjInstalled = True
If Err = -2147352567 Then IsObjInstalled = True
Set xTestObj = Nothing
Err = 0
Exit Function
End Function
%>

當然照搬是肯定是不能正常使用的

需要按照你的網站具體修改!!

有什麼需要問的可以在我的貼吧
http://post..com/f?kw=%C1%FA%B7%C9%D1%EF
留言

我的博客
http://hi..com/%D6%B1%B5%BD%D3%F6%B5%BD%B7%EF%C7%E1%CE%E8
留言
或加我 QQ:284610811

熱點內容
什麼手機安卓系統80 發布:2024-04-20 21:37:29 瀏覽:378
浙江萬里的伺服器地址 發布:2024-04-20 21:16:59 瀏覽:406
ndklinux下載 發布:2024-04-20 21:05:22 瀏覽:565
王者榮耀解壓資源包97 發布:2024-04-20 20:46:10 瀏覽:396
蘋果手機沒有密碼怎麼打開 發布:2024-04-20 20:45:25 瀏覽:92
如何用濃硝酸配置百分之2的硝酸 發布:2024-04-20 20:44:39 瀏覽:796
微信商城java源碼下載 發布:2024-04-20 20:27:35 瀏覽:121
用友軟體sql 發布:2024-04-20 20:10:01 瀏覽:933
python倒著循環 發布:2024-04-20 20:09:52 瀏覽:759
雲伺服器遠程電腦版 發布:2024-04-20 20:09:12 瀏覽:259