PHP uploader script using fsockopen

Discussion in 'PHP' started by Darker, Jul 9, 2011.

  1. Darker

    Darker New Member

    Joined:
    Jul 9, 2011
    Messages:
    11
    Likes Received:
    0
    Trophy Points:
    0
    Location:
    Czech Republic
    Hello.
    I have written a very simple script to send a multipart POST request to server.
    I don't see any difference between the request send by PHP script and the one send from firefox, but the remote site has empty both $_POST and $_FILES when the request comes from my site.
    I've included the source code.
     

    Attached Files:

  2. pein87

    pein87 Active Member

    Joined:
    Aug 6, 2010
    Messages:
    173
    Likes Received:
    47
    Trophy Points:
    28
    Occupation:
    Web Dev
    Location:
    Limbo
    I see a couple mistakes.

    PHP:
    $url="http://face-book.kvalitne.cz/upload.php";
    $referer=$url;
    $url parse_url($url);
    $host $url['host'];
    $path $url['path'];
    Right the your assigning the host and path variables the values of an associate array that does not exist. You defining the url variable as a string/char and not as an array. You can create the array and assign the indexes values and that part will be solved. Although I did see that part used.

    PHP:
    $ini = array("url" => "http://face-book.kvalitne.cz/upload.php","host" => "http://www.some_url.com","path" => "C:\somepath\here\");
    $referer = $ini['url'];
    $url = parse_url($ini['url']);
    $host = $ini['host'];
    $path = $ini['path'];
    The other problem I see is here

    PHP:
    foreach($data as $name => $value) {
     
    $request.=$boundary."\r\n";
     if(
    is_array($value)){
       
    $request.="Content-Disposition: form-data; name=\"$name\"; filename=\"{$value[1]}\"\r\n";
       
    $request.="Content-Type: {$value[2]}\r\n";
       
    $request.="Content-Length: ".strlen($value[0])."\r\n";
       
    $request.="\r\n";
       
    $request.=$value[0]."\r\n";
     }
     else {
         
    $request.="Content-Disposition: form-data; name=\"$name\"\r\n";
         
    $request.="Content-Length: ".strlen($value)."\r\n";
         
    $request.="\r\n";
         
    $request.=$value."\r\n";
     }
    }
    the part in particular is this

    PHP:
    $value[2]
    Your basically saying give me the second character of this value. When you use the foreach statement with the key value declaration like you did the value variable is the actual value for that key. Since you only have two values and two commented out for whatever reason your only showing two different key value pairs.

    upload => ok
    potvrd => Nahrát

    each time the code is ran the values for that character as given and not the value as a whole. For an example the content could be shown as this:

    for ok
    Code:
    Content-Type: null
    Content-Length: 1
    for Nahrat
    Code:
    Content-Type: h
    Content-Length: 1
    Your going to need to specify the contents type by actually getting the files content type and placing the appropriate value for it. What your doing is saying give me third character of ok which is null, and give me the string length of the character o which is 1. Your not specifying the values correctly and your only showing incomplete values as your headers. Look up acceptable content type and make an array of a function that takes a files extension and returns its content type. Get the string length of the entire text and not just the a specific character. instead of

    PHP:
    $value[0]
    it should just be

    PHP:
    $value
    I'm no expert on this matter(emulating a browser) but I can spot errors in code. Try curl instead because its better and more powerful. There are pure php implementations of curl if your host doesn't allow curl or your local machine doesn't have it. Check this site for the pure php curl http://www.phpclasses.org/search.html?words=curl&x=0&y=0&go_search=1 and this site for content types http://en.wikipedia.org/wiki/Internet_media_type and this one for header responses http://en.wikipedia.org/wiki/List_of_HTTP_header_fields

    edit: link to curl emulator in php http://code.blitzaffe.com/pages/phpclasses/files/libcurl_emulator_52-7 and curl function guide http://php.net/manual/en/ref.curl.php

    Best of luck to you mate.
     
    Last edited: Jul 12, 2011
    Darker and shabbir like this.
  3. Darker

    Darker New Member

    Joined:
    Jul 9, 2011
    Messages:
    11
    Likes Received:
    0
    Trophy Points:
    0
    Location:
    Czech Republic
    Hello, thank you very much, for exploring my (uncomented, sorry) code.
    But both errors werent errors actually. I've written a long explanation yesterday, but the fucki'n storm has shut down electric network I will not wtire it so detailed again, sorry.
    1. st error: parse_url returns array.
    2 nd. error: There are an if(is_array(...)) statement.

    But you've shared some usefull links for me, and I will try to use them and make the script another way.
    So thank you for your time and for wishing me luck.
     
  4. pein87

    pein87 Active Member

    Joined:
    Aug 6, 2010
    Messages:
    173
    Likes Received:
    47
    Trophy Points:
    28
    Occupation:
    Web Dev
    Location:
    Limbo
    I didn't know it returned an array(I've never used it), however it may not have that key. Make sure you first check to make sure its an array and that that array key exists.

    you should use this

    PHP:
    if(array_key_exists("path",$url))
    {
    //code here
    }
    if(
    array_key_exists("url",$url))
    {
    //code here
    }
    PHP:
    foreach($data as $name => $value)
    {
    echo 
    $name ": " $value[0];
    }
    It will show
    Code:
    upload : o
    potvrd: N
    Add the above code to yours(above your foreach loop) and I guarantee it will output the first character of each value and not the full value that your seeking. The whole point of the foreach loop is to iterate through the array without having to use a regular for loop and the whole string counting bit. the $value variable is the data that the $name key holds, i.e if you added the below:

    PHP:
    $array[$name] = $value;
    print_r($array);
    to your code in that foreach loop(either one) you'd make a duplicate array with the same key-value pairing as the original name $array.

    the testing for if its an array in a foreach loop is not needed. You already hard coded it as an array so testing for it to be one is ridiculous, because you already know its an array. If anything you should have tested before hand if its an array because that function will be called every iteration and consume unneeded memory. Php allows for char level variable output and thats why what your doing in the foreach loop is wrong and poor coding in general. Don't take my word though, output that data during iteration and see for yourself the values it gives you.

    As far as getting content types, well you'll need to probably use regex to get all the data after the .(because some formats use 4 characters for their extension instead of 3) or substr(). Compare that value to predefined values and return the appropriate content type.
     
    Last edited: Jul 15, 2011
  5. Darker

    Darker New Member

    Joined:
    Jul 9, 2011
    Messages:
    11
    Likes Received:
    0
    Trophy Points:
    0
    Location:
    Czech Republic
    See here :) jmareda.tk/roboti/uploader.php
    The code is almost (maybe absolutely) the same as uploaded.
    And about the file types: in older PHP versions, I can use file_content_mime_type (or somethink like that) but one of goals of the script was to test, if the professional upload servers are testing the mime type as I do, or they simply use the header.
    Because, as we see here, if the script worked, the header could be faked...

    About the values and arrays:
    I've written the script to work witch array, which contains values and arrays together. It is because now i can know: Its an array - its a file upload. Otherwise, its a post field, which has only name, nothing else.

    Actually, after some more tests, header changes and so on, i've really strong suspition, that there are some server anti hack on my or remote hosting.
     
  6. Darker

    Darker New Member

    Joined:
    Jul 9, 2011
    Messages:
    11
    Likes Received:
    0
    Trophy Points:
    0
    Location:
    Czech Republic
    :D stupid site told me that one URL is too many links. Internet hates me today, facebook also pissed me of. Or fired me out, said better. I'm gonne hack them as a revanche one day.
     
  7. pein87

    pein87 Active Member

    Joined:
    Aug 6, 2010
    Messages:
    173
    Likes Received:
    47
    Trophy Points:
    28
    Occupation:
    Web Dev
    Location:
    Limbo
    So your basically trying to make this an upload script? Why not just create an upload page? What exactly are you trying to upload and to where? Using ajax, and php you could create an intuitive ui with excellence features. If you wanted to send a file why not use curl or heck even ftp functions in a client server system. You could create a simple multi-file upload using simple code. Of you could upload from a folder to a remote server using curl or the ftp functions. I think you need to develop the idea farther and determine exactly what you want this script to do and how you can implement each feature. You could also use this function which works better and is faster. http://www.php.net/manual/en/function.finfo-file.php
    There are so many ways to tackle the task that you've set up but you need to first understand what it is your trying to do. As far as your idea you need a client to receive the file and process it and a server that does the actual labor. Of course you could have multiple clients that use the server but a client server system is probably your best shot. You could also write a p2p system but using php it would be very limited to just being a web or crude cli program.
     
    Darker likes this.
  8. Darker

    Darker New Member

    Joined:
    Jul 9, 2011
    Messages:
    11
    Likes Received:
    0
    Trophy Points:
    0
    Location:
    Czech Republic
    Actually, if i wantet to send files to servers where I have FTP access i would use something else. But once i want to send data somewhere else (like some kiddie upload script) I want to make this.
    And once O realised that its not easy, it became also a bit chalenge for me :)
    Thank you for link to new version of file_mime_type :)
    But actually, I maybe want to do not send mime types with files correctly, to test skils of my frinds (and enemies :D)
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice