Is it possible to obtain file upload information in php

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post by josh »

Well my method does NOT involve submitting the file to the iframe, build your form like normal, except instead of placing a submit button at the end place a normal button and have it call javascript:

Code: Select all

<script>
<!--
// Toggle your div layer's visibility or whatever you want to do

// Now submit your form to begin uploading the file
document.myForm.submit();

// You will generate a token from php and put it in a hidden form field, this way you can associate the RPC request with the right user
token = document.myForm.token;

// Your RPC call
document.myIFrame.location = 'http://example.com/rpc.php?token=' + token;

function updateStatus(msg) {
    // This code will output msg to the div layer
}
-->
</script>
rpc.php would accept the token, it would lookup the bytes uploaded so far and would output some javascript to
1) make a parent call to the page where you are uploading the file from, calling the updateStatus() command, this call will output status information to the div layer, (for example it could output the bytes uploaded so far)
2) it would output a meta refresh tag to refresh itself in 1 second (when refreshing it would also re-send the token)


As for getting rpc.php to stat the filesize of the file you are uploading, you are on your own on that one.

Once you get the above working you could use some other method, like feyd suggested for obtaining the TOTAL file size, from there you could dispaly as detailed information such as upload progress.

Good luck, and if you get this thing working let me know!
Fourtet
Forum Commoner
Posts: 29
Joined: Fri Sep 02, 2005 5:55 pm

Post by Fourtet »

If I get this thing working with PHP standard I think I'll be famous. :lol:

Seriously, your logic is great I just don't think it's possible. I think when you submit the form the file uploads, now you can return the tmp name to the callback from php, but once you have the tmp name and send it back to php there is no way in php to get information about the process of that file upload.
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post by josh »

An option always is just use a little bit of perl. Sort of a "perl-PHP hybrid uploader"
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

I've FIGURED IT OUT.

A PURE PHP FILE UPLOAD PROGRESS BAR.

I just choose not to share.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
Fourtet
Forum Commoner
Posts: 29
Joined: Fri Sep 02, 2005 5:55 pm

Post by Fourtet »

Perl is evil. :(

May have to resort to it though, will see how it goes. Blast, got to set it up on apache now. :roll:

Thanks for your advice jsh.
User avatar
bokehman
Forum Regular
Posts: 509
Joined: Wed May 11, 2005 2:33 am
Location: Alicante (Spain)

Post by bokehman »

It is not possible solely with PHP. Your best bet is the javascript option. I have posted a demo here. If you like it all you need to do is copy the source. http://myhomewebserver.co.uk/loading
Fourtet
Forum Commoner
Posts: 29
Joined: Fri Sep 02, 2005 5:55 pm

Post by Fourtet »

Boke it does nothing server side. Surely the whole point of the thread is that it would be useful if we could offer download time /percentage stats etc.


I've been trying to use the excellent perl/php hybrid code at raitha.com - and adapting it to use an embedded iframe instead of a pop up, it doesn't seem to work though, it creates the iframe on clicking submit but doesn't write the prog bar to it, but it works fine when using the pop up function.

Html

Code: Select all

<form  enctype="multipart/form-data" action="/cgi-bin/upload.cgi?sid=<? echo $sid; ?>" method="post">


<table border="0" cellpadding="10" align="center">
<tr><td width="35%" valign="top"><p>
Use this form to upload some files and check out the functionality of the uploader.
The total file size should be less than 5MB or it will be rejected. 
</p>
<p>If you use a
file that is too small the progress bar may not have enough time to display itself,
specially if you are on a high speed connection.
</p>
<table class="newboxTable">
	<tr><td class="newboxTitle"  align="center">Note</td></tr>
	<tr><td>
		The speed of upload has been throttled for the health of the server.
	</td></tr>
	</table>
</td>
<td  valign="top">
	<table border=0 align="left" cellpadding=3>
	<tr><td><input type="file" name="file[0]"></td></tr>
	<tr><td colspan=2 align="center">
		<input type="hidden" name="sessionid" value="<?= $sid ?>">
		<input type="button" value="Send" onClick="postIt();">
		<!-- uncomment the following to test with out the progress bar -->
		<!input type="submit" value="Send">
	</td></tr></table>
</td>
</tr></table>

</form>

<IFRAME id="iframe" SRC="" TITLE="The Famous Recipe">
</IFRAME>
JS

Code: Select all

function popUP(mypage, myname, w, h, scroll, titlebar)
{

	var winl = (screen.width - w) / 2;
	var wint = (screen.height - h) / 2;
	winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+',scrollbars='+scroll+',resizable'
	win = window.open(mypage, myname, winprops)
	if (parseInt(navigator.appVersion) >= 4) {
		win.window.focus();
	}
}

function postIt()
{

	if(check_types() == false)
	{
		return false;
	}
	baseUrl = postLocation;
	sid = document.forms[0].sessionid.value;
	iTotal = escape("-1");
	baseUrl += "?iTotal=" + iTotal;
	baseUrl += "&iRead=0";
	baseUrl += "&iStatus=1";
	baseUrl += "&sessionid=" + sid;

	//popUP(baseUrl,"Uploader",460,262,false,false);
	
		newi = document.createElement("iframe"); 
		newi.id = "newi";		
		document.body.appendChild(newi); 
		newi.src = baseUrl;
	
	document.forms[0].submit();
}
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post by josh »

Kinda digging up an old thread, but what if you wrote a simple http server using PHP's socket support, and had that running (as a shell script, not cgi), you could have your little server listen on port 8080, then when you do your file uploads just set the action to "http://example.com:8080", then your PHP web server would just read the http request, therefore achieving raw header access!
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

it could work, but you'd have to find a host that allows permanently loaded shell scripts ;) .. I don't remember many existing these days (that are open to the public)
Post Reply