显示模具('消息')问题

时间:2011-01-09 21:29:56

标签: php javascript

如何在move_uploaded_file结果的同一位置显示if($ allowed)中的die()消息?

<?php
  $destination_path = $_SERVER['DOCUMENT_ROOT'].'/uploads/';

  $allowed[] = 'image/gif'; 
  $allowed[] = 'image/jpeg';
  $allowed[] = 'image/pjpeg'; 
  $allowed[] = 'image/png'; 

  if (!in_array($_FILES['myfile']['type'], $allowed)) { 
    die('Wrong file type!'); 
  } 

  $result = 0;

  $now = time();

  $ext = end(explode(".", $_FILES['myfile']["name"]));

  $filename = ( $_FILES['myfile'][0].$now.".".$ext);

  if(@move_uploaded_file($_FILES['myfile']['tmp_name'], $destination_path .$filename)) {
     $result = 1;
  }

  sleep(1);
?>

<script language="javascript" type="text/javascript"> window.top.window.stopUpload('<? php echo $result; ?>', '<?php echo $filename; ?>');</script>   

显示move_uploaded_file结果的Javascript:

 function stopUpload(success,filename){
  var result = '';
  if (success == 1){
     result = '<span class="msg">Great!<\/span><br/><br/>';
  }
  else {
     result = '<span class="emsg">Not so great.<\/span><br/><br/>';
  }

2 个答案:

答案 0 :(得分:0)

添加第三个$result条件(例如,-1),然后执行die(message);,而不是$result = -1; exit(0);。更改JavaScript else if (success == -1) { result = 'Wrong file type!' }

答案 1 :(得分:0)

你的意思是在同样的条件下吗?

if (in_array($_FILES['myfile']['type'], $allowed) && @move_uploaded_file($_FILES['myfile']['tmp_name'], $destination_path .$filename)) { 
  $result = 1;
} else {
  die("Error while uploding file");
}