Display die('message') problem
How do i display the die() message in if($allowed) at the same place as move_uploaded_file result?
<?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>
Javascript to display move_uploaded_file result:
function stopUpload(success,filename){
var result = '';
if (success == 1){
result = '<span 开发者_Python百科class="msg">Great!<\/span><br/><br/>';
}
else {
result = '<span class="emsg">Not so great.<\/span><br/><br/>';
}
do you mean in the same condition?
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");
}
Add a third $result
condition (-1, for example), then instead of die(message);
, do $result = -1; exit(0);
. Change the JavaScript so else if (success == -1) { result = 'Wrong file type!' }
精彩评论