在PHP编码一些未定义的变量时出错


getting errors in php coding something undefined variable

我在此代码中收到以下错误: Strict Standards: Only variables should be passed by reference in C:'xampp'htdocs'uploader'upload.php on line 12 Success

Notice: Undefined variable: fulltarget in C:'xampp'htdocs'uploader'upload.php on line 101其他错误

Notice: Undefined variable: fulltarget in C:'xampp'htdocs'uploader'upload.php on line 101

这是粘贴链接以查看整个代码: http://pastebin.com/JKegmNHC

这里还有

代码也可用..你可以通过这里或使用pastebin链接从相关行中获取错误指示。

这是代码:

    <?php
$submit=$_POST['sub'];
if(isset($submit))
{
$name=$_FILES['img']['name'];
$type=$_FILES['img']['type'];
$size=($_FILES['img']['size'])/1024;
$ext=end(explode('.',$name));
if (($ext == "gif")
|| ($ext == "jpeg")
|| ($ext == "jpg")
|| ($ext =="png")
&& ($size > 30))
{

############################## File Renaming ###################################################
$newname=uniqid();
//$ext=end(explode('.',$name));
$fullname=$newname.".".$ext;
$target="pics/";
$fulltarget=$target.$fullname;
if(move_uploaded_file($_FILES['img']['tmp_name'],$fulltarget))
{
echo "Success";
}
else
{
echo "Failed";
}
############################## File Renaming end ###################################################
}
else{
echo "not successful";
}
}
?>
<!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>
<link rel="stylesheet" type="text/css" href="abhi.css" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Php Image Uploader</title>
</head>
<body>
<div id="a1">
<form name="frm" method="post" enctype="multipart/form-data">
<input type="file" name="img" /><br />
<input type="submit" name="sub" value="Store" />
</form>
</div>
<div id="a2">
<?php echo "
<html>
<head>
<title>Aviary Photo Editer</title>
<!-- Load Feather code -->
<script type='text/javascript' src='http://feather.aviary.com/js/feather.js'></script>
<!-- Instantiate Feather -->
<script type='text/javascript'>
var featherEditor = new Aviary.Feather({
apiKey: 'ceegvx4siylhayrr',
apiVersion: 3,
theme: 'dark', // Check out our new 'light' and 'dark' themes!
tools: 'enhance,frames,crop,orientation,brightness,saturation,sharpness,draw,redeye,blemish,effects,stickers,resize,focus,contrast,warmth,colorsplash,text,whiten',
appendTo: '',
onSave: function(imageID, newURL) {
    var img = document.getElementById(imageID);
    img.src = newURL;
},
onError: function(errorObj) {
    alert(errorObj.message);
}
});
function launchEditor(id, src) {
featherEditor.launch({
    image: id,
    url: src
 });
return false;
}
 </script>
</head>
<body>
<div id='injection_site'></div>
<img id='image1' src='$fulltarget'/>
<p><input type='image' src='http://images.aviary.com/images/edit-photo.png' value='Edit photo' onclick='"return launchEditor('image1', '$fulltarget');'" /></p>";
?>

</body>
</html>

根据 [PHP: end - Manual],当你使用 end() 时,它会传递数组中的最后一个元素作为引用

您可以尝试以下几种方法:

  1. 不要在end()中使用explode()。当您声明$name时尝试进行爆炸:
    $name = explode( '.', $_FILES['img']['name'] );

  2. 使用 array_slice()list() 代替 end()
    list($ext) = array_slice( explode( '.' , $name ), -1 );

任何一个都可能起作用。

您收到"注意:未定义的变量"错误,因为您尝试使用变量而不先初始化它。在您的 HTML 中使用"$fulltarget",但只有在您提交表单后才会设置它。如果你想摆脱这个通知,你应该在你的PHP开始标签后面放一些类似$fulltarget = '';的东西。

如果您查看 PHP end() 函数文档 http://php.net/manual/en/function.end.php 您将看到该函数通过引用将数组作为参数。要修复此错误,您应该将explode()结果另存为变量,然后将该变量传递给end()函数。例如:$arr = explode('.', $name); $ext = end($arr);