通过表单提交图像后成功或错误消息?

时间:2017-06-27 11:24:20

标签: php html forms message display

我是html和php的新手。我正在尝试使用一个表单,用于将图片上传到服务器。

<form class="myBtn" id="Upload" action="upload.php" method="post" enctype="multipart/form-data" style="visibility:hidden;"><br>
      Select template to upload:<br>
         <input type="file" name="fileToUpload" id="fileToUpload"><br>
         <input type="submit" value="Upload Image" name="submit">
</form>

这是我在HTML中的表单。

<?php $target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
$needheight = 355;
$needwidth = 275;

// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
    $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
    if($check !== false) {
        echo "Checked: File is an image! - " . $check["mime"] . ".";
        $uploadOk = 1;
    } else {
        echo "Checked: File is not an image!";
        $uploadOk = 0;
    }
}

if (file_exists($target_file)) {
    echo "Sorry, file already exists.";
    $uploadOk = 0;
}
if ($_FILES["fileToUpload"]["size"] > 1048576) {
  echo "Sorry, your file is too large.";
    $uploadOk = 0;
}
if($imageFileType != "png") {
    echo "Sorry, only .PNG is allowed.";
    $uploadOk = 0;
}

if ($uploadOk == 0) {
    echo "Sorry, your file was not uploaded.";
}

else {
    if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
        echo "Your cape template". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
    } else {
        echo "Sorry, there was an error uploading your template. Try again.";
    }
}

?>

在这里你可以看到我的PHP代码。我真的很抱歉这是一个WoC(代码墙),但我真的不知道你需要哪些代码部分来帮助我。

我的php工作花了我很多年,但我做到了。我可以成功将模板上传到我的服务器。 但目前,在成功提交模板后,我将被重定向到另一个空白页面,显示: “已检查:文件是图像! - image / png!您的海角模板已上传!”

有没有办法不重定向到空白页但在主网站上显示成功或错误回应?

非常感谢,我现在卡住了。

3 个答案:

答案 0 :(得分:0)

从表单中删除操作,以便页面在同一页面上发布。还有你的 html <form>编写来自php的代码,该代码目前位于upload.php

<?php $target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
$needheight = 355;
$needwidth = 275;

// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
    $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
    if($check !== false) {
        echo "Checked: File is an image! - " . $check["mime"] . ".";
        $uploadOk = 1;
    } else {
        echo "Checked: File is not an image!";
        $uploadOk = 0;
    }
}

if (file_exists($target_file)) {
    echo "Sorry, file already exists.";
    $uploadOk = 0;
}
if ($_FILES["fileToUpload"]["size"] > 1048576) {
  echo "Sorry, your file is too large.";
    $uploadOk = 0;
}
if($imageFileType != "png") {
    echo "Sorry, only .PNG is allowed.";
    $uploadOk = 0;
}

if ($uploadOk == 0) {
    echo "Sorry, your file was not uploaded.";
}

else {
    if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
        echo "Your cape template". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
    } else {
        echo "Sorry, there was an error uploading your template. Try again.";
    }
}

?>

<form class="myBtn" id="Upload" action="" method="post" enctype="multipart/form-data" style="visibility:hidden;"><br>
      Select template to upload:<br>
         <input type="file" name="fileToUpload" id="fileToUpload"><br>
         <input type="submit" value="Upload Image" name="submit">
</form>

答案 1 :(得分:0)

您可能会这样做 - 将所有代码保留在一个页面上,并在提交表单后显示相关消息。

np.random.seed(123)
N = 1000000
df = pd.DataFrame({'ResponseType': np.random.choice(['ok','ok2','no2', 'no'], N),
                   'TYPE':['mod'] * N,
                   'OID':np.random.randint(100000, size=N)})
print (df)

In [285]: %timeit (df[~df['OID'].isin(df.loc[df['ResponseType'].isin(['no','no2']), 'OID'])])
10 loops, best of 3: 67.2 ms per loop

In [286]: %timeit (df[~df['OID'].isin(df.loc[df['ResponseType'].isin(['no','no2']), 'OID'].unique())])
10 loops, best of 3: 69.5 ms per loop

#zipa solution
In [287]: %timeit (df[~df['OID'].isin(df[df['ResponseType'].isin(['no', 'no2'])]['OID'])])
10 loops, best of 3: 91.5 ms per loop

#groupby solution :(
In [288]: %timeit (df.groupby('OID').filter(lambda x: ~((x['ResponseType']=='no') |  (x['ResponseType']=='no2')).any() ))
1 loop, best of 3: 1min 54s per loop

答案 2 :(得分:0)

很高兴你自己完成了PHP部分。你已经使用了两个文件用于HTML和PHP用作一个文件并使用$ _SERVER [&#39; PHP_SELF&#39;]。

<form class="myBtn" id="Upload" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data"><br>
 <label>Select template to upload:</label><br>
     <input type="file" name="fileToUpload" id="fileToUpload"><br>
     <input type="submit" value="Upload Image" name="submit">
</form>

<?php 
$message = "";
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
$target_dir = "/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
$needheight = 355;
$needwidth = 275;
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
if($check !== false) {
    echo $message = "Checked: File is an image! - " . $check["mime"] . ".";
    $uploadOk = 1;
} else {
    echo $message = "Checked: File is not an image!";
    $uploadOk = 0;
}
if (file_exists($target_file)) {
echo $message ="Sorry, file already exists.";
$uploadOk = 0;
}
if ($_FILES["fileToUpload"]["size"] > 1048576) {
echo $message ="Sorry, your file is too large.";
$uploadOk = 0;
}
if($imageFileType != "png") {
echo $message ="Sorry, only .PNG is allowed.";
$uploadOk = 0;
}

if ($uploadOk == 0) {
echo $message ="Sorry, your file was not uploaded.";
}

else {
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
echo $message ="Your cape template". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
} else {
echo $message ="Sorry, there was an error uploading your template. Try again.";
}
}
}
?>

<script>
var alert_msg = "<?php echo $message; ?>";
<?php if(strlen($message) > 1) { ?>
alert(alert_msg);
<?php } ?>
</script>

我使用提醒来显示您的状态消息。希望它有效。

请删除表单中隐藏的样式。(为什么会这样。)