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-

in form.php (also contains javascript)-

// 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() {
'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) {

<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" />


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

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.


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

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