Jump to content

Recommended Posts

OK This makes no sence to me..

My user ID is set I have tryied it this way.

<?php

global $UserId;

$UserId=100001957015772;

?>

 

And this way;

<?php

global $UserId;

$UserId='100001957015772';

?>

Now in side the javascript i have it echoed. but it will not work. like this.

xhr.open(

'POST',

'upload.php?UserId=<? echo $UserId ?>'

);

 

 

But if i set it like this.

xhr.open(

'POST',

'upload.php?UserId=100001957015772'

);

 

it works just fine, whats wrong with it?

 

INDEX.php

<?php
global $UserId;
$UserId=100001957015772;
?>
<!DOCTYPE html>


<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" type="text/css" href="style/default.css" media="screen" />
<title>FileAPI nad XHR 2 ajax uploading</title>
</head>
<body>
<div id="wrap">


<h1>Choose (multiple) files or drag them onto drop zone below</h1>


<form action="" method="post" enctype="multipart/form-data">
<input type="file" id="fileField" name="fileField" multiple />
</form>


<div id="fileDrop">
<p>Drop files here</p>
</div>


<div id="files">
<h2>File list</h2>
<a id="reset" href="#" title="Remove all files from list">Clear list</a>
<ul id="fileList"></ul>
<a id="upload" href="#" title="Upload all files in list">Upload files</a>
</div>
</div>
<script type="text/javascript">

function FileAPI (t, d, f) {


var fileList = t,
fileField = f,
dropZone = d,
fileQueue = new Array()
preview = null;



this.init = function () {
fileField.onchange = this.addFiles;
dropZone.addEventListener("dragenter", this.stopProp, false);
dropZone.addEventListener("dragleave", this.dragExit, false);
dropZone.addEventListener("dragover", this.dragOver, false);
dropZone.addEventListener("drop", this.showDroppedFiles, false);
}


this.addFiles = function () {
addFileListItems(this.files);
}


this.showDroppedFiles = function (ev) {
ev.stopPropagation();
ev.preventDefault();
var files = ev.dataTransfer.files;
addFileListItems(files);
}


this.clearList = function (ev) {
ev.preventDefault();
while (fileList.childNodes.length > 0) {
fileList.removeChild(
fileList.childNodes[fileList.childNodes.length - 1]
);
}
}


this.dragOver = function (ev) {
ev.stopPropagation();
ev.preventDefault();
this.style["backgroundColor"] = "#F0FCF0";
this.style["borderColor"] = "#3DD13F";
this.style["color"] = "#3DD13F"
}


this.dragExit = function (ev) {
ev.stopPropagation();
ev.preventDefault();
dropZone.style["backgroundColor"] = "#FEFEFE";
dropZone.style["borderColor"] = "#CCC";
dropZone.style["color"] = "#CCC"
}


this.stopProp = function (ev) {
ev.stopPropagation();
ev.preventDefault();
}


this.uploadQueue = function (ev) {
ev.preventDefault();
while (fileQueue.length > 0) {
var item = fileQueue.pop();
var p = document.createElement("p");
p.className = "loader";
var pText = document.createTextNode("Uploading...");
p.appendChild(pText);
item.li.appendChild(p);
if (item.file.size < 1048576) {
uploadFile(item.file, item.li);
} else {
p.textContent = "File to large";
p.style["color"] = "red";
}
}
}


var addFileListItems = function (files) {
for (var i = 0; i < files.length; i++) {
var fr = new FileReader();
fr.file = files[i];
fr.onloadend = showFileInList;
fr.readAsDataURL(files[i]);
}
}


var showFileInList = function (ev) {
var file = ev.target.file;
if (file) {
var li = document.createElement("li");
if (file.type.search(/image\/.*/) != -1) {
var thumb = new Image();
thumb.src = ev.target.result;
thumb.addEventListener("mouseover", showImagePreview, false);
thumb.addEventListener("mouseout", removePreview, false);
li.appendChild(thumb);
}
var h3 = document.createElement("h3");
var h3Text = document.createTextNode(file.name);
h3.appendChild(h3Text);
li.appendChild(h3)
var p = document.createElement("p");
var pText = document.createTextNode(
"File type: ("
+ file.type + ") - " +
Math.round(file.size / 1024) + "KB"
);
p.appendChild(pText);
li.appendChild(p);
var divLoader = document.createElement("div");
divLoader.className = "loadingIndicator";
li.appendChild(divLoader);
fileList.appendChild(li);
fileQueue.push({
file : file,
li : li
});
}
}


var showImagePreview = function (ev) {
var div = document.createElement("div");
div.style["top"] = (ev.pageY + 10) + "px";
div.style["left"] = (ev.pageX + 10) + "px";
div.style["opacity"] = 0;
div.className = "imagePreview";
var img = new Image();
img.src = ev.target.src;
div.appendChild(img);
document.body.appendChild(div);
document.body.addEventListener("mousemove", movePreview, false);
preview = div;
fadePreviewIn();
}


var movePreview = function (ev) {
if (preview) {
preview.style["top"] = (ev.pageY + 10) + "px";
preview.style["left"] = (ev.pageX + 10) + "px";
}
}


var removePreview = function (ev) {
document.body.removeEventListener("mousemove", movePreview, false);
document.body.removeChild(preview);
}


var fadePreviewIn = function () {
if (preview) {
var opacity = preview.style["opacity"];
for (var i = 10; i < 250; i = i+10) {
(function () {
var level = i;
setTimeout(function () {
preview.style["opacity"] = opacity + level / 250;
}, level);
})();
}
}
}


var uploadFile = function (file, li) {
if (li && file) {
var xhr = new XMLHttpRequest(),
upload = xhr.upload;
upload.addEventListener("progress", function (ev) {
if (ev.lengthComputable) {
var loader = li.getElementsByTagName("div")[0];
loader.style["width"] = (ev.loaded / ev.total) * 100 + "%";
}
}, false);
upload.addEventListener("load", function (ev) {
var ps = li.getElementsByTagName("p");
var div = li.getElementsByTagName("div")[0];
div.style["width"] = "100%";
div.style["backgroundColor"] = "#0f0";
for (var i = 0; i < ps.length; i++) {
if (ps[i].className == "loader") {
ps[i].textContent = "Upload complete";
ps[i].style["color"] = "#3DD13F";
break;
}
}
}, false);
upload.addEventListener("error", function (ev) {console.log(ev);}, false);
xhr.open(
'POST',
'upload.php?UserId=<? echo $UserId ?>' <--------------------------------------------------------------------------------------------------------------------DOWN HERE PROBLEM
);
xhr.setRequestHeader("Cache-Control", "no-cache");
xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest");
xhr.setRequestHeader("X-File-Name", file.name);
xhr.send(file);
xhr.send(file);
}
}

}

 

