Visualizzazione dei risultati da 1 a 7 su 7
  1. #1
    Utente di HTML.it
    Registrato dal
    Aug 2001
    Messaggi
    162

    Upload foto e database (problemi con la path)

    Ciao a tutti, seguendo un tutorial su www.dmxzone.com/go?221 sono riuscito a fare l'upload di un file inserendo nello stesso tempo i dati in un database.
    Il problema è che il campo FILENAME del database viene riempito con il nome del file trasmesso es. foto.jpg.
    Io vorrei che venisse compilato con l'intero percorso. ../public/foto/foto.jpg

    Esiste una funzione, in grado di fare questo?
    Qual'è il codice da modificare ?

    Se volete dare uno sguardo al codice questo è il link alla pagina di prova che ho messo appositamente online:
    PAGINA DI PROVA

    CIAO A TUTTI !!
    Chi gode un pò non tribola sempre.

  2. #2
    sarebbe da vedere il codice, non la pagina...

  3. #3
    Utente di HTML.it
    Registrato dal
    Aug 2001
    Messaggi
    162
    Il codice è un po' lungo lo posto su 2 messaggi

    <%@LANGUAGE="VBSCRIPT"%>

    <%
    Sub BuildUploadRequest(RequestBin)
    'Get the boundary
    PosBeg = 1
    PosEnd = InstrB(PosBeg,RequestBin,getByteString(chr(13)))
    if PosEnd = 0 then
    Response.Write "Form was submitted with no ENCTYPE=""multipart/form-data""
    "
    Response.Write "Please correct the form attributes and try again."
    Response.End
    end if
    boundary = MidB(RequestBin,PosBeg,PosEnd-PosBeg)
    boundaryPos = InstrB(1,RequestBin,boundary)
    'Get all data inside the boundaries
    Do until (boundaryPos=InstrB(RequestBin,boundary & getByteString("--")))
    'Members variable of objects are put in a dictionary object
    Dim UploadControl
    Set UploadControl = CreateObject("Scripting.Dictionary")
    'Get an object name
    Pos = InstrB(BoundaryPos,RequestBin,getByteString("Conte nt-Disposition"))
    Pos = InstrB(Pos,RequestBin,getByteString("name="))
    PosBeg = Pos+6
    PosEnd = InstrB(PosBeg,RequestBin,getByteString(chr(34)))
    Name = getString(MidB(RequestBin,PosBeg,PosEnd-PosBeg))
    PosFile = InstrB(BoundaryPos,RequestBin,getByteString("filen ame="))
    PosBound = InstrB(PosEnd,RequestBin,boundary)
    'Test if object is of file type
    If PosFile<>0 AND (PosFile<PosBound) Then
    'Get Filename, content-type and content of file
    PosBeg = PosFile + 10
    PosEnd = InstrB(PosBeg,RequestBin,getByteString(chr(34)))
    FileName = getString(MidB(RequestBin,PosBeg,PosEnd-PosBeg))
    FileName = Mid(FileName,InStrRev(FileName,"\")+1)
    'Add filename to dictionary object
    UploadControl.Add "FileName", FileName
    Pos = InstrB(PosEnd,RequestBin,getByteString("Content-Type:"))
    PosBeg = Pos+14
    PosEnd = InstrB(PosBeg,RequestBin,getByteString(chr(13)))
    'Add content-type to dictionary object
    ContentType = getString(MidB(RequestBin,PosBeg,PosEnd-PosBeg))
    UploadControl.Add "ContentType",ContentType
    'Get content of object
    PosBeg = PosEnd+4
    PosEnd = InstrB(PosBeg,RequestBin,boundary)-2
    Value = FileName
    ValueBeg = PosBeg-1
    ValueLen = PosEnd-Posbeg
    Else
    'Get content of object
    Pos = InstrB(Pos,RequestBin,getByteString(chr(13)))
    PosBeg = Pos+4
    PosEnd = InstrB(PosBeg,RequestBin,boundary)-2
    Value = getString(MidB(RequestBin,PosBeg,PosEnd-PosBeg))
    ValueBeg = 0
    ValueEnd = 0
    End If
    'Add content to dictionary object
    UploadControl.Add "Value" , Value
    UploadControl.Add "ValueBeg" , ValueBeg
    UploadControl.Add "ValueLen" , ValueLen
    'Add dictionary object to main dictionary
    UploadRequest.Add name, UploadControl
    'Loop to next object
    BoundaryPos=InstrB(BoundaryPos+LenB(boundary),Requ estBin,boundary)
    Loop
    End Sub

    'String to byte string conversion
    Function getByteString(StringStr)
    For i = 1 to Len(StringStr)
    char = Mid(StringStr,i,1)
    getByteString = getByteString & chrB(AscB(char))
    Next
    End Function

    'Byte string to string conversion
    Function getString(StringBin)
    getString =""
    For intCount = 1 to LenB(StringBin)
    getString = getString & chr(AscB(MidB(StringBin,intCount,1)))
    Next
    End Function

    Function UploadFormRequest(name)
    on error resume next
    if UploadRequest.Item(name) then
    UploadFormRequest = UploadRequest.Item(name).Item("Value")
    end if
    End Function

    'Process the upload
    UploadQueryString = Replace(Request.QueryString,"GP_upload=true","")
    if mid(UploadQueryString,1,1) = "&" then
    UploadQueryString = Mid(UploadQueryString,2)
    end if

    GP_uploadAction = CStr(Request.ServerVariables("URL")) & "?GP_upload=true"
    If (Request.QueryString <> "") Then
    if UploadQueryString <> "" then
    GP_uploadAction = GP_uploadAction & "&" & UploadQueryString
    end if
    End If

    If (CStr(Request.QueryString("GP_upload")) <> "") Then
    GP_redirectPage = ""
    If (GP_redirectPage = "") Then
    GP_redirectPage = CStr(Request.ServerVariables("URL"))
    end if

    RequestBin = Request.BinaryRead(Request.TotalBytes)
    Dim UploadRequest
    Set UploadRequest = CreateObject("Scripting.Dictionary")
    BuildUploadRequest RequestBin

    GP_keys = UploadRequest.Keys
    for GP_i = 0 to UploadRequest.Count - 1
    GP_curKey = GP_keys(GP_i)
    'Save all uploaded files
    if UploadRequest.Item(GP_curKey).Item("FileName") <> "" then
    GP_value = UploadRequest.Item(GP_curKey).Item("Value")
    GP_valueBeg = UploadRequest.Item(GP_curKey).Item("ValueBeg")
    GP_valueLen = UploadRequest.Item(GP_curKey).Item("ValueLen")

    if GP_valueLen = 0 then
    Response.Write "An error has occured saving uploaded file!

    "
    Response.Write "Filename: " & Trim(GP_curPath) & UploadRequest.Item(GP_curKey).Item("FileName") & "
    "
    Response.Write "File does not exists or is empty.
    "
    Response.Write "Please correct and try again"
    response.End
    end if

    'Create a Stream instance
    Dim GP_strm1, GP_strm2
    Set GP_strm1 = Server.CreateObject("ADODB.Stream")
    Set GP_strm2 = Server.CreateObject("ADODB.Stream")

    'Open the stream
    GP_strm1.Open
    GP_strm1.Type = 1 'Binary
    GP_strm2.Open
    GP_strm2.Type = 1 'Binary

    GP_strm1.Write RequestBin
    GP_strm1.Position = GP_ValueBeg
    GP_strm1.CopyTo GP_strm2,GP_ValueLen

    'Create and Write to a File
    GP_curPath = Request.ServerVariables("PATH_INFO")
    GP_curPath = Trim(Mid(GP_curPath,1,InStrRev(GP_curPath,"/")) & "public/foto")
    if Mid(GP_curPath,Len(GP_curPath),1) <> "/" then
    GP_curPath = GP_curPath & "/"
    end if
    on error resume next
    GP_strm2.SaveToFile Trim(Server.mappath(GP_curPath))& "\" & UploadRequest.Item(GP_curKey).Item("FileName"),2
    if err then
    Response.Write "An error has occured saving uploaded file!

    "
    Response.Write "Filename: " & Trim(GP_curPath) & UploadRequest.Item(GP_curKey).Item("FileName") & "
    "
    Response.Write "Maybe the destination directory does not exist, or you don't have write permission.
    "
    Response.Write "Please correct and try again"
    err.clear
    response.End
    end if
    end if
    next

    '*** GP NO REDIRECT
    end if
    if UploadQueryString <> "" then
    UploadQueryString = UploadQueryString & "&GP_upload=true"
    else
    UploadQueryString = "GP_upload=true"
    end if

    %>
    <%
    ' *** Edit Operations: (Modified for File Upload) declare variables

    MM_editAction = CStr(Request.ServerVariables("URL")) 'MM_editAction = CStr(Request("URL"))
    If (UploadQueryString <> "") Then
    MM_editAction = MM_editAction & "?" & UploadQueryString
    End If

    ' boolean to abort record edit
    MM_abortEdit = false

    ' query string to execute
    MM_editQuery = ""
    %>
    <%
    ' *** Insert Record: (Modified for File Upload) set variables

    If (CStr(UploadFormRequest("MM_insert")) <> "") Then

    MM_editConnection = MM_uploadconn_STRING
    MM_editTable = "upload"
    MM_editRedirectUrl = "view.asp"
    MM_fieldsStr = "fmname|value|fmemail|value|fmfile|value|fmtipolog ia|value|fmdescrizionebreve|value|fmdescrizione|va lue|fmlocalita|value|fmsiglaprovincia|value|fmregi one|value|fmprezzo|value|fmprezzoeffettivo|value|f mprezzovisualizzato|value|fmvetrina|value|fmaffitt o|value"
    MM_columnsStr = "name|',none,''|email|',none,''|filename|',none,'' |tipologia|',none,''|descrizionebreve|',none,''|de scrizione|',none,''|localita|',none,''|siglaprovin cia|',none,''|regione|',none,''|prezzo|',none,''|p rezzoeffettivo|',none,''|prezzovisualizzato|',none ,''|vetrina|',none,''|affitto|',none,''"

    ' create the MM_fields and MM_columns arrays
    MM_fields = Split(MM_fieldsStr, "|")
    MM_columns = Split(MM_columnsStr, "|")

    ' set the form values
    For i = LBound(MM_fields) To UBound(MM_fields) Step 2
    MM_fields(i+1) = CStr(UploadFormRequest(MM_fields(i)))
    Next

    ' append the query string to the redirect URL
    If (MM_editRedirectUrl <> "" And UploadQueryString <> "") Then
    If (InStr(1, MM_editRedirectUrl, "?", vbTextCompare) = 0 And UploadQueryString <> "") Then
    MM_editRedirectUrl = MM_editRedirectUrl & "?" & UploadQueryString
    Else
    MM_editRedirectUrl = MM_editRedirectUrl & "&" & UploadQueryString
    End If
    End If

    End If
    %>
    Chi gode un pò non tribola sempre.

  4. #4
    Utente di HTML.it
    Registrato dal
    Aug 2001
    Messaggi
    162
    <%
    ' *** Insert Record: (Modified for File Upload) construct a sql insert statement and execute it

    If (CStr(UploadFormRequest("MM_insert")) <> "") Then

    ' create the sql insert statement
    MM_tableValues = ""
    MM_dbValues = ""
    For i = LBound(MM_fields) To UBound(MM_fields) Step 2
    FormVal = MM_fields(i+1)
    MM_typeArray = Split(MM_columns(i+1),",")
    Delim = MM_typeArray(0)
    If (Delim = "none") Then Delim = ""
    AltVal = MM_typeArray(1)
    If (AltVal = "none") Then AltVal = ""
    EmptyVal = MM_typeArray(2)
    If (EmptyVal = "none") Then EmptyVal = ""
    If (FormVal = "") Then
    FormVal = EmptyVal
    Else
    If (AltVal <> "") Then
    FormVal = AltVal
    ElseIf (Delim = "'") Then ' escape quotes
    FormVal = "'" & Replace(FormVal,"'","''") & "'"
    Else
    FormVal = Delim + FormVal + Delim
    End If
    End If
    If (i <> LBound(MM_fields)) Then
    MM_tableValues = MM_tableValues & ","
    MM_dbValues = MM_dbValues & ","
    End if
    MM_tableValues = MM_tableValues & MM_columns(i)
    MM_dbValues = MM_dbValues & FormVal
    Next
    MM_editQuery = "insert into " & MM_editTable & " (" & MM_tableValues & ") values (" & MM_dbValues & ")"

    If (Not MM_abortEdit) Then
    ' execute the insert
    Set MM_editCmd = Server.CreateObject("ADODB.Command")
    MM_editCmd.ActiveConnection = MM_editConnection
    MM_editCmd.CommandText = MM_editQuery
    MM_editCmd.Execute
    MM_editCmd.ActiveConnection.Close

    If (MM_editRedirectUrl <> "") Then
    Response.Redirect(MM_editRedirectUrl)
    End If
    End If

    End If
    %>
    <%
    Dim RsUpload
    Dim RsUpload_cmd
    Dim RsUpload_numRows

    Set RsUpload_cmd = Server.CreateObject ("ADODB.Command")
    RsUpload_cmd.ActiveConnection = MM_uploadconn_STRING
    RsUpload_cmd.CommandText = "SELECT * FROM upload"
    RsUpload_cmd.Prepared = true

    Set RsUpload = RsUpload_cmd.Execute
    RsUpload_numRows = 0
    %>
    <html>
    <head>
    <title>PureASPUpload</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <script language="JavaScript">
    <!--

    function getFileExtension(filePath) { //v1.0
    fileName = ((filePath.indexOf('/') > -1) ? filePath.substring(filePath.lastIndexOf('/')+1,filePath.length) : filePath.substring(filePath.lastIndexOf('\\')+1,fi lePath.length));
    return fileName.substring(fileName.lastIndexOf('.')+1,fil eName.length);
    }

    function checkFileUpload(form,extensions) { //v1.0
    document.MM_returnValue = true;
    if (extensions && extensions != '') {
    for (var i = 0; i<form.elements.length; i++) {
    field = form.elements[i];
    if (field.type.toUpperCase() != 'FILE') continue;
    if (field.value == '') {
    alert('File is required!');
    document.MM_returnValue = false;field.focus();break;
    }
    if (extensions.toUpperCase().indexOf(getFileExtension (field.value).toUpperCase()) == -1) {
    alert('This file is not allowed for uploading!');
    document.MM_returnValue = false;field.focus();break;
    } } }
    }
    //-->
    </script>
    </head>
    <body bgcolor="#FFFFFF" text="#000000" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">






    <table width="90%" border="0" cellspacing="2" cellpadding="2" align="center">
    <tr>
    <td>
    <form name="fmupload" method="POST" action="<%=MM_editAction%>" enctype="multipart/form-data" onSubmit="checkFileUpload(this,'GIF,JPG,JPEG,BMP,P NG');return document.MM_returnValue">
    <table width="100%" border="0" cellspacing="0" cellpadding="0">
    <tr>
    <td></td>
    <td>
    <input name="fmname" type="text" size="10">
    </td>
    </tr>
    <tr>
    <td></td>
    <td><label for="fmtipologia"></label>
    <select name="fmtipologia" id="fmtipologia">
    <option selected>----------------</option>
    <option>Appartamento</option>
    <option>Ville e casali</option>
    <option>Terreno</option>
    <option>Altro</option>
    </select></td>
    </tr>
    <tr>
    <td></td>
    <td><textarea name="fmdescrizionebreve" cols="90" rows="5" id="fmdescrizionebreve"></textarea></td>
    </tr>
    <tr>
    <td></td>
    <td><textarea name="fmdescrizione" cols="90" rows="5" id="fmdescrizione"></textarea></td>
    </tr>
    <tr>
    <td></td>
    <td><input name="fmlocalita" type="text" id="fmlocalita"></td>
    </tr>
    <tr>
    <td></td>
    <td><input name="fmsiglaprovincia" type="text" id="fmsiglaprovincia"></td>
    </tr>
    <tr>
    <td></td>
    <td><input name="fmregione" type="text" id="fmregione"></td>
    </tr>
    <tr>
    <td></td>
    <td><input name="fmprezzo" type="text" id="fmprezzo"></td>
    </tr>
    <tr>
    <td></td>
    <td><input name="fmprezzoeffettivo" type="text" id="fmprezzoeffettivo"></td>
    </tr>
    <tr>
    <td></td>
    <td><input name="fmprezzovisualizzato" type="text" id="fmprezzovisualizzato"></td>
    </tr>
    <tr>
    <td></td>
    <td><label for="fmvetrina"></label>
    <select name="fmvetrina" id="fmvetrina">
    <option selected>---</option>
    <option>SI</option>
    <option>NO</option>
    </select></td>
    </tr>
    <tr>
    <td></td>
    <td><select name="fmaffitto" id="fmaffitto">
    <option selected>---</option>
    <option>SI</option>
    <option>NO</option>
    </select></td>
    </tr>
    <tr>
    <td><input name="fmemail" type="hidden" id="fmemail" value="info@xxx.it"></td>
    <td></td>
    </tr>
    <tr>
    <td> Foto</td>
    <td>
    <input type="file" name="fmfile">


    Vorrei che questo campo scrivesse nel database, non solo il nome del file ma anche il percorso. In questo caso ../public/foto
    </td>
    </tr>
    <tr>
    <td></td>
    <td>
    <input type="submit" name="fmsubmit" value="Upload">
    </td>
    </tr>
    </table>
    <input type="hidden" name="MM_insert" value="true">
    </form>
    </td>
    </tr>
    </table>
    </body>
    </html>
    <%
    RsUpload.Close()
    %>
    Chi gode un pò non tribola sempre.

  5. #5
    ti dirò la verità: io non mi metto a leggere TUTTO il codice (ma non è detto che non lo faccia qualcun altro). saresti così gentile da postare solo la porzione dove viene fatto il salvataggio?

  6. #6
    bhè ma se quello ke inserisci ora (es. nome = "foto.jpg") è un campo testo basta che nella query fai

    .. ="../public/foto/"&nome&" ..

    ho paura delle ripercussioni di questo che ho scritto -.-''

    PS non ho letto e non leggerò mai tutto quel programma postato

  7. #7
    Utente di HTML.it
    Registrato dal
    Aug 2001
    Messaggi
    162
    E' questa la parte di codice che trasmette il file e ne scrive il percorso nel database?
    In caso affermativo, che modifiche devo apportare per ottenere il risultato spiegato in precedenza?


    'Create and Write to a File

    GP_curPath = Request.ServerVariables("PATH_INFO")
    GP_curPath = Trim(Mid(GP_curPath,1,InStrRev(GP_curPath,"/")) & "public/foto")
    if Mid(GP_curPath,Len(GP_curPath),1) <> "/" then
    GP_curPath = GP_curPath & "/"
    end if
    on error resume next
    GP_strm2.SaveToFile Trim(Server.mappath(GP_curPath))& "\" & UploadRequest.Item(GP_curKey).Item("FileName"),2
    if err then
    Response.Write "An error has occured saving uploaded file!

    "
    Response.Write "Filename: " & Trim(GP_curPath) & UploadRequest.Item(GP_curKey).Item("FileName") & "
    "
    Response.Write "Maybe the destination directory does not exist, or you don't have write permission.
    "
    Response.Write "Please correct and try again"
    Chi gode un pò non tribola sempre.

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.