Go4Expert

Go4Expert (http://www.go4expert.com/)
-   PHP (http://www.go4expert.com/forums/php/)
-   -   PHP uploader script using fsockopen (http://www.go4expert.com/forums/php-uploader-script-using-fsockopen-t26249/)

Darker 10Jul2011 02:47

PHP uploader script using fsockopen
 
1 Attachment(s)
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.

pein87 12Jul2011 07:54

Re: PHP uploader script using fsockopen
 
I see a couple mistakes.

PHP Code:

$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 Code:

$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 Code:

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 Code:

$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 Code:

$value[0

it should just be

PHP Code:

$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.htm...=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/phpc..._emulator_52-7 and curl function guide http://php.net/manual/en/ref.curl.php

Best of luck to you mate.

Darker 14Jul2011 23:31

Re: PHP uploader script using fsockopen
 
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.

pein87 15Jul2011 15:24

Re: PHP uploader script using fsockopen
 
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 Code:

if(array_key_exists("path",$url))
{
//code here
}
if(
array_key_exists("url",$url))
{
//code here


PHP Code:

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 Code:

$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.

Darker 15Jul2011 21:56

Re: PHP uploader script using fsockopen
 
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.

Darker 15Jul2011 21:57

Re: PHP uploader script using fsockopen
 
: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.

pein87 17Jul2011 05:49

Re: PHP uploader script using fsockopen
 
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 17Jul2011 15:52

Re: PHP uploader script using fsockopen
 
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)


All times are GMT +5.5. The time now is 05:30.