Uploader

thenewprogrammer

New Member
Messages
45
Reaction score
0
Points
0
Hey, im using a uploader that works fine but instead of calling another page to test conditions i want to call a specific function. The function i want to call works perfectly fine if i load and call it normally but in this code it screws up.

This is the complete uploader
http://webdeveloperplus.com/jquery/multiple-file-upload-with-progress-bar-using-jquery/

The place to call it is in the begining

Code:
1.$(function(){   
2.    $('#swfupload-control').swfupload({   
3.        upload_url: "upload-file.php",   
4.        file_post_name: 'uploadfile',   
5.        file_size_limit : "1024",   
6.        file_types : "*.jpg;*.png;*.gif",   
7.        file_types_description : "Image files",   
8.        file_upload_limit : 5,   
9.        flash_url : "js/swfupload/swfupload.swf",   
10.        button_image_url : 'js/swfupload/wdp_buttons_upload_114x29.png',   
11.        button_width : 114,   
12.        button_height : 29,   
13.        button_placeholder : $('#button')[0],   
14.        debug: false  
15.    })
On line 3, i want to change that to call function uploadtesting($user). I tryed changing it to that instead of .php page however nnothing comes up when i upload file. I tryed adding php tags around it and it shows me all the javascript of the uploader on the page.I just wantto change what is is caled when a file is uploaded to a function instead of php page.
 
Last edited:

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
You've got a major conceptual problem. You can only call functions within the current execution of a script. Once the page has been generated and sent to the client, the script exits. Clients (thankfully) have no way of directly executing functions in a script on a server; they can only make HTTP requests. Technologies like SOAP and XML-RPC allow for remote execution by using special client & server support that packages a call into an HTTP conversation. Some implementations (e.g. Python's CGIXMLRPCRequestHandler and xmlrpclib) are almost completely transparent layers, appearing as normal method and function calls, though under the hood they still rely on HTTP.

In your case, you'll either need to pass extra parameters to the script specifying which action to take or write another script that calls the desired function, and POST to the new script. Extra credit if you apply REST ([2]) principles. This is easy to do after the fact using mod_rewrite, which lets you have separate public and private URIs (according to REST principles, public URIs should be names, not verbs or phrases with verbs, but verbs are very useful within scripts).

For example, suppose we want public URIs like "/file/name", "/file/name?test", but private URIs of "/path/to/upload.php?name=name" and "/path/to/upload.php?name=name&action=test". In .htaccess, put:
Code:
RewriteEngine on

# POSTed files get sent to upload.php
RewriteCond %{REQUEST_METHOD} =POST
# if there's a "test" or "save" in the query string, pass it to upload.php as the action.
# "save" is included to illustrate how to add additional actions (separated by "|") but
# is otherwise unnecessary.
RewriteCond %{QUERY_STRING} ((^|&)(test|save)(&|$))
# note: if this rewrite happens, next rule is skipped. [L] flag might also be appropriate
RewriteRule ^/?file/([^/]*) /path/to/upload.php?name=$1&action=%3 [QSA,S]

#Handles the case when no action is specified
RewriteCond %{REQUEST_METHOD} =POST
RewriteRule ^/?file/([^/]*) /path/to/upload.php?name=$1 [QSA,S]

In upload.php, put something like
PHP:
...
if ( ! (isset($_REQUEST['action'] && $_REQUEST['action']) ) {
    $_REQUEST['action'] = 'save'; // default action
}
function perform($action, $user) {
    switch ($action) {
    case 'test':
        uploadtesting($user);
        break;
    case 'save':
        saveFileFor($user);
        break;
    default:
        throw new Exception("Unknown action: '$action'. Either the developer screwed up, or you're being naughty.");
    }
}
// $user is set previously
perform($_REQUEST['action'], $user);
As an aside, note how some of the code almost reads as English? "perform action", "perform requested action".
 
Top