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
 
 
LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
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
 



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 07:37 PM.

 
     

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