gpt4 book ai didi

PHP-file_get_contents() : Filename cannot be empty

转载 作者:可可西里 更新时间:2023-10-31 22:40:53 41 4
gpt4 key购买 nike

有人可以帮我处理这个错误吗?我不知道用什么方法或方法来摆脱这个错误。我是 php 的新手并开始学习它。有人可以给我想法吗?

这里是错误: enter image description here

这是我的 PHP 代码。

<?php

include_once('connection.php');

$newsid = $_GET['news_id'];

if(isset($_POST['esubmit'])){
/* create a prepared statement */
if ($stmt = mysqli_prepare($con, "SELECT * FROM news WHERE news_id = ? LIMIT 1")) {
/* bind parameters */
mysqli_stmt_bind_param($stmt, "s", $newsid);

/* execute query */
mysqli_stmt_execute($stmt);

/* get the result set */
$result = mysqli_stmt_get_result($stmt);

/* fetch row from the result set */
$row = mysqli_fetch_array($result);
}

}


if(isset($_POST['update'])){

if(isset($_FILES['image'])){
$file=$_FILES['image']['tmp_name'];
/* Below is the line 30 causing the error*/
$image = addslashes(file_get_contents($_FILES['image']['tmp_name']));
$image_name= addslashes($_FILES['image']['name']);
move_uploaded_file($_FILES["image"]["tmp_name"],"img/" . $_FILES["image"]["name"]);
$newsimage="img/" . $_FILES["image"]["name"];

$title = $_POST['titles'];
$date = $_POST['dates'];
$content = $_POST['contents'];

$sql ="UPDATE news SET news_title ='$title', news_date ='$date', news_content = '$content', news_image ='$newsimage' WHERE news_id = '$newsid'";
mysqli_query($con, $sql);
echo "oh it worked ";
}
else{
$title = $_POST['titles'];
$date = $_POST['dates'];
$content = $_POST['contents'];
$sql ="UPDATE news SET news_title ='$title', news_date ='$date', news_content = '$content' WHERE news_id = '$newsid'";
mysqli_query($con, $sql);
echo "oh it worked again ";
}

}
?>
<!DOCTYPE HTML>
<html>
<head>
</head>
<body>

<?php

if(isset($_POST['esubmit'])){
?>

<form method="post" action ="edit2.php?news_id=<?php echo $row['news_id']; ?>" enctype="multipart/form-data">
Title<input type ="text" name ="titles" value="<?php echo $row['news_title']; ?>"/><br>
Date<input type ="text" name="dates" value="<?php echo $row['news_date']; ?>" /><br>
Content<textarea name="contents"><?php echo $row['news_content']; ?></textarea>
<input class="form-control" id="image" name="image" type="file" accept="image/*" onchange='AlertFilesize();'/>
<img id="blah" src="<?php echo $row['news_image']; ?>" alt="your image" style="width:200px; height:140px;"/>

<input type="submit" name="update" value="Update" />
</form>

<?php
}

?>

<script src="js/jquery-1.12.4.min.js"></script>
<script src="js/bootstrap.min.js"></script>
<script type="text/javascript">
function readURL(input) {
if (input.files && input.files[0]) {
var reader = new FileReader();

reader.onload = function (e) {
$('#blah').attr('src', e.target.result);
}

reader.readAsDataURL(input.files[0]);
}
}

$("#image").change(function(){
readURL(this);
});
</script>
</body>
</html>

最佳答案

为什么要在(临时)文件名中添加斜杠?

你的第 30 行:

$image= addslashes(file_get_contents($_FILES['image']['tmp_name']));

所以要删除错误警告:

if(!empty($_FILES['image']['tmp_name']) 
&& file_exists($_FILES['image']['tmp_name'])) {
$image= addslashes(file_get_contents($_FILES['image']['tmp_name']));
}
  • 你可以/应该用这段代码做很多其他事情,但我不能和你讨论太多细节,但基本上你应该检查 $_FILES['image ']['error'] == 0 以确保代码仅在文件已成功上传时运行。

  • 替换

      if(isset($_FILES['image'])){

错误检查:

   if($_FILES['image']['error'] == 0){

这意味着只有 OK 上传的文件才会运行 IF 语句内容

  • 停止添加斜杠,不需要它。

  • 为您的 SQL 查询使用准备好的语句。

  • Move_uploaded_file 在完美的世界中应该被赋予绝对路径而不是相对路径。

  • 您是否意识到您正在 file_get_contents 获取文件中的数据,而不是引用而是实际的二进制文件文件数据。这看起来不是您在此阶段需要做的事情。您的 $image 值没有在您提供的代码中明确使用,正如 apokryfos 正确指出的那样,您实际上是在向检索到的图像文件数据中添加斜杠。这只会使您的 $image 乱码。

关于PHP-file_get_contents() : Filename cannot be empty,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38146473/

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