Forum Moderators: coopster

Message Too Old, No Replies

Iframe, php upload problem

         

bakbai

8:13 pm on Jun 10, 2009 (gmt 0)

10+ Year Member



please can anyone help me sort this prob out. i have this sample which is use to upload image and load it at the same time. but it keeps given me error message.
here are the code below. there are three files, one imade_upload.php, upload.php and resize.php
<?php
/*************************************************
<snip>

*************************************************
?>
<html>
<head>
<title>Asynchronous image file upload without AJAX</title>
<style>
iframe {border-width: 0px;height: 600px;width: 400px;}
iframe.hidden {visibility: hidden;width:0px;height:0px;}
#main1 {overflow: hidden;margin: auto;width: 90%;height: 420px;border-style: solid;border-width: 1px;background-color: white;}
#images1 {width: 95%;height: auto;margin: 20px;}
#images1 div {margin: 10px;width: 130px;height: 130px;border-style: solid;border-width: 2px;border-color: #DEDFDE;float: left;overflow: hidden;}
#images1 div:hover {border-color: #D8F18C;}
#images1 img.load { margin-top:20px;}
.blacklink:link, .blacklink:alink, .blacklink:vlink, .blacklink:hover{color:#000000}
a.redlink{color: #96001C; text-decoration: none}
a:hover.redlink{color: #96001C; text-decoration: underline}
</style>
</head>
<body>
<table width="600" align="center" cellpadding="0" cellspacing="0" border="0">
<Tr><td><h1>PHPAsks.com</h1></td></Tr>
<Tr>
<td align="center" valign="middle">
<div id="main1">
<div id="iframe">
<iframe src="upload.php" frameborder="0"></iframe>
</div>
<div id="images1">
</div>
</div>
<div id="myDivLink"></div>
</td>
</Tr>
<tr>
<Td align="center">
Power by <a href="http://www.example.com" class="redlink">Example.com</a>
</Td>
</tr>
</table>
</body>
</html>


<?php
/**************************************
<snip>

***************************************
if($_GET['pic']){
$img = new img('upload/'.$_GET['pic']);
$width=$_GET["width"];
$height=$_GET["height"];
$img->resize();
$img->show();
}

class img {

var $image = '';
var $temp = '';

function img($sourceFile){
if(file_exists($sourceFile)){
$this->image = ImageCreateFromJPEG($sourceFile);
} else {
$this->errorHandler();
}
return;
}

function resize($width = 100, $height = 100, $aspectradio = true){
$o_wd = imagesx($this->image);
$o_ht = imagesy($this->image);
if(isset($aspectradio)&&$aspectradio) {
$w = round($o_wd * $height / $o_ht);
$h = round($o_ht * $width / $o_wd);
if(($height-$h)<($width-$w)){
$width =& $w;
} else {
$height =& $h;
}
}
$this->temp = imageCreateTrueColor($width,$height);
imageCopyResampled($this->temp, $this->image,
0, 0, 0, 0, $width, $height, $o_wd, $o_ht);
$this->sync();
return;
}

function sync(){
$this->image =& $this->temp;
unset($this->temp);
$this->temp = '';
return;
}

function show(){
$this->_sendHeader();
ImageJPEG($this->image);
return;
}

function _sendHeader(){
header('Content-Type: image/jpeg');
}

function errorHandler(){
echo "error";
exit();
}

function store($file){
ImageJPEG($this->image,$file);
return;
}

function watermark($pngImage, $left = 0, $top = 0){
ImageAlphaBlending($this->image, true);
$layer = ImageCreateFromPNG($pngImage);
$logoW = ImageSX($layer);
$logoH = ImageSY($layer);
ImageCopy($this->image, $layer, $left, $top, 0, 0, $logoW, $logoH);
}
}
?>


<?php
/**************************
<snip>

***************************

sleep(2);
$ftmp = $_FILES['image']['tmp_name'];
$oname = $_FILES['image']['name'];

$extenssion = strstr($oname, ".");
$fname = 'upload/'.$_FILES['image']['name'];
if(move_uploaded_file($ftmp, $fname)){
?>
<html><head><script>
var par = window.parent.document;
var images = par.getElementById('images1');
var imgdiv = images.getElementsByTagName('div')[<?=(int)$_POST['imgnum']?>];
var image = imgdiv.getElementsByTagName('img')[0];
imgdiv.removeChild(image);

var image_new = par.createElement('img');
image_new.src = 'resize.php?pic=<?=$oname?>';
image_new.className = 'loaded';

imgdiv.appendChild(image_new);

var bre=par.createElement('br');
imgdiv.appendChild(bre);

var myLink1 = par.createElement('a');
myLink1.href = "#";
myLink1.className = "blacklink";
myLink1.onclick=function(e){alert('x'+' Y: '+'usrname');}
myLink1.appendChild(par.createTextNode('Remove'));
imgdiv.appendChild(myLink1);

var oNewP = par.createElement('span');
var oText = par.createTextNode(" ");
oNewP.appendChild(oText);
imgdiv.appendChild(oNewP);

</script></head>
</html>
<?php
exit();
}
?><html><head>
<script language="javascript">
function upload(){
// hide old iframe
var par = window.parent.document;
var num = par.getElementsByTagName('iframe').length - 1;
var iframe = par.getElementsByTagName('iframe')[num];
iframe.className = 'hidden';

// create new iframe
var new_iframe = par.createElement('iframe');
new_iframe.src = 'upload.php';
new_iframe.frameBorder = '0';
par.getElementById('iframe').appendChild(new_iframe);

// add image progress
var images = par.getElementById('images1');
var new_div = par.createElement('div');
var new_img = par.createElement('img');
new_img.src = '/images/indicator.gif';
new_img.className = 'load';
new_div.appendChild(new_img);

images.appendChild(new_div);

var imgnum = images.getElementsByTagName('div').length - 1;
document.iform.imgnum.value = imgnum;
setTimeout(document.iform.submit(),5000);
}
</script>
<style>
#file {
width: 350px;
}
</style>
<head><body><center>
<form name="iform" action="" method="post" enctype="multipart/form-data">
<input id="file" type="file" name="image" onChange="upload()" />
<input type="hidden" name="imgnum" />
</form>
</center></html>

[edited by: jatar_k at 2:29 pm (utc) on June 11, 2009]
[edit reason] Removed specifics [/edit]

jatar_k

1:54 pm on Jun 11, 2009 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld bakbai,

what error message are you getting?
what exactly is happening when you try and execute your script?

bakbai

1:59 pm on Jun 11, 2009 (gmt 0)

10+ Year Member



I got this message
PHP Notice: Undefined index: image in C:\Inetpub\wwwroot\Glis\finance\TEST\upload.php on line 10 PHP Notice: Undefined index: image in C:\Inetpub\wwwroot\Glis\finance\TEST\upload.php on line 11 PHP Notice: Undefined index: image in C:\Inetpub\wwwroot\Glis\finance\TEST\upload.php on line 14

jatar_k

2:31 pm on Jun 11, 2009 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



they are only notices they shouldn't actually stop it, maybe

with errors we always work top down

what is on line 10? (which is in the first notice)

bakbai

2:39 pm on Jun 11, 2009 (gmt 0)

10+ Year Member



these are code from line 9 down
sleep(2);
$ftmp = $_FILES['image']['tmp_name'];
$oname = $_FILES['image']['name'];

$extenssion = strstr($oname, ".");
$fname = 'upload/'.$_FILES['image']['name'];
if(move_uploaded_file($ftmp, $fname)){

jatar_k

2:57 pm on Jun 11, 2009 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



what is your form element name that has the filename in it?

you could add this to the top of your script to see exactly what your script is receiving in the $_POST

echo '<pre>';
print_r($_POST);
echo '</pre>';

bakbai

3:43 pm on Jun 11, 2009 (gmt 0)

10+ Year Member



actually this his how its all setup. we have a file called image_upload which uses an iframe to post and retrieve the image loading it on the same page. everything is happening within a page. can i give you the files to see?

jatar_k

5:24 pm on Jun 11, 2009 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



well if it uses an iframe then it isn't actually happening on the same page, that would be 2 pages

you didn't look at my questions, the reason i asked about the form element name is

$_FILES['image']['tmp_name'];

the above is returning undefined, that could mean you aren't pulling from the right var name, a guess anyway, an answer to that question would help with what the next question should be

>> can i give you the files to see?

if we keep it in the thread then you can do the leg work and more members can see the thread to contribute.

bakbai

12:16 am on Jun 12, 2009 (gmt 0)

10+ Year Member



you're quiet right. let me go over it again and will get back to you shortly. you are being of help to me and many others