Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Jun 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Search a text into a local file

    Hi at all
    in order to upload a local file to the server I insert into my script a filter to detect NON images files.
    Infact at first time I want to upload jpg and gif images only.
    But now is borned a new big problem.
    User want upload medical DICOM files.
    DICOM files have not extension and are like a zip file containing incapsulated images and text.
    I know that the DICOM files has to contain text "DICM" at the offset 0x80, so that tags start at the offset 0x84 of file.
    I need to know if it is possible to read into a local file of the user to search "DICM" text at the offset 0x80 of the files selcting by the user using my form to upload like following:
    <form id="uploadForm" name="uploadForm" action="pld.php" method="post" enctype="multipart/form-data">
    <input id="imageInput" type="file" name="userfile" size="66" onchange="chackimageFile();" />
    <input type="submit" value="upload file">
    </forn>

    Thank you very much in advance

  • #2
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,296
    Thanks
    10
    Thanked 584 Times in 565 Posts
    Code:
    function chackimageFile(){
      var file=imageInput.files[0];
      var fr=new FileReader;
      fr.onload=function(e){
        if( e.target.result !== "DICM" ){
              imageInput.value=null;
              alert("Only DICM files are allowed, please try again");
              imageInput.focus();
        }
        
     };
     fr.readAsBinaryString(file.slice(128,132));
    }


    also, you can cut down on mistakes using an accept parameter:

    Code:
    <input id=imageInput type=file accept=".dcm,image/dicm" onchange="chackimageFile()" />
    Last edited by rnd me; 06-14-2013 at 08:33 PM.
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/5/28) IE7:0.1, IE8:5.3, IE11:8.4, IE9:3.2, IE10:3.2, FF:18.2, CH:46, SF:7.9, NON-MOUSE:32%

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,189
    Thanks
    75
    Thanked 4,341 Times in 4,307 Posts
    But for true safety, *ALSO* perform a content test on the server, in your PHP code.

    So even if a bogus file gets uploaded, your PHP code will detect that and simply refuse to store it on the server.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •