Thursday, April 19, 2012

I am getting an undefined variable

I am trying to retrieve a file name from the server of the file that has just been uploaded. The problem is that I am getting an undefine index on my php variable below:



 Undefined index: fileimagename in /web/stud/xxx/Mobile_app/QandATable.php on line 19


What I am trying to do is that user uploads the form which is done by the php script (imageupload.php), Now when uploading stops it uses the javascript function which is on a seperate page (QandATable.php, same page as the ) to display the message if upload is successful and this would append the name of the file uploaded.
So my question is how can I fix the undefined variable and how can I get the javascript function in the QandATable.php to retireve the file name from imageupload.php?



Below is code which consists the undefined variable and javascript function (QandATable.php):



            <?php
session_start();

$fileimagename = $_SESSION['fileimagename'];

?>

<script type="text/javascript">

function stopImageUpload(success){

var fileimagename = <?php echo $fileimagename; ?>;
var result = '';
if (success == 1){
result = '<span class="msg">The file was uploaded successfully!</span><br/><br/>';
$('.listImage').append(fileimagename + '<br/>');
}
else {
result = '<span class="emsg">There was an error during file upload!</span><br/><br/>';
}

return true
};
</script>


Below is php script which uploads the file into the server: (imageupload.php)



 <?php

session_start();

if (isset($_POST['fileimagename'])) {

$_SESSION['fileimagename'] = $_SESSION[' $_FILES['fileImage']['name']'];

}

if( file_exists("ImageFiles/".$_FILES['fileImage']['name'])) {
$parts = explode(".",$_FILES['fileImage']['name']);
$ext = array_pop($parts);
$base = implode(".",$parts);
$n = 2;

while( file_exists("ImageFiles/".$base."_".$n.".".$ext)) $n++;
$_FILES['fileImage']['name'] = $base."_".$n.".".$ext;

move_uploaded_file($_FILES["fileImage"]["tmp_name"],
"ImageFiles/" . $_FILES["fileImage"]["name"]);
$result = 1;

}
else
{
move_uploaded_file($_FILES["fileImage"]["tmp_name"],
"ImageFiles/" . $_FILES["fileImage"]["name"]);
$result = 1;

}

?>

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

window.top.window.stopImageUpload(<?php echo $result;?>,'<?php echo $fileimagename;?>');

</script>




No comments:

Post a Comment