UPLOAD.php

<?php
include "connect.php";
//$UserId='100001957015772';
$UserId = ($_GET['UserId']);





if(empty($UserId))
{//1
//echo "Error!";
return true;
}//
else
{//2


$query = "SELECT * FROM Access_Profile WHERE UserId = '$UserId'";
$results = mysql_query($query) or die("Data not found.");
//Get the number of results from the query.
$rows = mysql_num_rows($results);
//echo "CHECKING!";
if($rows == 1)
{
echo $UserId;
$root ="users/";
$folder ="/images/";
$filepath =$root.$UserId.$folder;


if(!file_exists($root))
{
mkdir($root,0777);


chmod($root,0777);
}
if(!file_exists($root.$UserId))
{
mkdir($root.$UserId,0777);


chmod($root.$UserId,0777);
}
if(!file_exists($root.$UserId.$folder))
{
mkdir($root.$UserId.$folder,0777);


chmod($root.$UserId.$folder,0777);
}




if(!file_exists($filepath))
{
mkdir($filepath,0777);
chmod($filepath,0777);
}


class File_Streamer
{
private $fileName;

private $contentLength;
private $path;

private $UserId;



public function __construct()
{
if (array_key_exists('HTTP_X_FILE_NAME', $_SERVER) && array_key_exists('CONTENT_LENGTH', $_SERVER)) {
$this->fileName = md5($_SERVER['HTTP_X_FILE_NAME']).'.jpeg';

$this->contentLength = $_SERVER['CONTENT_LENGTH'];





} else
{
throw new Exception("Error retrieving headers");
}
}

public function setDestination($p)
{
$this->path = $p;
}

public function receive($UserId)
{
if (!$this->contentLength > 0) {
throw new Exception('No file uploaded!');
}

file_put_contents(
$this->path . $this->fileName,
file_get_contents("php://input")
);





$setuserbackground= mysql_query("UPDATE WiiS_Background SET LargePicture = 'http://wiistream.com/$this->path . $this->fileName' WHERE UserId = '$UserId'");
$setuserbackgroundss= mysql_query("UPDATE WiiS_Background SET SmallPicture = 'http://wiistream.com/$this->path . $this->fileName' WHERE UserId = '$UserId'");
$setuserbackgrounds = mysql_query("INSERT INTO WiiS_Backgrounds(UserId,Thumb,Image) VALUES('$UserId','http://wiistream.com/$this->path . $this->fileName','http://wiistream.com/$this->path . $this->fileName')");
return true;
}
}
$ft = new File_Streamer();
$ft->setDestination($filepath);
$ft->receive($UserId);
}
else
{
echo "NO ONE IS IN THE DATABASE!";
}
}

 

