Jump to content


This topic is now archived and is closed to further replies.


uploading a file using ajax (JS functions) and php-ftp

Recommended Posts

[RESOLVED PROBLEM - 22/03/2006]

To start off, I have the tendancy to sound confusing when i explain things. Please feel free to query about anything you might not understand!
Please request code you would like to see, besides the two examples below there is ALOT of code and dont want to clutter the first post too much.

I have a html form with the usual "name" "number" etc fields, but it also has a "file" input field. When submit is pressed the form moves to a php script (seperate file). The form beginning looks like:
[!--html--][div class=\'htmltop\']HTML[/div][div class=\'htmlmain\'][!--html1--]<[color=blue]form id[/color]="[color=orange]attach_form[/color]" action="[color=orange]phpScriptFile.php[/color]" method="[color=orange]post[/color]">
<[color=blue]input type[/color]="[color=orange]file[/color]" id="[color=orange]filenames[/color]">

The script then uploads the specified file via php's ftp functions to the server and then adds it to a generated email as an attachment. Then sends the email to bla@bla.bla

With the above structure it works. Everything uploads and sends correctly. BUT now i have been adding Ajax features to my website, to avoid jumping to pages and have things refresh/reloaded all the time. You know, make it look cool! And this is where my problem comes into play.

When i add the Ajax features i make so that the form no longer goes directly to the php script/file. Now it goes through a few JS functions before it gets to the php file. First some validation and then the ajax call.
Before the ajax i was using $_FILES array in php to access the data i needed from the file input field to upload via ftp. Examples of these values/data:

$docName= $_FILES['filenames']['name'];
$docType= $_FILES['filenames']['type'];

Nothing had to be brought in via $_POST['..'] for the file data, just used $_FILES.
$_FILES as i understand it is a reference to the temp location on the server, of the file made by the file input field in the html. I also read that if one does not use the temp data of the file immediately, it goes away. "POOF! All Gone!"

So now when i use the ajax(JS), by the time the code gets to the php script the $_FILES array is empty!

My question is basically:
Is there a way that i can keep the "file" data alive in JS that i can run through the JS functions and still access the temp data via $_FILES array in the php file? Or even if there might be a different way to access the info which i need after the whole JS traversal?

Thank you upfront!

P.S: What do i use to show php code? As seen above [code=php:0] ... [/code] dont work?

Share this post

Link to post
Share on other sites
Ajax can not be used to upload files. Javascript is "sandboxed" and can not access the computer's file system, execpt for cookies. You will have to use a "conventional" posted form to upload the file.

Also, how is your file upload working when you are not using the 'enctype="multipart/form-data"' declaration in your form?

Share this post

Link to post
Share on other sites
Thank you hitman6003, for the information! Started having the idea that even if there was a way it would probably make the code NOT act ajaxian anyway!? :)

For the enctype not added to the form example? It is there (in my code at the office) must have forgotten to add it to the post!!
My bad, please forgive me!! :P

Thank you again.

Share this post

Link to post
Share on other sites


Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.