All in one forum  - Applications | Games | E-Books | Music, Movies & Videos | Mobile Stuff | Live Discussions | Webmaster Stuff | Many More | Community to Hang Out & Stick to
Search Today's Posts Mark Forums Read

Go Back   Home > Tutorial Section > Programming > PHP
Reload this Page [Tutorial] AJAX file upload tutorial
Forgot Password? Join Us!
PHP Post your Personal Home Pages Programing Tutorial Here

Notices
Free Image Hosting Solution for all Your link here @ 10$ 10 K Unique Visitors 1 Million Visitors Per Month Your link here @ 10$

Your Ad Here

Rate This Thread - AJAX file upload tutorial.

Post New Thread Reply
 
LinkBack Thread Tools Display Modes
  (#1 (permalink)) Old
 
KoOL's Avatar
 
User Info
Join Date: Oct 2007
Location: In You Mind
Age: 27
Achievements Posts: 10,860
Casino Cash: $1149880

Total Points: 238,247,191,197.98
Donate

KoOL has disabled reputation

Awards Showcase
Administrator Of the Month Of Jan 
Total Awards: 1
AJAX file upload tutorial - 06-13-2008, 05:30 AM

First of all I have to say that to create a pure AJAX file upload system is not possible because of security limitations of JavaScript. All of the Ajax upload systems I know use some third party tool/package or only mimics the AJAX feeling. Even so it makes file upload process a bit nicer. In the next section I will present you a solution which imitates the AJAX process, but uses a normal upload process and iFrames.
Quote:
The concept:

* Create a simple HTML form for file upload
* Set the target to an iFrame which is on the actual page but not visible
* Call a JavaScript function on form submit to display the animation
* After the PHP part finishes the upload process, then it hides the animation

Creating the HTML file:

The HTML file we will use in this article is quite simple. It just have a simple form with a file field and a submit button. However don't forget about the enctype parameter.


<form action="upload.php" method="post" enctype="multipart/form-data" target="upload_target" >

File: <input name="myfile" type="file" />

<input type="submit" name="submitBtn" value="Upload" />

</form>


Besides this we need to add a bit more code to it. First we need a block where we will display the progress animation. We need an other block where we inform the visitor whether the upload was success or not. Besides this we need to add a hidden iFrame to the page which is used as the form target. At least we add an onSubmit event to the form. So our HTML body looks like this:


<body>

<p id="f1_upload_process">Loading...<br/><img src="loader.gif" /></p>

<p id="result"></p>

<form action="upload.php" method="post" enctype="multipart/form-data" target="upload_target" onsubmit="startUpload();" >

File: <input name="myfile" type="file" />

<input type="submit" name="submitBtn" value="Upload" />

</form>


<iframe id="upload_target" name="upload_target" src="#" style="width:0;height:0;border:0px solid #fff;"></iframe>

</body>

By default the progress animation block content is hidden. We need a JavaScript function which makes this block visible if the submit button was pressed. It is a very simple code, that only changes the visibility parameter.


function startUpload(){

document.getElementById('f1_upload_process').style .visibility = 'visible';

return true;

}


Besides this we need an other function, what we will call at the end of the upload process. This code will be print out a result message depending on it's parameter and hides the progress block again.


function stopUpload(success){

var result = '';

if (success == 1){

document.getElementById('result').innerHTML =

'<span class="msg">The file was uploaded successfully!<\/span><br/><br/>';

}

else {

document.getElementById('result').innerHTML =

'<span class="emsg">There was an error during file upload!<\/span><br/><br/>';

}

document.getElementById('f1_upload_process').style .visibility = 'hidden';

return true;

}


Before we creating the PHP code we still need to create a style for the form and the progress block. The form style is very simple, but the progress block style sets the z-index, position and visibility parameter as well.



#f1_upload_process{

z-index:100;

position:absolute;

visibility:hidden;

text-align:center;

width:400px;

margin:0px;

padding:0px;

background-color:#fff;

border:1px solid #ccc;

}


form{

text-align:center;

width:390px;

margin:0px;

padding:5px;

background-color:#fff;

border:1px solid #ccc;


}



Now we can focus on the server side.

Server side code:

The server side code is written in PHP and it is very short and simple. First we need to set the file destination path. In this case we will use the actual working directory. Afterwards we introduce a variable that shows if there was an error or not during the upload process. Next we move the uploaded file from the tmp directory to it's defined location and set the result variable if upload was success. At the end I have inserted a sleep command to make animation a bit longer in case of very fast uploads.

Now the end the PHP code looks like this:


<?php

$destination_path = getcwd().DIRECTORY_SEPARATOR;


$result = 0;

$target_path = $destination_path . basename( $_FILES['myfile']['name']);


if(@move_uploaded_file($_FILES['myfile']['tmp_name'], $target_path)) {

$result = 1;

}

sleep(1);

?>






Finish the work from iFrame:

The output of the PHP code will be displayed / executed inside the iFrame. As you remember the iFrame is not visible, however we can call a JavaScript function here. And exactly this is the point where we can call the JavaScript function defined in the HTML code to hide the progress animation and display the file upload result on the main page. We can do it with the following JavaScript code:


<script language="javascript" type="text/javascript">

window.top.window.stopUpload(<?php echo $result; ?>);

</script>

That's it.




For Download Links U need to Press Thanks Button

Please don't PM me For Posts, Request and Not Working Threads. Use Button.
Reply With Quote
The Following 4 Users Say Thank You to KoOL For This Useful Post:
apkamndora (08-08-2008), atulfunky (10-09-2008), barnick (06-28-2008), PaNkAJ (06-13-2008)
Click here to Donate to remove the Adverts.
Your Ad Here
Post New Thread Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Photoshop Tutorial - Animated Rain Tutorial in 7 Steps PaNkAJ Adobe Photoshop 0 01-12-2008 12:29 PM
CRACKING (Reverse Engineering) Tutorial Video, It's very good tutorial for everyone PaNkAJ Garbage Bin 11 01-04-2008 03:06 PM
How do you upload/transfer/upload videos Adan .O. Core Hardware 0 07-16-2007 07:36 AM
Bulletproof Ajax sharad_mlk Ebooks,tutorials & Other Stuff 0 05-24-2007 01:56 AM
Beginning Ajax with ASP.NET sharad_mlk Ebooks,tutorials & Other Stuff 1 04-24-2007 02:16 AM


New To AiO Forum? Need Help?

All times are GMT. The time now is 03:21 AM.
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
vBulletin Skin developed by: vBStyles

RapidShare Links PhazeDDL Warez
PhazeDDL Warez