PHP 根据HTML 复选框上传到特定文件夹
PHP upload to certain folder based on HTML checkbox
我是 PHP 的新手,仍在学习中,但我想知道我是否能够做这样的事情。我有一个基本的 HTML 表格,如下所示。
<form action="upload.php" method="post"
enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file"><br>
<input type="submit" name="submit" value="Submit">
<label><input type="checkbox" id="cb" value="checkb">Make Private.</label>
</form>
还有像下面这样的基本上传 PHP 文档。
<?php
if ($_FILES["file"]["size"] < 2097152) {
if ($_FILES["file"]["error"] > 0) {
header('Location: /');
}else {
echo "Original Name: " . $_FILES["file"]["name"] . "<br>";
echo "File Type: " . $_FILES["file"]["type"] . "<br>";
echo "File Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
$randprefix = hash('ripemd160', openssl_random_pseudo_bytes(32), false);
$filepath = "upload/" . $randprefix . $_FILES["file"]["name"];
if (file_exists($filepath)) {
echo $filepath . " already exists. ";
}else {
move_uploaded_file($_FILES["file"]["tmp_name"], $filepath);
echo "Link to file: " . '<a href = "' . $filepath . '">' . $filepath . '</a>' ;
}
}
}else {
echo "File too large.";
}
?>
我想问的是,是否可以在 HTML 表单上添加一个复选框,选中后文件将上传到名为 "private" 的文件夹,取消选中时文件将上传到名为 "public" 的文件夹。谢谢。
您可以使用这样的条件,以便在提交表单后只有您可以获得复选框的值。
if(isset($_POST['check']))
{
$filepath = "upload/private" . $randprefix . $_FILES["file"]["name"];
}
else
{
$filepath = "upload/public" . $randprefix . $_FILES["file"]["name"];
}
You have to place the name for the checkbox input type so that you can check in the upload.php page and then set the folder over there.
替换:
<label><input type="checkbox" id="cb" value="checkb">Make Private.</label>
有:
<label><input type="checkbox" id="cb" value="checkb" name="check">Make Private.</label>
说明:当用户在提交时选中复选框时,将检查复选框值,并使$filepath
为upload/private
或者如果用户在提交数据时 he/she 没有选中复选框,它将根据复选框值的选择更改代码。这样您就可以move_upload_file()
根据可用的选择。
<form action="upload.php" method="post" enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file"><br>
<input type="submit" name="submit" value="Submit">
<label><input type="checkbox" id="cb" name="checkb" value="checkb">Make Private.</label>
</form>
<?php
if ($_FILES["file"]["size"] < 2097152)
{
if ($_FILES["file"]["error"] > 0)
{
header('Location: /');
}
else
{
echo "Original Name: " . $_FILES["file"]["name"] . "<br>";
echo "File Type: " . $_FILES["file"]["type"] . "<br>";
echo "File Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
$randprefix = hash('ripemd160', openssl_random_pseudo_bytes(32), false);
if($_POST['checkb'] == "checkb")
{
$filepath = "private/" . $randprefix . $_FILES["file"]["name"];
}
else
{
$filepath = "upload/" . $randprefix . $_FILES["file"]["name"];
}
if (file_exists($filepath))
{
echo $filepath . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"], $filepath);
echo "Link to file: " . '<a href = "' . $filepath . '">' . $filepath . '</a>' ;
}
}
}
else
{
echo "File too large.";
}
?>
我认为这是我测试过的作品,你没有问题可以复制!
我是 PHP 的新手,仍在学习中,但我想知道我是否能够做这样的事情。我有一个基本的 HTML 表格,如下所示。
<form action="upload.php" method="post"
enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file"><br>
<input type="submit" name="submit" value="Submit">
<label><input type="checkbox" id="cb" value="checkb">Make Private.</label>
</form>
还有像下面这样的基本上传 PHP 文档。
<?php
if ($_FILES["file"]["size"] < 2097152) {
if ($_FILES["file"]["error"] > 0) {
header('Location: /');
}else {
echo "Original Name: " . $_FILES["file"]["name"] . "<br>";
echo "File Type: " . $_FILES["file"]["type"] . "<br>";
echo "File Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
$randprefix = hash('ripemd160', openssl_random_pseudo_bytes(32), false);
$filepath = "upload/" . $randprefix . $_FILES["file"]["name"];
if (file_exists($filepath)) {
echo $filepath . " already exists. ";
}else {
move_uploaded_file($_FILES["file"]["tmp_name"], $filepath);
echo "Link to file: " . '<a href = "' . $filepath . '">' . $filepath . '</a>' ;
}
}
}else {
echo "File too large.";
}
?>
我想问的是,是否可以在 HTML 表单上添加一个复选框,选中后文件将上传到名为 "private" 的文件夹,取消选中时文件将上传到名为 "public" 的文件夹。谢谢。
您可以使用这样的条件,以便在提交表单后只有您可以获得复选框的值。
if(isset($_POST['check']))
{
$filepath = "upload/private" . $randprefix . $_FILES["file"]["name"];
}
else
{
$filepath = "upload/public" . $randprefix . $_FILES["file"]["name"];
}
You have to place the name for the checkbox input type so that you can check in the upload.php page and then set the folder over there.
替换:
<label><input type="checkbox" id="cb" value="checkb">Make Private.</label>
有:
<label><input type="checkbox" id="cb" value="checkb" name="check">Make Private.</label>
说明:当用户在提交时选中复选框时,将检查复选框值,并使$filepath
为upload/private
或者如果用户在提交数据时 he/she 没有选中复选框,它将根据复选框值的选择更改代码。这样您就可以move_upload_file()
根据可用的选择。
<form action="upload.php" method="post" enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file"><br>
<input type="submit" name="submit" value="Submit">
<label><input type="checkbox" id="cb" name="checkb" value="checkb">Make Private.</label>
</form>
<?php
if ($_FILES["file"]["size"] < 2097152)
{
if ($_FILES["file"]["error"] > 0)
{
header('Location: /');
}
else
{
echo "Original Name: " . $_FILES["file"]["name"] . "<br>";
echo "File Type: " . $_FILES["file"]["type"] . "<br>";
echo "File Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
$randprefix = hash('ripemd160', openssl_random_pseudo_bytes(32), false);
if($_POST['checkb'] == "checkb")
{
$filepath = "private/" . $randprefix . $_FILES["file"]["name"];
}
else
{
$filepath = "upload/" . $randprefix . $_FILES["file"]["name"];
}
if (file_exists($filepath))
{
echo $filepath . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"], $filepath);
echo "Link to file: " . '<a href = "' . $filepath . '">' . $filepath . '</a>' ;
}
}
}
else
{
echo "File too large.";
}
?>
我认为这是我测试过的作品,你没有问题可以复制!