By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,377 Members | 3,038 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,377 IT Pros & Developers. It's quick & easy.

File upload with php and ajax - possible?

Markus
Expert 5K+
P: 6,050
I've tried myself and all to no avail because i know - or think i know - that the $_FILES super-global won't pass with javascript.. or will it?

Does anyone have any insight on this? I know it's possible because i've seen it done.. many times.

Hmpf!
Jan 3 '08 #1
Share this Question
Share on Google+
7 Replies


eragon
100+
P: 431
Yes, i do. In fact, i use this on m site. Do you have any code you can share, specifics on what kind of file you want? Images, text, everything...

Ask and ye shall recieve.
Knock and it shall be opened unto you.

anotherwords

i have the answer, i need details...
Feb 3 '08 #2

Markus
Expert 5K+
P: 6,050
Ahaha!

I was just thinking of images at the moment..

So, do explain how you worked this :)
Feb 3 '08 #3

eragon
100+
P: 431
certainly! This script took a while to get working, so i will help you get it working if some things are different. Here is the first part, the image upload form:

[HTML]<form action="?a=upload" method="post" enctype="multipart/form-data" name="imgForm" id="imgForm">
Choose an Image to upload: <input type="file" name="image" />
<input type="submit" name="Upload" value="Upload" /></form>[/HTML]

and this part goes anywhere on your php page:

[PHP]if($_GET['a'] == "upload") { //Comfirms the action

//The function that confirms an image is valid:
function image_valid($type)
{
$file_types = array(
'image/pjpeg' => 'jpg',
'image/jpeg' => 'jpg',
'image/jpeg' => 'jpeg',
'image/gif' => 'gif',
'image/X-PNG' => 'png',
'image/PNG' => 'png',
'image/png' => 'png',
'image/x-png' => 'png',
'image/JPG' => 'jpg',
'image/GIF' => 'gif',
'image/bmp' => 'bmp',
'image/bmp' => 'BMP',
);

if(!array_key_exists($type, $file_types))
{
return false;
}
else
{
return true;
}
}

//File Handler
if(image_valid($_FILES['image']['type']))
{
//------------ Chooose the path to upload the image. Path must be writable. -------------//
// (Edit the part in quotes) //
$target_path = "../images/".$_FILES['file']['name'];
$_FILES['file']['tmp_name'];
if(move_uploaded_file($_FILES['file']['tmp_name'], $target_path)) {
echo "Image uploaded!";
} else { echo "There was an error uploading the file, please try again!"; }
} else { die("Image file is not valid");}
}
}[/PHP]

i hop[e this works, post any errors you may get.
Feb 4 '08 #4

hsriat
Expert 100+
P: 1,654
Couldn't find AJAX... thought it would work EXCELLENT!!! :p
Feb 10 '08 #5

eragon
100+
P: 431
this *could* be implemented into ajax using sajax. If your not sure, look up sajax on google, it is an ajax implementation that calls any php function, and with a little modification to the code, will alllow uploading files without even reloading the page! How did my php work?
Feb 11 '08 #6

Markus
Expert 5K+
P: 6,050
this *could* be implemented into ajax using sajax. If your not sure, look up sajax on google, it is an ajax implementation that calls any php function, and with a little modification to the code, will alllow uploading files without even reloading the page! How did my php work?
Looks like it should work..
I already have an upload class / functions, though.

I'm reading into sajax now.

Thanks for the heads up; sajax looks interesting!
Feb 11 '08 #7

eragon
100+
P: 431
not a problem. i use sajax for my most up to date (and most bandwidth stealing) active users monitior. A js in the head of every page calls the php every second and if the php was not called for 3 seconds the user is removed from the list.

I like absolute control over my computer.

And website.

Just wait till i get more than 20 members... my head will break.


and bandwidth limit.
Feb 13 '08 #8

Post your reply

Sign in to post your reply or Sign up for a free account.