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 One low monthly fee you get access to files using 4 premium accounts

Unlimited Storage and Bandwidth for $4.95/mo Your Link here @20$ Member of the Month - Free Rapidshare Account Join NFO Competition
Go Back   Home > Tutorial Section > Programming > PHP
Forgot Password? Join Us!

PHP Post your Personal Home Pages Programing Tutorial Here

Notices

Your Ad Here


Post New Thread Reply
 
LinkBack Thread Tools
Old 06-13-2008, 05:30 AM   #1 (permalink)
 
KoOL's Avatar

 
User Info
Join Date: Oct 2007
Location: In Your Mind
Age: 27
Achievements Posts: 11,068
Casino Cash: $1184880

Total Points: 239,522,161,428.68
Donate

KoOL has disabled reputation

Awards Showcase
Administrator Of the Month Of Jan 
Total Awards: 1
AJAX file upload tutorial

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.
KoOL is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply
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.
Post New Thread Reply

Bookmarks


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

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 Off
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


These are the 100 most used thread tags
Tag Cloud
(2008) 1080i 2007 2008 action adobe advanced adventure aio antivirus appz audio avalon build burning collection comedy converter copy crack database deluxe desktop development digital tutors direct download disciple download drama driver edition files final finance flash fonts full game games getdata graphics hacking halloween hdtv hidden hq increditools internet introducing myself keygen link linkin park magic matlab maya megaupload microsoft mobile movie multimedia music n95 network office pack photoshop pictures pillar platinum plumb pocket portable premium professional rapidshare recover retail rs.com security serial server sexy software studio symbian telugu movie template tools ultimate utilities video virus vista wallpaper wallpapers web development windows wwe xp [rs.com]

New To AiO Forum? Need Help?

Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.

Site Best Viewed with Firefox 3.0 & IE v7.0
RapidShare Links PhazeDDL Warez
PhazeDDL Warez