gpt4 book ai didi

javascript - 如何将两个php页面合并为一个php

转载 作者:行者123 更新时间:2023-12-01 00:42:19 25 4
gpt4 key购买 nike

我有index.phpupload.phpindex.php 提供选择图像的选项,一旦单击上传图像按钮,upload.php 就会被调用,然后创建一个目录并保存图像。

index.php

<!DOCTYPE html>
<html>

<head>
<title>Face Recognition</title>
<link href="main.css" rel="stylesheet" type="text/css" href="" />
</head>

<body>
<form enctype="multipart/form-data" action="upload.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="512000" />
Select file: <input name="userfile" type="file" />
<?php $file = isset($filename) ? $filename : ''; ?>
<input type="text" name="filename" value="<?php echo $file; ?>" />

<input type="submit" value="Upload Images" />


</form>

</body>



</html>

上传.php

<?php

$uploaddir = 'G:/dataset/' . $_POST['filename'] . "/";
// check if directory exists
if(!is_dir($uploaddir)){
mkdir($uploaddir);
}
$uploadfile = $uploaddir . basename($_FILES['userfile']['name']) ;

echo $uploadfile;
move_uploaded_file($_FILES['userfile']['tmp_name'],$uploadfile);

?>

调用upload.php时,页面重定向到upload.php页面,并显示上传文件路径。我不想重定向到 upload.php。我想将所有内容保留在单个index.php 文件中。这样上传图片后,它会在同一个index.php上显示上传文件路径。我们怎样才能实现它?

最佳答案

将两者放在一个 PHP 页面中,并将表单的操作设置为页面本身。它会成功的:)

<!DOCTYPE html>
<html>

<head>
<title>Face Recognition</title>
<link href="main.css" rel="stylesheet" type="text/css" href="" />
</head>

<body>
<form enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="512000" />
Select file: <input name="userfile" type="file" />
<?php $file = isset($filename) ? $filename : ''; ?>
<input type="text" name="filename" value="<?php echo $file; ?>" />

<input type="submit" value="Upload Images" />

</form>
</body>
</html>

<?php

if(!empty($_FILES['userfile']))
{
$uploaddir = 'G:/dataset/' . $_POST['filename'] . "/";

// check if directory exists
if(!is_dir($uploaddir)){
mkdir($uploaddir);
}
$uploadfile = $uploaddir . basename($_FILES['userfile']['name']) ;

echo $uploadfile;
move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile);
}

?>

关于javascript - 如何将两个php页面合并为一个php,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57635533/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com