使用php上传文件后如何重定向到成功页面?
我试图在成功上传后重定向到另一个页面。
所以我在 stackoverflow 上搜索了类似的答案,但似乎没有解决我的问题。
这是我的表单:
<form enctype="multipart/form-data"
action="<?php print $_SERVER['PHP_SELF']?>" method="post">
<p><input type="hidden" name="MAX_FILE_SIZE" value="9000000" /> <input
type="file" name="pdfFile" /><br />
<br />
<input type="submit" value="upload" /></p>
</form>
这是我的 php,其中包含重定向的标头
<?php
if ( isset( $_FILES['pdfFile'] ) ) {
if ($_FILES['pdfFile']['type'] == "application/pdf") {
$source_file = $_FILES['pdfFile']['tmp_name'];
$dest_file = "upload/".$_FILES['pdfFile']['name'];
if (file_exists($dest_file)) {
print "The file name already exists!!";
}
else {
move_uploaded_file( $source_file, $dest_file )
or die ("Error!!");
if($_FILES['pdfFile']['error'] == 0) {
print "Pdf file uploaded successfully!";
print "<b><u>Details : </u></b><br/>";
print "File Name : ".$_FILES['pdfFile']['name']."<br.>"."<br/>";
print "File Size : ".$_FILES['pdfFile']['size']." bytes"."<br/>";
print "File location : upload/".$_FILES['pdfFile']['name']."<br/>";
header('Location: success.php');
}
}
}
else {
if ( $_FILES['pdfFile']['type'] != "application/pdf") {
print "Error occured while uploading file : ".$_FILES['pdfFile']['name']."<br/>";
print "Invalid file extension, should be pdf !!"."<br/>";
print "Error Code : ".$_FILES['pdfFile']['error']."<br/>";
}
}
}
?>
I am trying to redirect to another page after a successful upload.
So I searched for similar answers on stackoverflow but non seems to solve my problem.
This is my form:
<form enctype="multipart/form-data"
action="<?php print $_SERVER['PHP_SELF']?>" method="post">
<p><input type="hidden" name="MAX_FILE_SIZE" value="9000000" /> <input
type="file" name="pdfFile" /><br />
<br />
<input type="submit" value="upload" /></p>
</form>
This is my php which includes the header that redirects
<?php
if ( isset( $_FILES['pdfFile'] ) ) {
if ($_FILES['pdfFile']['type'] == "application/pdf") {
$source_file = $_FILES['pdfFile']['tmp_name'];
$dest_file = "upload/".$_FILES['pdfFile']['name'];
if (file_exists($dest_file)) {
print "The file name already exists!!";
}
else {
move_uploaded_file( $source_file, $dest_file )
or die ("Error!!");
if($_FILES['pdfFile']['error'] == 0) {
print "Pdf file uploaded successfully!";
print "<b><u>Details : </u></b><br/>";
print "File Name : ".$_FILES['pdfFile']['name']."<br.>"."<br/>";
print "File Size : ".$_FILES['pdfFile']['size']." bytes"."<br/>";
print "File location : upload/".$_FILES['pdfFile']['name']."<br/>";
header('Location: success.php');
}
}
}
else {
if ( $_FILES['pdfFile']['type'] != "application/pdf") {
print "Error occured while uploading file : ".$_FILES['pdfFile']['name']."<br/>";
print "Invalid file extension, should be pdf !!"."<br/>";
print "Error Code : ".$_FILES['pdfFile']['error']."<br/>";
}
}
}
?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
请记住,在发送任何实际输出之前,必须调用
header()
,无论是通过普通 HTML 标记、文件中的空行还是从 PHP 发送。PHP 标头(来自 php.net)
WITH PHP:
如果你愿意要使用输出,首先使用
Html
或Javascript
代码WITH HTML:
使用 JavaScript:
Remember that
header()
must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP.PHP HEADER (From php.net)
WITH PHP:
If you want to use outputs first use
Html
orJavascript
codeWITH HTML:
WITH JAVASCRIPT: