trying to read a file using cgi scripting and C

This is a discussion on trying to read a file using cgi scripting and C within the C Programming forums, part of the General Programming Boards category; i have a flex interface ... I have a program that reads data from standard in and writes to a ...

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    3

    trying to read a file using cgi scripting and C

    i have a flex interface ...
    I have a program that reads data from standard in and writes to a file..
    Very simple. The problem is..
    when i try to access the file with a url the .jpg file gives me trash and not the picture...

    I am not exactly sure what i am doing wrong.. I have 2 post...
    i will use the code tag for both...

    The info in the image file...

    Code:
    # more  /opt/moneylikes/tmp/*  
    ::::::::::::::
    /opt/moneylikes/tmp/file
    ::::::::::::::
    ------------cH2gL6gL6GI3KM7ei4ae0gL6KM7ae0
    Content-Disposition: form-data; name="Filename"
    
    Botanical Garden 239.JPG
    ------------cH2gL6gL6GI3KM7ei4ae0gL6KM7ae0
    Content-Disposition: form-data; name="Filedata"; filename="Botanical Garden 239.JPG"
    Content-Type: application/octet-stream
    
    PHExifMM*
    (1i    ^L^LE8
    # mv  /opt/moneylikes/tmp/file /opt/moneylikes/tmp/file.jpg
    # more  /opt/moneylikes/tmp/*                              
    ------------cH2KM7Ij5Ef1ae0cH2ae0KM7ei4Ef1
    Content-Disposition: form-data; name="Filename"
    
    Botanical Garden 239.JPG
    ------------cH2KM7Ij5Ef1ae0cH2ae0KM7ei4Ef1
    Content-Disposition: form-data; name="Filedata"; filename="Botanical Garden 239.JPG"
    Content-Type: application/octet-stream
    
    PHExifMM*
    (1i    ^L^LE8
    
    
    
    EASTMAN KODAK COMPANYKODAK EASYSHARE C613 ZOOM DIGITAL CAMERAKODAK EASYSHARE C613 ZOOM DIGITAL CAMERA'"'0221
    *2
    :B
    J|1R..0100....
    ..9hi..E1PzqN+Rӱ/aTr1..H)LJS[jJHe"2Iz'9>$        JwIjlҤ
                  npx5<8BHYZtQ,..)*Hpd,qR*m&WHW;FtĥTAQM4FI
                                                                                      5H#}sZE3987Us..Y##~W4Z$v&:P}IO
    ------------cH2KM7Ij5Ef1ae0cH2ae0KM7ei4Ef1
    Content-Disposition: form-data; name="Upload"
    
    Submit Query
    ------------cH2KM7Ij5Ef1ae0cH2ae0KM7ei4Ef1

    Source code...
    Code:
    main() {
    
    //char *querystring;
    int querylen;
    char *line;
    FILE *fp;
    FILE *fp2;
    
    if ( getenv("CONTENT_LENGTH") != NULL ) {
            querylen=atoi(getenv("CONTENT_LENGTH"));
            line=malloc(querylen*512);
            //querystring=malloc(querylen*512);
    
    } else {
            querylen=10240;
            line=malloc(querylen*512);
            //querystring=malloc(querylen*512);
    }
    
    char querystring[querylen];
    
    fread ( querystring, querylen, 1, stdin );
    printf("Content-type: text/plain\n\n");
    fp=fopen("/opt/moneylikes/tmp/file", "wb+");
    fwrite(querystring, querylen, 1, fp);
    
    exit(0);
    }
    I want to say thanks in advanced to anyone who can help me fill in the missing info..
    I am pretty sure it is how i handle the headder info with the CGI.. i am just not sure what exactly it is i am doing wrong..

  2. #2
    Registered User
    Join Date
    May 2011
    Posts
    3
    sorry.. Not sure i stated clearly what i am doing...... files are uploaded via a interface using flex.. flex sends the selected picture or video file to the url noted in the code...

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,752
    Read about MIME -> MIME - Wikipedia, the free encyclopedia

    Code:
    Content-Disposition: form-data; name="Filename"
    
    Botanical Garden 239.JPG
    ------------cH2KM7Ij5Ef1ae0cH2ae0KM7ei4Ef1
    Content-Disposition: form-data; name="Filedata"; filename="Botanical Garden 239.JPG"
    Content-Type: application/octet-stream
    
    PHExifMM*
    (1i    ^L^LE8
    
    
    
    EASTMAN KODAK COMPANYKODAK EASYSHARE C613 ZOOM DIGITAL CAMERAKODAK EASYSHARE C613 ZOOM DIGITAL CAMERA'"'0221
    *2
    :B
    J|1R..0100....
    ..9hi..E1PzqN+Rӱ/aTr1..H)LJS[jJHe"2Iz'9>$        JwIjlҤ
                  npx5<8BHYZtQ,..)*Hpd,qR*m&WHW;FtĥTAQM4FI
                                                                                      5H#}sZE3987Us..Y##~W4Z$v&:P}IO
    ------------cH2KM7Ij5Ef1ae0cH2ae0KM7ei4Ef1
    The blue stuff is your actual JPG data, the rest is some MIME decoration you need to detect and strip off.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    May 2011
    Posts
    3

    update

    ok.. i tried that ...
    my file is much smaller...

    i still think it has to do with the encoding or something..

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Basic file read, file write program
    By starwarsyeah in forum C++ Programming
    Replies: 5
    Last Post: 02-28-2011, 03:23 PM
  2. Open a file, read it ... and ... read it again
    By Tiago in forum C Programming
    Replies: 1
    Last Post: 04-17-2010, 04:32 AM
  3. File Handling -Read write and edit a file
    By aprop in forum C Programming
    Replies: 3
    Last Post: 02-27-2010, 02:01 PM
  4. How can I know the actual bytes read in a file read
    By pliang in forum C++ Programming
    Replies: 1
    Last Post: 06-08-2005, 05:23 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21