DEFAULT.css

/* 
   Document   : default
   Created on : 25-Sep-2010, 08:33:58
   Author     : phil
   Description:
       Purpose of the stylesheet follows.
*/


/* 
  TODO customize this sample style
  Syntax recommendation http://www.w3.org/TR/REC-CSS2/
*/


body {
   font: 0.8em/1em "trebuchet MS", arial, sans-serif;
   color: #777;
}


h1 {
   font-size: 1.6em;
   margin: 30px 0;
   padding: 0;
}


h2 {
   font-size: 1.4em;
   padding: 0 0 6px 0;
   margin: 0;
   border-bottom: solid 1px #ccc;
}


h3 {
   font-size: 1.2em;
   margin: 0 0 10px 0;
   padding: 0;
}


p {
   margin: 0;
   padding: 0;
}


form {
   padding: 0 0 30px 0;
}


#wrap {
   width: 100%;
   margin: 0 auto;
}


#fileDrop {
   width: 300px;
   height: 300px;
   border: dashed 2px #ccc;
   background-color: #fefefe;
   float: left;
   color: #ccc;
}


   #fileDrop p {
       text-align: center;
       padding: 125px 0 0 0;
       font-size: 1.6em;
   }


#files {
   margin: 0 0 0 400px;
   width: 30%;
  height:30%
   padding: 20px 20px 40px 20px;
   border: solid 2px #ccc;
   background: #fefefe;
   min-height: 240px;
   position: relative;
}


#fileDrop,
#files {
   -moz-box-shadow: 0 0 20px #ccc;
}


#fileList {
   list-style: none;
width: 75px;
  height:75px;
   padding: 0;
   margin: 0;
}


   #fileList li {
 width: 70px;
  height:70px;
       margin: 0;
       padding: 10px 0;
       margin: 0;
       overflow: auto;
       border-bottom: solid 1px #ccc;
       position: relative;
   }


       #fileList li img {
           width: 65px;
  height:65px;

           border: solid 1px #999;
           padding: 6px;
           margin: 0 10px 0 0;
           background-color: #eee;
           display: block;
           float: left;
       }


#reset {
   position: absolute;
   top: 10px;
   right: 10px;
   color: #ccc;
   text-decoration: none;
}


#reset:hover {
   color: #333;
}


#upload {
   color: #fff;
   position: absolute;
   display: block;
   bottom: 10px;
   right: 10px;
   width: auto;
   background-color: #777;
   padding: 4px 6px;
   text-decoration: none;
   font-weight: bold;
   -moz-border-radius: 6px;
}


#upload:hover {
   background-color: #333;
}


.loader {
   position: absolute;
   bottom: 10px;
   right: 0;
   color: orange;
}


.loadingIndicator {
   width: 0%;
   height: 2px;
   background-color: orange;
   position: absolute;
   bottom: 0;
   left: 0;
}


.imagePreview {
   width:auto;
  height:auto;
   padding: 10px;
   border: solid 1px #ccc;
   position: absolute;
   background-color: white;
}


   .imagePreview img {

    width:auto;
  height:auto;
       display: block;
       margin: 0 auto;
   }

Edited by Mancent
Link to comment
https://forums.phpfreaks.com/topic/272327-dealing-with-java-and-php-problem/
Share on other sites

Firstly: Since you're already in the global scope, and (more importantly) you never change scope, the use of the global keyword is quite superfluous.

Also, you should strive to never use the global keyword, as it makes code interdependent and harder to maintain and reuse.

 

Secondly: The limitation of short tags has nothing to do with where in the code they were used. PHP ignores everything that's not PHP-code and has no idea that JS even exists, after all. What it depends upon is whether or not the server is configured to use the short-tags, which yours isn't. Like most servers today, which is why it's recommended to only use the full tags.

Edited by Christian F.
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.