TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 05-22-2008, 06:03 PM   #1 (permalink)
The Acquainted
 
EyeDentify's Avatar
 
Join Date: Nov 2007
Location: Sweden
Posts: 106
Thanks: 13
EyeDentify is on a distinguished road
Default Trying to upload files with PHP and Prototype AJAX

Hello there.

I have a problem i don´t seem to understand why it wont work out the way i intend it to.

The only thing i can think of is that the headers or the upload data don´t get transfered correctly.

I will show you 3 files that involves the problem:

First my javascript that uses the Prototype Framwork.
Code:
function AjaxUpload(MyUrl, MyDiv, TargetDiv)
{
var TargetDiv;
var MyDiv;
var MyUrl;

	new Ajax.Request(MyUrl, {
		method: 'post',
		contentType: 'multipart/form-data',
		encoding: 'UTF-8',
		parameters: $(TargetDiv).serialize(true),
			onCreate: function(transport)
			{ 
				var MyStatusText = $('MyStatusDiv');
				MyStatusText.addClassName('standardBox');
				new Ajax.Updater(MyStatusText, 'ajax_loader.php');
			},
				onSuccess: function(transport)
				{ 
					// var MyResponseText = $(MyDiv);
					// dölj div före utfällning och lägg på class
					// $('ajax_container').hide();
					$(MyDiv).addClassName('standardBox');
					// skicka in data i div
					$(MyDiv).update(transport.responseText);
					// fäll ut div, pulsera och visa data
					// Effect.toggle('ajax_container', 'blind');
					$(MyDiv).show();
					Effect.Pulsate(MyDiv);
				},
			onComplete: function(transport)
			{ 
				var MyStatusText = $('MyStatusDiv');
				MyStatusText.update('');
				MyStatusText.removeClassName('standardBox');
			}
	});

}
And the upload form:
PHP Code:
<form id="ajaxUploadForm" action="#" method="post">
    
    <!-- MAX_FILE_SIZE must precede the file input field -->
    <input type="hidden" name="MAX_FILE_SIZE" value="<?PHP echo(1024*1024*100); ?>" />
    
        Fil att ladda upp:<br />
        <input name="userfile" type="file" size="30" maxlength="" />
        <br /><br />
    
    <input type="hidden" name="token" value="test_<?PHP echo(md5(microtime())); ?>" />
    
    <input type="submit" name="SUBMIT" value="Ladda upp"  onclick="AjaxUpload('ajax_upload.php', 'ajax_container', 'ajaxUploadForm'); return false"/>
    </form>
And the php code that should provide the logic for upload.
This file is called by the JavaScript function mentioned.
PHP Code:
<?PHP
    
/* Debug styff */
    
print_r($_FILES);
    echo(
'<br /><br />');
    echo(
'Debug token: ' $_POST['token']);
    echo(
'<br /><br />');
    
/* Debug styff */
    
    
$error $_FILES["userfile"]["error"];    

    
// where to upload
    
$uploaddir '/home/virtual/myhost/public_html/uploads/';

    
// make a random file prefix
    
$file_prefix 'id_' substr(md5(microtime() . mt_rand(11000000)), 016) . '_';
    
$uploadfile $uploaddir basename($file_prefix $_FILES['userfile']['name']);
    
$upload_file_name basename($file_prefix $_FILES['userfile']['name']);

    
// Do the upload
    
if(move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile))
    {
    
    
/* Is the Mime type valid ? */
            
$file_content_type $_FILES['userfile']['type'];
            
$permited_mimes[] = 'image/jpeg';
            
$permited_mimes[] = 'image/gif';
            
$permited_mimes[] = 'image/png';
    
            if(
in_array($file_content_type$permited_mimes)){
            } else {
                
unlink('uploads/' $upload_file_name);
                
$upload_file_name '';
                echo(
'filetype type not valid!');
                exit;
            }
    
    
    } else {
        echo(
'An Upload error accured!');
        exit;
    }

    echo(
'File:' $upload_file_name ' Has been uploaded.<br />');

    echo(
'Error code: ' $error);
?>
I guess the problem is in the $_FILES array not reaching the upload logic code.

Do you gurus have any tips ? :$

If there is odd words somewhere, it´s because im swedish ;)

Thx in advance.

/Eye
__________________
Of course the whole point of a doomsday machine, would have been lost if you keep it a secret.
EyeDentify is offline  
Reply With Quote
Old 05-22-2008, 06:19 PM   #2 (permalink)
Wizard
Top Contributor 
 
Village Idiot's Avatar
 
Join Date: Sep 2007
Posts: 1,299
Thanks: 17
Village Idiot is on a distinguished road
Default

To my knowledge, there is no way to pass that though ajax. Every "ajax" upload you see is really a hidden Iframe.
__________________

Village Idiot is offline  
Reply With Quote
The Following User Says Thank You to Village Idiot For This Useful Post:
EyeDentify (05-22-2008)
Old 05-22-2008, 06:23 PM   #3 (permalink)
The Frequenter
Zend Certified 
 
Join Date: Sep 2007
Location: Denmark
Posts: 352
Thanks: 8
Kalle is on a distinguished road
Default

Like Village Idiot said, theres no way to pass file uploads though AJAX so you have to use a hidden iframe which submits the data, the sourcecode of this small script should help give you a clue ;)

AJAX File Upload
__________________
Send a message via MSN to Kalle Send a message via Skype™ to Kalle
Kalle is offline  
Reply With Quote
The Following User Says Thank You to Kalle For This Useful Post:
EyeDentify (05-22-2008)
Old 05-22-2008, 06:33 PM   #4 (permalink)
The Acquainted
 
EyeDentify's Avatar
 
Join Date: Nov 2007
Location: Sweden
Posts: 106
Thanks: 13
EyeDentify is on a distinguished road
Default

Thx :)

I was afraid of that answer :)

So ajax is currently just usable for sending data into an database or something other then uploading a file ?

I say thanks again :)

I will have to figure something else out :)

/Eye
__________________
Of course the whole point of a doomsday machine, would have been lost if you keep it a secret.
EyeDentify is offline  
Reply With Quote
Old 01-19-2009, 01:16 PM   #5 (permalink)
The Visitor
 
Join Date: Jan 2009
Posts: 1
Thanks: 0
valums is on a distinguished road
Default

You can use my ajax upload plugin for prototype, it allows you to upload multiple files and use any element as upload button.
valums is offline  
Reply With Quote
Old 01-19-2009, 10:01 PM   #6 (permalink)
The Frequenter
Newcomer 
 
xenon's Avatar
 
Join Date: Dec 2007
Location: Bucharest, Romania
Posts: 438
Thanks: 3
xenon is on a distinguished road
Default

Also, in order to be able to upload files to a server, you need to set the encoding type on the form to multipart/form-data:

Code:
<form action="..." method="post" enctype="multipart/form-data">
__________________
I have optimistic thoughts, even though sometimes (if not always) life's a bitch.
xenon is offline  
Reply With Quote
Old 01-19-2009, 10:04 PM   #7 (permalink)
Wizard
Top Contributor 
 
Village Idiot's Avatar
 
Join Date: Sep 2007
Posts: 1,299
Thanks: 17
Village Idiot is on a distinguished road
Default

Lastly, your MIME data can easily be forged, I could upload literally anything to your server (show me where it is hosted and I will gladly prove it).
__________________

Village Idiot is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 10:30 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design