#6 Kit
Už mi to funguje zmenil som $savefolder = "images"; na
$savefolder = "c:/uploads/"; Len sa mi nezobrazuje obrázok
//index.html
<!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>
<title>Title</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<link rel="stylesheet" type="text/css" rel="nofollow" href="style.css" />
<script type="text/javascript" src="xmlhttp.js"></script>
<script type="text/javascript" src="functions.js"></script>
</head>
<body>
<div id="showimg"></div>
<form id="uploadform" action="process_upload.php" method="post" enctype="multipart/form-data" target="uploadframe">
Upload a File:<br />
<input type="file" id="myfile" name="myfile" />
<input type="submit" value="Submit" onclick="uploadimg(document.getElementById('uploadform')); return false;" />
<iframe id="uploadframe" name="uploadframe" src="process_upload.php" class="noshow"></iframe>
</form>
</body>
</html>
//process_upload.php
<?php
//process_upload.php
//Allowed file mime types.
$allowedtypes = array ("image/jpeg","image/pjpeg","image/png","image/gif");
//Where we want to save the file to.
$savefolder = "images";
/*
//First, kill off all older files.
if (is_dir ($savefolder)){
$scanarray = scandir ($savefolder);
$numdirs = count ($scanarray);
//parse the array
for ($i = 0; $i < $numdirs; $i++){
// make sure it is not the '.' or '..' files
if ($scanarray[$i] != "." && $scanarray[$i] !="..") {
//make sure it's a file
if (is_file ($savefolder . "/" . $scanarray[$i])){
unlink ($savefolder . "/" . $scanarray[$i]);
}
}
}
}
*/
//If we have a valid file.
if (isset ($_FILES['myfile'])){
//Then we need to confirm it is of a file type we want.
if (in_array ($_FILES['myfile']['type'],$allowedtypes)){
//Then we can perform the copy.
if ($_FILES['myfile']['error'] == 0){
$thefile = $savefolder . "/" . $_FILES['myfile']['name'];
if (!move_uploaded_file ($_FILES['myfile']['tmp_name'], $thefile)){
echo "There was an error uploading the file.";
} else {
//Signal the parent to load the image.
?>
<!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>
<script type="text/javascript" src="functions.js"></script>
</head>
<body>
<img src="<?=$thefile?>" onload="doneloading (parent,'<?=$thefile?>')" />
</body>
</html>
<?php
}
}
}
}
?>
// other functions to save into functions.js
//functions.js
//Function to determine when the process_upload.php file has finished executing.
function doneloading(theframe,thefile){
var theloc = "showimg.php?thefile=" + thefile
theframe.processajax ("showimg",theloc);
}
function uploadimg (theform){
//Submit the form.
theform.submit();
//Then display a loading message to the user.
setStatus ("Loading...","showimg");
}
//Function to set a loading status.
function setStatus (theStatus, theObj){
obj = document.getElementById(theObj);
if (obj){
obj.innerHTML = "<div class=\"bold\">" + theStatus + "</div>";
}
}
function changesize (img, sml){
//The display a loading message to the user.
theobj = document.getElementById("showimg");
if (theobj){
setStatus ("Loading...","showimg");
var loc = "thumb.php?img=" + img + "&sml=" + sml;
processajax ("showimg",loc);
}
}
Posted on 26 Oct 2009, 22:01 - Category: AJAX
Comments - Edit - Delete
AJAX xmlhttp.js get or post include
//Just save, upload, include
//xmlhttp.js
//Function to create an XMLHttp Object.
function getxmlhttp (){
//Create a boolean variable to check for a valid microsoft active X instance.
var xmlhttp = false;
//Check if we are using internet explorer.
try {
//If the javascript version is greater than 5.
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
//If not, then use the older active x object.
try {
//If we are using internet explorer.
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch (E) {
//Else we must be using a non-internet explorer browser.
xmlhttp = false;
}
}
//If we are using a non-internet explorer browser, create a javascript instance of the object.
if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
xmlhttp = new XMLHttpRequest();
}
return xmlhttp;
}
//Function to process an XMLHttpRequest.
function processajax (serverPage, obj, getOrPost, str){
//Get an XMLHttpRequest object for use.
xmlhttp = getxmlhttp ();
if (getOrPost == "get"){
xmlhttp.open("GET", serverPage);
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
obj.innerHTML = xmlhttp.responseText;
}
}
xmlhttp.send(null);
} else {
xmlhttp.open("POST", serverPage, true);
xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
obj.innerHTML = xmlhttp.responseText;
}
}
xmlhttp.send(str);
}
}