gpt4 book ai didi

PHP 上传文件如果无效设置其他值而不是随机词

转载 作者:行者123 更新时间:2023-11-29 01:49:50 24 4
gpt4 key购买 nike

这个脚本工作正常..但我只是想知道如果没有选择图像值 "none" 而不是 random words 我如何更改插入数据库的值像15199660151398490708

<?php
$name = ''; $type = ''; $size = ''; $error = '';
$upload = "asssets/img/";
$new = time().rand();
$target = $upload . $new . basename( $_FILES['photo']['name']);

require_once('includes/config.php');

// If form submitted, insert values into the database.
if(isset($_POST['submit']) && !empty($_POST['submit'])) {

$pic=$new .($_FILES['photo']['name']);

$query = "INSERT INTO posting(photo) VALUES('$pic')";
$result = mysql_query($query);
//Writes the photo to the server
if (($_FILES["photo"]["type"] == "image/gif") ||
($_FILES["photo"]["type"] == "image/jpeg") ||
($_FILES["photo"]["type"] == "image/png") ||
($_FILES["photo"]["type"] == "image/pjpeg")) {
$filename = compress_image($_FILES["photo"]["tmp_name"], $target, 80);
{
echo "";
}
}else
echo "";{
?><form method="post" style="padding:0px;margin:0px;display: block;" enctype="multipart/form-data" autocomplete="off">

<input name="photo" type="file" accept="image/*">
<input name="submit" class="button_post" id="click" type="submit" value=" Post" />

</form><?php } ?>

This is example image mysql phpmyadmin

最佳答案

检查文件输入是否为空,如果出现问题,则返回错误代码,因为它可能为 0。

if ($_FILES['photo']['size'] == 0 && $_FILES['photo']['error'] == 0)
{
$pic = "none";
}
else
{
$pic = $new .($_FILES['photo']['name']);
}

代码将检查文件输入是否为空。如果为空,它会将 $pic 变量值设置为“none”,否则它将生成随机数。

那么整体代码将是:

<?php
$name = ''; $type = ''; $size = ''; $error = '';
$upload = "asssets/img/";
$new = time().rand();
$target = $upload . $new . basename( $_FILES['photo']['name']);

require_once('includes/config.php');

// If form submitted, insert values into the database.
if(isset($_POST['submit']) && !empty($_POST['submit'])) {

if ($_FILES['photo']['size'] == 0 && $_FILES['photo']['error'] == 0)
{
$pic = "none";
}
else
{
$pic = $new .($_FILES['photo']['name']);
}

$query = "INSERT INTO posting(photo) VALUES('$pic')";
$result = mysql_query($query);
//Writes the photo to the server
if (($_FILES["photo"]["type"] == "image/gif") ||
($_FILES["photo"]["type"] == "image/jpeg") ||
($_FILES["photo"]["type"] == "image/png") ||
($_FILES["photo"]["type"] == "image/pjpeg")) {
$filename = compress_image($_FILES["photo"]["tmp_name"], $target, 80);
{
echo "";
}
}else
echo "";{
?><form method="post" style="padding:0px;margin:0px;display: block;" enctype="multipart/form-data" autocomplete="off">

<input name="photo" type="file" accept="image/*">
<input name="submit" class="button_post" id="click" type="submit" value=" Post" />

</form><?php } ?>

关于PHP 上传文件如果无效设置其他值而不是随机词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49063078/

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