Friday, March 11, 2011

UPLOADIFY - problem while SETTING SESSION/ COOKIE - Alternate solution

Today I got a weird problem with UPLOADIFY while setting a cookie/ session with uploaded filename with current timestamp; from AJAXed uploadify.php.
I was trying to set that from uploadify.php so that I could get the filename to formSubmit.php.
but it was not working as there used to be a flash player issue (the flash player access the uploadify script as a new user and gets a new session).

So, what I did is-

1.
in form.php (also contains javascript)-


<?php
// setting a variable with current timestamp, to rename a file.
[it will not affect on .gif/.png images]

$uploadedImg = time().'.jpg';
?>

<script type="text/javascript">
$(document).ready(function() {
$('#file_upload').uploadify({
'uploader' : './uploadify/uploadify.swf',
'script' : './uploadify/uploadify.php',
'cancelImg' : './uploadify/cancel.png',
'folder' : './pics',
'auto' : true,
'scriptData' : {'imgName': '<?=$uploadedImg?>'},
'buttonText': 'Select Logo',

'onAllComplete' : function(event,data) {
$('#profile_pic_flag').val(1);
}
});
});
</script>


<form method="POST" action="formSubmit.php">

<!-- UPLOADIFY input element -->
<input id="file_upload" name="file_upload" type="file" />



<!-- input element to get the Picture name-->
<input type="hidden" name="profile_pic" id="profile_pic" value="<?=$uploadedImg?>" />

<!-- input element to Check if an imege is
uploaded or not(value set by UPLOADIFY onAllComplete event),
[this element is more efficient while Editing forms]-->

<input type="hidden" name="profile_pic_flag" id="profile_pic_flag" value="0" />

</form>




2.
in AJAXed uploadify.php

replace this part-


$tempFile = $_FILES['Filedata']['tmp_name'];
$targetPath = $_SERVER['DOCUMENT_ROOT'] . $_REQUEST['folder'] . '/';
$upFileName = $_REQUEST['imgName'];
$targetFile = str_replace('//','/',$targetPath) . $upFileName;

// uploading the file with the 'imgName', which was set by form.php
// and picture uploaded easily


3.
in formSubmit.php


if(isset($_POST['profile_pic']) && $_POST['profile_pic'] && $_POST['profile_pic_flag'] )
{
$profilePic = './pics/'.$_POST['profile_pic'];
// Then storing the $profilePic in Database.
}


- And all are done.

2 comments:

  1. Thanx dude.. It works for me.. :)

    ReplyDelete
  2. u r welcome.
    Your comment made this site Active & I will post latest stuffs again.
    Thanks for commenting here.

    ReplyDelete