0

I am trying to retrieve a file name from one page where the php script uploads the file (imageupload.php), and I want to display it in another page within a javascript function (QandATable.php). But I don't know how to do this

I will show you all of the relevant code so you can follow it and so you are able to understand what is happening.

UPDATE: BELOW I WILL SHOW YOU THE STEPS ON HOW THE FILE IS UPLOADED. THE CODE BELOW SUCCESSFULLY UPLOADS THE FILE.

Below is the form (QandATable.php);

var $fileImage = $("<form action='imageupload.php' method='post' enctype='multipart/form-data' target='upload_target' onsubmit='return imageClickHandler(this);' class='imageuploadform' >" + 
<label>Image File: <input name='fileImage' type='file' class='fileImage' /></label><br/><label class='imagelbl'>" + 
"<input type='submit' name='submitImageBtn' class='sbtnimage' value='Upload' /></label>" + 
"</p><ul class='listImage' align='left'></ul>" +
"<iframe class='upload_target' name='upload_target' src='#' style='width:0;height:0;border:0px;solid;#fff;'></iframe></form>"); 

On the same page when the user submits the form, it will go onto the function below, it will check for validation and then when validation is clear, it will go onto the startImageUpload() function:

 function imageClickHandler(imageuploadform){ 
      if(imageValidation(imageuploadform)){ 
          return startImageUpload(imageuploadform); 
      } 
      return false;
  }

If there is no validation then it will go onto the JS function (QandATable.php) below where it hides the file input and it will submit the form to the imageupload.php where the file uploading occurs. When the file is uploaded it then calls back to the stopImageUpload() function (QandAtable.php) where it will display the message on whether the file is uploaded or not and this is where I want the name of the file from the server to be appended.

Below is startImageUpload() function:

var sourceImageForm;

function startImageUpload(imageuploadform){

$(imageuploadform).find('.fileImage').css('visibility','hidden');
sourceImageForm = imageuploadform;

return true;
        }

Below is the php script where it uploads the file (imageupload.php):

     <?php

session_start();

$result = 0;

   if ($_FILES["fileImage"]["error"] > 0) {
      echo "Error: " . $_FILES["fileImage"]["error"] . "<br />";
      } else {
      echo "Upload: " . $_FILES["fileImage"]["name"] . "<br />";
      echo "Type: " . $_FILES["fileImage"]["type"] . "<br />";
      echo "Size: " . ($_FILES["fileImage"]["size"] / 1024) . " Kb<br />";
      echo "Stored in: " . $_FILES["fileImage"]["tmp_name"];
      }

if( file_exists("ImageFiles/".$_FILES['fileImage']['name'])) {
    $_SESSION['fileImage']['name'] = $_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"],
    $_SESSION['fileImage']['name'] = $_FILES['fileImage']['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;?>);
    </script>

Finally when upload is finished it goes back to the stopUploadImage() function (QandATable.php) to display the message on whether file is successfully uploaded or not. This is also where I want the uploaded file name from the server to be appended.

   function stopImageUpload(success){

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

    return true;

    }

1 Answer 1

1

To call on the stopImageUpload() function I would do the following:

From:

<script language="javascript" type="text/javascript">
window.top.windo.stopImageUpload(<?php echo $result;?>);
</script>

To:

<script language="javascript" type="text/javascript">
window.top.stopImageUpload(<?php echo "'$result'";?>);
</script>

To pass file names page to page use session variables. I would suggest something similar to the following (since you're already using sessions):

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

Just make sure to have session_start(); at the top of every page you want to pass those variables to.

Troubleshoot by running this:

<?php

    if ($_FILES["file"]["error"] > 0) {
      echo "Error: " . $_FILES["file"]["error"] . "<br />";
      } else {
      echo "Upload: " . $_FILES["file"]["name"] . "<br />";
      echo "Type: " . $_FILES["file"]["type"] . "<br />";
      echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
      echo "Stored in: " . $_FILES["file"]["tmp_name"];
      }
    ?>

To resolve your unexpected ';' error I moved the $_SESSION line outside of the :

<?php

session_start();

$result = 0;

   if ($_FILES["fileImage"]["error"] > 0) {
      echo "Error: " . $_FILES["fileImage"]["error"] . "<br />";
      } else {
      echo "Upload: " . $_FILES["fileImage"]["name"] . "<br />";
      echo "Type: " . $_FILES["fileImage"]["type"] . "<br />";
      echo "Size: " . ($_FILES["fileImage"]["size"] / 1024) . " Kb<br />";
      echo "Stored in: " . $_FILES["fileImage"]["tmp_name"];
      }

if( file_exists("ImageFiles/".$_FILES['fileImage']['name'])) {
    $_SESSION['fileImage']['name'] = $_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"]);
    //The error was thrown because you had $_SESSION inside the move_upload_file()
    //Can't have a ; inside a function
    //However, you need to remove this $_SESSION anyway, it is already declared above, 
    //it wouldn't work here anyway since your code breaks apart the $_FILES array.
    //$_SESSION['fileImage']['name'] = $_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;?>);
    </script>
Sign up to request clarification or add additional context in comments.

16 Comments

It still does the same thing, it doesn't upload the file, it is trying to load the file as you see the loading bar but then it doesn't do or finish the uploading
have you tried changing your original code <?php echo $result;?> to <?php echo "'$result'";?>?
I have tried that, still doing the same thing, I just can't see where the problem is :(
To be honest I tried to limit this code so that I tried to include as less code as possible. If I show my full code then it is a huge code but the uploading does work. I will update code to show whole code where uploading was working. What my question is that how do I retireve the file name from the server and append it in the javascript function (they are on seperate pages). Give me 10 mins to update code and we will do this slowly together :)
I have updated code, my question is how can I append the uploaded file name from the server by retrieving it from the php imageupload.php page into the javascript function stopImageUpload() function in the QandATable.php. I will be here waiting for comments and we can tackle this together :) Please follow the code slowly and thank you for helping me :)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.