Thread: trying to read a file using cgi scripting and C

  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*
    (1âi    ^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*
    (1âi    ^L^LE8ê
    
    
    
    EASTMAN KODAK COMPANYKODAK EASYSHARE C613 ZOOM DIGITAL CAMERAààKODAK EASYSHARE C613 ZOOM DIGITAL CAMERA'òú"'0221
    *2
    :B
    J|1¸R..0100....
    àÁ..9îhiµ..E«1¥½PÛÆzqâ¬çN+ùR¨Ó±/a¸Tr1..Hä)LJÉõíS®[jJÈH¤eÚÙ"æ¢þ2I¢z½'È9Å>$Ó        ÏøÁíÅJØwIjlÒ¤òÀÆ
                  Ön¸ÏÖpx5<²±¹Ç8¨BHÍY·ÿZ£tQ,Ð..)Û*HÛp¤d,ÀqR*m&ÔÖWHW;Fìtõ¨Ä¥»T¯ÓßAQM4FIÁ
                                                                                      5Hî#}sZE«398ó7UòsÇ..Y##ØÔ~W4àÒZ$õæ¥v&£õÀÍ:Pö¨£}Iò½OÿÙ
    ------------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 int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    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*
    (1âi    ^L^LE8ê
    
    
    
    EASTMAN KODAK COMPANYKODAK EASYSHARE C613 ZOOM DIGITAL CAMERAààKODAK EASYSHARE C613 ZOOM DIGITAL CAMERA'òú"'0221
    *2
    :B
    J|1¸R..0100....
    àÁ..9îhiµ..E«1¥½PÛÆzqâ¬çN+ùR¨Ó±/a¸Tr1..Hä)LJÉõíS®[jJÈH¤eÚÙ"æ¢þ2I¢z½'È9Å>$Ó        ÏøÁíÅJØwIjlÒ¤òÀÆ
                  Ön¸ÏÖpx5<²±¹Ç8¨BHÍY·ÿZ£tQ,Ð..)Û*HÛp¤d,ÀqR*m&ÔÖWHW;Fìtõ¨Ä¥»T¯ÓßAQM4FIÁ
                                                                                      5Hî#}sZE«398ó7UòsÇ..Y##ØÔ~W4àÒZ$õæ¥v&£õÀÍ:Pö¨£}Iò½OÿÙ
    ------------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.

  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, 03: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, 04:23 PM

Tags for this Thread