Jump to content

Ajax File sharing


almystersv

Recommended Posts

Hi Guys,

 

I managed to get hold of this script on the internet and was hoping someone could help to explain it a little bit to me.

 

I would like to know what I need to do to change the file upload directory and also a little explanaition on the code.

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
   <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
   <title>BIS Portal File Uploader</title>
   <link href="style/style.css" rel="stylesheet" type="text/css" />
   
<script language="javascript" type="text/javascript">
<!--
function startUpload(){
      document.getElementById('f1_upload_process').style.visibility = 'visible';
      document.getElementById('f1_upload_form').style.visibility = 'hidden';
      return true;
}

function stopUpload(success){
      var result = '';
      if (success == 1){
         result = '<span class="msg">The file was uploaded successfully!<\/span><br/><br/>';
      }
      else {
         result = '<span class="emsg">There was an error during file upload!<\/span><br/><br/>';
      }
      document.getElementById('f1_upload_process').style.visibility = 'hidden';
      document.getElementById('f1_upload_form').innerHTML = result + '<label>File: <input name="myfile" type="file" size="30" /><\/label><label><input type="submit" name="submitBtn" class="sbtn" value="Upload" /><\/label>';
      document.getElementById('f1_upload_form').style.visibility = 'visible';      
      return true;   
}
//-->
</script>   
</head>

<body>
       <div id="container">
            <div id="header"><div id="header_left"></div>
            <div id="header_main">BIS Portal  File Uploader</div>
            <div id="header_right"></div></div>
            <div id="content">
                <form action="upload.php" method="post" enctype="multipart/form-data" target="upload_target" onsubmit="startUpload();" >
                     <p id="f1_upload_process">Loading...<br/><img src="loader.gif" /><br/></p>
                     <p id="f1_upload_form" align="center"><br/>
                         <label>File:  
                              <input name="myfile" type="file" size="30" />
                         </label>
                         <label>
                             <input type="submit" name="submitBtn" class="sbtn" value="Upload" />
                         </label>
                     </p>
                     
                     <iframe id="upload_target" name="upload_target" src="#" style="width:0;height:0;border:0px solid #fff;"></iframe>
                 </form>
             </div>
         </div>
                 
</body>

 

<?php
   // Edit upload location here
   $destination_path = getcwd().DIRECTORY_SEPARATOR;

   $result = 0;
   
   $target_path = $destination_path . basename( $_FILES['myfile']['name']);

   if(@move_uploaded_file($_FILES['myfile']['tmp_name'], $target_path)) {
      $result = 1;
   }
   
   sleep(1);
?>

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

Link to comment
https://forums.phpfreaks.com/topic/90668-ajax-file-sharing/
Share on other sites

Code:

--> is used for my comments

 

<?php

  // Edit upload location here

  $destination_path = getcwd().DIRECTORY_SEPARATOR;

 

  -->getcwd() IS A FUNCTION WHICH WILL RETURN CURRENT WORKING DIRECTORY.

  -->DIRECTORY_SEPARATOR IS A DEFINED CONSTANT

 

  $result = 0;

 

  $target_path = $destination_path . basename( $_FILES['myfile']['name']);

    -->BASENAME IS A FUNCTION WHICH RETURN FILENAME ONLY.

 

  if(@move_uploaded_file($_FILES['myfile']['tmp_name'], $target_path)) {

      $result = 1;

  }

  -->move_uploaded_file IS A FUNCTION WHICH MOVE THE UPLOADED FILE FROM TEMP DIR TO $target_path

  -->$_FILES IS A SUPER GLOBAL ARRAY WHICH GET POPULATED WHEN YOU DO FILE UPLOADING

  sleep(1);

?>

 

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

--> upload will get stop once files are moved to correct directory.

 

I think the js part is not a concern area.

 

Regards

Link to comment
https://forums.phpfreaks.com/topic/90668-ajax-file-sharing/#findComment-464791
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.