PDA

View Full Version : File Upload through HTTP.Submit Action


Ninj
04-13-2005, 03:00 AM
I've been trying to use the HTTP.Submit action to upload a file into a Web Server. There were some issues with the HTTP.Submit action before, and it has been resolved as per the release of latest version.

Now my problem is, I can't still use this action to upload the file into the Web Server. Is this really possible with this action or not? I've tried using an HTTP activeX before which achieve the desired result. The activeX was also using HTTP Protocol so I am assuming that this should also be possible with HTTP.Submit.

If it is not possible, this will be a great enhancement for the HTTP.Submit Action.

Thanks,
Ninj

Mark
04-13-2005, 08:55 AM
Hi Ninj,

Perhaps you could post your current action so that everyone here can have a look at it and perhaps make some suggestions?

What size file are you uploading? How is your Web script performing the upload? Is any part of the file being uploaded to your server? Is there an error code being returned by the HTTP.Submit() action? If so what is the error? What does HTTP.GetHTTPErrorInfo() return?

I believe that you said in the past that you have gotten this to work by increasing the timeout time to a very large number, does this still hold true?

Truthfully I do not see why the HTTP.Submit() action would not work in this scenario since, the action is simply passing the data to the web script, which then has to perform the actual upload task. Can you get this action to work on an empty text file?

Ninj
04-13-2005, 09:52 PM
Below is my script:

myValues = { filesfolder="/tu", OpCode=2, filesize=_FileSize, logfile="LogFile.txt", nameoffile="test.txt"};
SubmitFName = HTTP.Submit("http://www.mywebserver.com/cgi-bin/uploadtest.cgi", myValues, SUBMITWEB_POST, 200, 80, nil, nil);

Attached is a copy of the uploadtest.cgi, in order to attach it here I renamed as .txt.

As you have said, I thought that I have made it work before. However, I just noticed that the uploaded file is an empty file with 0 bytes. Therefore, the data was not really sent to the cgi, just the filename it seems.

Actually, I am already using the attached cgi for another application and am trying to make the upload work in TU2 as well without having to create another cgi script. Using a 3rd party HTTP activeX, the cgi script attached is able to upload a file to the server.

Mark
04-14-2005, 08:47 AM
Hi Ninj,

I tried to test this on my side but I kept getting error 2516 ("An internal server error has occurred." HTTP error code 500) when trying to test your script. This is probably because I do not have something needed installed on my server or something like that, but just to make sure do you receive any errors when you test your script?

Try inserting an Application.GetLastError() after the HTTP.Submit() action and test what the results are.

Desmond
04-14-2005, 01:01 PM
Hello Ninj,

From what i can determine with yoru action, you're not actually passing the script any data to write into the file. A web script can't come onto your system and grab a file without permission. If you use that script through IE, IE has permission to access files on your system that you let it, and send them off to the script. Or at least that's how I understand it.

But with TrueUpdate, the script can access only what values you send to it from your lua script. Thoguh i'm not positive, how I would envision a file upload working is as follows:

1. UUencode the file in trueupdate (i'm not entirely sure how to do this)
2. send the following values to your script:
filename - the file, in form filename.ext
filedata - the actual data, the uuencoded string
3. Create a script that accepts those two values, uudecodes the string, and writes the filestream to the specified filename on the server. I haven't tried this, but in php i'd do somethign like this (please keep in mind this hasn't been tried or tested, but is where i'd start):


//save the below code into, say, upload.php and access it from trueupdate, method = post

<?php
//NEED POST VARIABLES:
//filedata: uuencoded string of file contents
//filename: string in form filename.ext

$filedata = convert_uudecode($_POST['filedata']);

if (!($writefilestream = fopen($_POST['filename'], 'w')))
return;

fprintf($writefilestream, "%s", $filedata);

?>


All I did was googled uudecode and php, then writing to a file, and found those functions. Does it work? WHo knows! But this shoudl give you a place to start.

Lemme know how this works out for you. I've never done this, so it's kinda interesting!

Intrigued
04-14-2005, 01:51 PM
Hello Ninj,

From what i can determine with yoru action, you're not actually passing the script any data to write into the file. A web script can't come onto your system and grab a file without permission. If you use that script through IE, IE has permission to access files on your system that you let it, and send them off to the script. Or at least that's how I understand it.

But with TrueUpdate, the script can access only what values you send to it from your lua script. Thoguh i'm not positive, how I would envision a file upload working is as follows:

1. UUencode the file in trueupdate (i'm not entirely sure how to do this)
2. send the following values to your script:
filename - the file, in form filename.ext
filedata - the actual data, the uuencoded string
3. Create a script that accepts those two values, uudecodes the string, and writes the filestream to the specified filename on the server. I haven't tried this, but in php i'd do somethign like this (please keep in mind this hasn't been tried or tested, but is where i'd start):


//save the below code into, say, upload.php and access it from trueupdate, method = post

<?php
//NEED POST VARIABLES:
//filedata: uuencoded string of file contents
//filename: string in form filename.ext

$filedata = convert_uudecode($_POST['filedata']);

if (!($writefilestream = fopen($_POST['filename'], 'w')))
return;

fprintf($writefilestream, "%s", $filedata);

?>


All I did was googled uudecode and php, then writing to a file, and found those functions. Does it work? WHo knows! But this shoudl give you a place to start.

Lemme know how this works out for you. I've never done this, so it's kinda interesting!

I would recommend also if you use that PHP code to "lock" the file. (flock)

Desmond
04-14-2005, 02:15 PM
oooh, that's a good point. to prevent possible errors cause of two attempts to write, right?

Is the code above correct? I'm oober new to PHP, i wasn't sure.

Intrigued
04-14-2005, 07:21 PM
Here is an example of using the flock function from php.net (and a link to the page):

http://us2.php.net/flock

<?php

$fp = fopen("/tmp/lock.txt", "w+");

if (flock($fp, LOCK_EX)) { // do an exclusive lock
fwrite($fp, "Write something here\n");
flock($fp, LOCK_UN); // release the lock
} else {
echo "Couldn't lock the file !";
}

fclose($fp);

?>

Corey
04-14-2005, 07:27 PM
FWIW if a script is hitting a single file with simultaneous writes, it's a planning issue. PHP flock wouldn't cure that because you would still be "losing" writes, i.e. if a script's author is seeking to write two pieces of data at the same time then presumably they also wish to also read them... So in that case you would just use unique temp files in a FIFO cycle. :yes

Note from php.net:

flock() will not work on NFS and many other networked file systems. Check your operating system documentation for more details.

On some operating systems flock() is implemented at the process level. When using a multithreaded server API like ISAPI you may not be able to rely on flock() to protect files against other PHP scripts running in parallel threads of the same server instance!

flock() is not supported on antiquated filesystems like FAT and its derivates and will therefore always return FALSE under this environments (this is especially true for Windows 98 users).

Intrigued
04-14-2005, 08:33 PM
I just wanted to put forth a point with regards to using fopen, as the original poster is. Whether that was the right choice... that's up for discussion and you Corey have more PHP experience than I and have found some good counter points to using fopen.

:yes

Corey
04-14-2005, 08:48 PM
Yes I'm glad you did. This also applies to all scripting, not just PHP. In general writing twice to one file such that it would cause an error is something to plan for beforehand in all scripts/programs which write files. Common methods include using temp files or using a "flag file" to detect when another file is "ready for writing" and a corresponding file queue, etc... :yes

mcme
10-14-2005, 04:01 PM
I started same one in nes thread.