php - 上传文件后重定向
问题描述
我正在尝试在文件上传完成后向页面添加重定向,但是我尝试了一些方法均未成功。我希望将用户重定向到特定页面或在弹出窗口上显示成功消息。指导将不胜感激。这就是我所拥有的:
$target_dir = "galleries/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
// 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 "File is an image - " . $check["mime"] . ".";
$uploadOk = 1;
} else {
echo "File is not an image.";
$uploadOk = 0;
}
}
// Check if file already exists
if (file_exists($target_file)) {
echo "Sorry, file already exists.";
$uploadOk = 0;
}
// Check file size
if ($_FILES["fileToUpload"]["size"] > 500000) {
echo "Sorry, your file is too large.";
$uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
$uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
}
}
解决方案
在else
块中代码的最后一部分,您可以添加一个header
,它将被重定向到给定的 url
....
else {
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
header('Location: http://google.com/'); // after the upload user will be redirected to the page
}
....
}
推荐阅读
- r - 如何在 R 中为 ggplot 的每个方面添加 R2?
- python - 如何按相似列折叠数据框中的行
- html - 将 div 从一侧到另一侧定位到从上到下
- anylogic - 在将代理移动到特定目的地时避免与其他对象发生碰撞
- javascript - 如何检测图片是否被点击
- python - Python源代码文件中空行的含义
- css - 需要在页面的一个部分中放入媒体查询以使其在另一个部分中工作?SCSS/SASS
- google-bigquery - 使用 UPDATE FROM 时出现 BigQuery 错误 6034920
- wifi - 如何选择在 ESP32 中使用的网络接口
- java - 打印用于描述两个数字相加的公式