我有2个文件,分别是 upload.php 和 process.php 。
在我的 upload.php 文件中,我的表单是这样的:
<form action="upload.php" method="post" enctype="multipart/form-data">
<h5>Upload File</h5>
<p>.jpg and .png only.</p>
<input type="file" name="myfile" required> <br><br>
<h5>Choose action</h5>
<select class="form-control" name="action"required>
<option>No action (Finished)</option>
<option>Please Check</option>
</select><br>
<h5>Who are you</h5>
<select class="form-control" name="uploader" required>
<option>Person A</option>
<option>Person B</option>
<option>Person C</option>
</select><br>
<h5>Remark</h5>
<textarea name="remark" style="resize:none; width:100%; height:100px;">-</textarea><br><br>
<button type="submit" name="save" class="btn btn-dark btn-block">Upload</button><br>
</form>
在我的 process.php 中:
if (isset($_POST['save'])) { // if save button on the form is clicked
// name of the uploaded file
$filename = $_FILES['myfile']['name'];
$uploader = $_POST['uploader'];
$action = $_POST['action'];
$remark = $_POST['remark'];
// destination of the file on the server
$destination = 'uploads/' . $filename;
// get the file extension
$extension = pathinfo($filename, PATHINFO_EXTENSION);
// the physical file on a temporary uploads directory on the server
$file = $_FILES['myfile']['tmp_name'];
$size = $_FILES['myfile']['size'];
if (!in_array($extension, ['jpg','png'])) {
echo '<script language="javascript">';
echo 'alert("Upload failed! Your file extension must be .jpg or .png")';
echo '</script>';
} elseif ($_FILES['myfile']['size'] > 1000000) { // file shouldn't be larger than 1Megabyte
echo "File too large!";
} else {
// move the uploaded (temporary) file to the specified destination
if (move_uploaded_file($file, $destination)) {
$sql = "INSERT INTO files (name, size, uploader, action, remark, downloads) VALUES ('$filename', $size, '$uploader', '$action', '$remark', 0)";
if (mysqli_query($conn, $sql)) {
echo '<script language="javascript">';
echo 'alert("File uploaded successfully.")';
echo '</script>';
}
} else {
echo '<script language="javascript">';
echo 'alert("Failed to upload file.")';
echo '</script>';
}
}
}
我如何实现:如果文件上传成功,将显示looks like this的模式框,表明上传成功?现在,就像您在代码中看到的那样,我只使它显示警报。
答案 0 :(得分:0)
我想我刚刚解决了自己的问题。
我将模态添加到我的upload.php中,然后添加此javascript,以便可以通过直接url触发模态框。
<script type="text/javascript">
$(document).ready(function() {
if(window.location.href.indexOf('#success') != -1) {
$('#success').modal('show');
}
});
</script>
然后在process.php中,如果上传成功,则重定向到mywebsite.com/upload.php#success
header(Location: 'upload.php#success')
如果有人有更好的解决方案,请随时分享。 :)