HTML/PHP File Upload AJAX+Fancybox

moorehanoi

New Member
So I have an upload feature on my website. So in my main page, the html looks like this:\[code\]<iframe class="iframe" name="my_iframe" src="http://stackoverflow.com/questions/12731687/upload_file.php" style="display:none;"></iframe><form id="uploadForm" action="upload_file.php" method="post" enctype="multipart/form-data" target="my_iframe"> Select a file: <input type="file" name="upload"> <input type="submit" value="http://stackoverflow.com/questions/12731687/Upload"></form>\[/code\]If I didn't hide my iframe, then the response from my php would be displayed in that box. But I want to hide it until there is a response, then I want to show that response like a popup box. So here's my php:\[code\]<?php ini_set('display_errors', 'On'); error_reporting(E_ALL | E_STRICT); session_start(); $allowedExts = array("doc", "docx"); $extension = pathinfo( $_FILES["upload"]["name"],PATHINFO_EXTENSION); $username = $_SESSION["username"]; if (($_FILES["upload"]["size"] < 200000) && in_array($extension, $allowedExts)) { if ($_FILES["upload"]["error"] > 0) { echo "Return Code: " . $_FILES["upload"]["error"] . "<br />"; } else { echo "Upload: " . $_FILES["upload"]["name"] . "<br />"; echo "Type: " . $_FILES["upload"]["type"] . "<br />"; echo "Size: " . ($_FILES["upload"]["size"] / 1024) . " Kb<br />"; echo "Temp file: " . $_FILES["upload"]["tmp_name"] . "<br />"; $dir_exists = is_dir("/disks/***/***/***/Proposals/". $_SESSION["FirstName"] ."-".$_SESSION["username"]."/"); $file_exists = file_exists("/disks/***/***/***/Proposals/".$_SESSION["FirstName"] ."-".$_SESSION["username"]."/" . $_FILES["upload"]["name"]); $folderName=$_SESSION["FirstName"]; $baseDir = "/disks/***/***/***/Proposals/"; // Create directory if it does not exist if (! $dir_exists) { if (is_writable($baseDir)) { mkdir($baseDir . $_SESSION["FirstName"]."-".$_SESSION["username"]); } else { trigger_error($baseDir. " is not writeable"); } } if ($file_exists) { echo $_FILES["upload"]["name"] . " already exists. "; } else { move_uploaded_file($_FILES["upload"]["tmp_name"], $baseDir. $_SESSION["FirstName"] ."-".$_SESSION["username"]."/". $_FILES["upload"]["name"]); echo "Stored in: " . $baseDir. $_SESSION["FirstName"] ."-".$_SESSION["username"]."/". $_FILES["upload"]["name"]; } } } else { echo "Invalid file"; }?>\[/code\]And I put some AJAX code in my main page that looks like:\[code\]$("#uploadForm").bind("submit", function() { $.ajax({ type : "POST", cache : false, url : "upload_file.php", success : function(data) { $.fancybox(data.responseText,{ 'hideOnContentClick': true }); } }); return false;});\[/code\]My file upload works perfectly, without any problems. But the problem is there is no confirmation message or anything which is why i'm trying to implement this iframe with fancybox that appears on response from upload_file.php and then can be closed on click. However when I try to upload a file it uploads perfectly but the iframe doesn't appear with any message (yes I know i have display:none but i need it to be invisible until the upload is done). Any ideas what I should add/change?
 
Back
Top