Hi guys

Hoping you can help me I have designed a form when you click on Attach it opens a pop up window with the upload script, uploading is not a problem but returning the value from the pop up text field to the main form is not working.

Main form.

<form enctype="multipart/form-data" name="frmmain" action="">
<INPUT TYPE="text" NAME="firstname2" VALUE="" SIZE=23 MAXLENGTH=23 readonly="">
	          <INPUT NAME="Pop Up Button" TYPE=BUTTON onClick="window.open('testupload.php','popup','width=300,height=300,left=800,top=400')" VALUE="Attach">
</form>

Pop up form with upload script

<SCRIPT LANGUAGE=JAVASCRIPT>



function transferdataout() {


opener.document.form.firstname2.value=document.popup.firstname2.value; 
return validbutton(this);
return validbox(this);
self.close();
return false

}

function loaddatain() {


document.popup.firstname2.value=opener.document.form.firstname2.value;

return false

}
</SCRIPT>
   
   
    </head> 

    <body onLoad="loaddatain(this);"> 

    <form method="post" action="uploadv2.php" enctype="multipart/form-data" name="popup"> 

    <p>Select a file.<br />
      <input type="file" name="firstname2" tabindex="1" />
    </p>
    <p><input type="hidden" name="execute" value="1" /></p> 
	<P><INPUT TYPE=submit NAME="Transfer Data" VALUE="Upload File"
onclick="return transferdataout(this)">
	  <br>
	  <br>
	  <img src="close_grey.png" width="64" height="20" onClick="self.close();" class="clickimage"/>
	
	<p>
    
    </form>

Upload script

<?php
$allowedExtensions = array("jpg", "png","bmp");

function isAllowedExtension($fileName) {
  global $allowedExtensions;

  return in_array(end(explode(".", $fileName)), $allowedExtensions);
}
 if(isset($_POST['execute'])=="1"){
if($_FILES["firstname2"]['error'] == UPLOAD_ERR_OK) {
  if(isAllowedExtension($_FILES["firstname2"]['name'])) {
    # Do uploading here
	if (file_exists("../img/" . $_FILES["firstname2"]["name"]))
      {
      echo $_FILES["firstname2"]["name"] . " already exists. ";
      echo "<script>alert(\"File already exists\")</script>";
	  echo "<script>window.location.replace(\"testupload_1.php\"); </script>";
	  }
    else
      {
      move_uploaded_file($_FILES["firstname2"]["tmp_name"],
      "../img/" . $_FILES["firstname2"]["name"]);
      echo "Stored in: " . "../img/" . $_FILES["firstname2"]["name"];
	  echo "<script>self.close(); </script>";
      }
  } else {
    echo "<script>alert(\"Invalid file type\")</script>";
	echo "<script>window.location.replace(\"testupload_1.php\"); </script>";
  }
} else 
echo "<script>alert(\"Cannot upload\")</script>";

echo "<script>window.location.replace(\"testupload_1.php\"); </script>";
};
?>

Please help :'( can see where is not working.

<?php
    $allowedExtensions = array("jpg", "png","bmp");
     
    function isAllowedExtension($fileName) {
    global $allowedExtensions;
     
    return in_array(end(explode(".", $fileName)), $allowedExtensions);
    }
    if(isset($_POST['execute'])=="1"){
    if($_FILES["firstname2"]['error'] == UPLOAD_ERR_OK) {
    if(isAllowedExtension($_FILES["firstname2"]['name'])) {
    # Do uploading here
    if (file_exists("img/" . $_FILES["firstname2"]["name"]))
    {
    echo $_FILES["firstname2"]["name"] . " already exists. ";
    echo "<script>alert(\"File already exists\")</script>";
    echo "<script>window.location.replace(\"testupload_1.php\"); </script>";
    }
    else
    {
    move_uploaded_file($_FILES["firstname2"]["tmp_name"],
    "img/" . $_FILES["firstname2"]["name"]);
    echo "Stored in: " . "../img/" . $_FILES["firstname2"]["name"];
    echo "<script>self.close(); </script>";
    }
    } else {
    echo "<script>alert(\"Invalid file type\")</script>";
    echo "<script>window.location.replace(\"testupload_1.php\"); </script>";
    }
    } else
    echo "<script>alert(\"Cannot upload\")</script>";
     
    echo "<script>window.location.replace(\"testupload_1.php\"); </script>";
    };
    ?>

Check those lines

if (file_exists("img/" . $_FILES["firstname2"]["name"]))
    {
    echo $_FILES["firstname2"]["name"] . " already exists. ";
    echo "<script>alert(\"File already exists\")</script>";
    echo "<script>window.location.replace(\"testupload_1.php\"); </script>";
    }
    else
    {
    move_uploaded_file($_FILES["firstname2"]["tmp_name"],
    "img/" . $_FILES["firstname2"]["name"]);

You have entered for location ../img (which is wrong) you can use either ./img or just img/
All depends on your system. For more information check PHP.net path information.

PHP paths are not as html paths so when you use 2 dots ../ you go out of your site folder and the location is not valid

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.