基于PHP的AJAX技术实现文件异步上传源码(2)

2013 年 8 月 6 日3920

<?php
$upload_dir = "/var/http://www.zjjv.com///">AnyExample</a>
 </body></html>
 END;
 
 $f = fopen($upload_dir.'/'.$filename.'-desc.html', "w");
 fwrite($f, $html);
 fclose($f);
 $msg = "File {$filename} uploaded,
 <a href='{$web_upload_dir}/{$filename}-desc.html'>see file information page</a>";

 $f = fopen($upload_dir."/upload-log.html", "a");
 fwrite($f, "<p>$msg</p>\n");
 fclose($f);

 setcookie('msg', $msg);
 header("Location: http://".$_SERVER['HTTP_HOST'].$PHP_SELF);
 exit();
}

if (isset($_COOKIE['msg']) && $_COOKIE['msg'] != '')
{
 if (get_magic_quotes_gpc())
  $msg = stripslashes($_COOKIE['msg']);
 else
  $msg = $_COOKIE['msg'];
  setcookie('msg', '');
}
?>
<!-- Beginning of main page -->
<html><head>
<title>IFRAME Async file uploader example</title>
</head>
<body>
<?php
 if (isset($msg))
  echo '<p>'.$msg.'</p>';
?>
<h1>Upload file:</h1>
<p>File will begin to upload just after selection. </p>
<p>You may write file description, while you file is being uploaded.</p>

<form action="<?=$PHP_SELF?>" target="upload_iframe" method="post" enctype="multipart/form-data">
 <input type="hidden" value="true">
 <!-- Target of the form is set to hidden iframe -->
 <!-- From will send its post data to fileframe section of this PHP script (see above) -->

 <label for="file">text file uploader:</label><br>
 <!-- JavaScript is called by OnChange attribute -->
 <input type="file" onChange="jsUpload(this)">
</form>
<script type="text/javascript">
/* This function is called when user selects file in file dialog */
function jsUpload(upload_field)
{
 // this is just an example of checking file extensions
 // if you do not need extension checking, remove
 // everything down to line
 // upload_field.form.submit();
 
 var re_text = /\.txt|\.xml|\.zip/i;
 var filename = upload_field.value;

 /* Checking file type */
 if (filename.search(re_text) == -1)
 {
  alert("File does not have text(txt, xml, zip) extension");
  upload_field.form.reset();
  return false;
 }

 upload_field.form.submit();
 document.getElementById('upload_status').value = "uploading file...";
 upload_field.disabled = true;
 return true;
}
</script>
<iframe>
</iframe>
<!-- For debugging purposes, it's often useful to remove
"display: none" from attribute -->

<br>
Upload status:<br>
<input type="text"
value="not uploaded" size="64" disabled>
<br><br>

File name:<br>
<input type="text" value="none" disabled>

<form action="<?=$PHP_SELF?>" method="POST">
 <!-- one field is "disabled" for displaying-only. Other, hidden one is for sending data -->
 <input type="hidden">
 <br><br>

 <label for="photo">File description:</label><br>
 <textarea rows="5" cols="50"></textarea>

 <br><br>
 <input type="submit" value="save file" disabled>
</form>
<br><br>
<a href="<?=$web_upload_dir?>/upload-log.html">upload-log</a>
<br><br><br>

Example by <a href="http://http://www.zjjv.com///">AnyExample</a>
</body>
</html>

0 0