PHP - file_get_contents(): 文件名不能为空
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38146473/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP - file_get_contents(): Filename cannot be empty
提问by nethken
Can someone help me to handle this error? I don't know what method or way to get rid of this error. Im new to php and starting to learn it. Can someone give me ideas?
有人可以帮我处理这个错误吗?我不知道用什么方法或方法来摆脱这个错误。我是 php 新手并开始学习它。有人可以给我想法吗?
here is my php code.
这是我的 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>
回答by Martin
Why are you adding slahes to your (temporary) filename?
为什么要在(临时)文件名中添加斜线?
your line 30:
您的第 30 行:
$image= addslashes(file_get_contents($_FILES['image']['tmp_name']));
$image= addslashes(file_get_contents($_FILES['image']['tmp_name']));
So to remove the error warning:
所以要删除错误警告:
if(!empty($_FILES['image']['tmp_name'])
&& file_exists($_FILES['image']['tmp_name'])) {
$image= addslashes(file_get_contents($_FILES['image']['tmp_name']));
}
There is a LOT of other things you can / should do with this code but I can't go over it in too much detail with you, but basically you should check that
$_FILES['image']['error'] == 0
to ensure that code only runs if the file has been successfully uploaded.Replace
if(isset($_FILES['image'])){
您可以/应该使用此代码做很多其他事情,但我不能与您详细讨论,但基本上您应该检查
$_FILES['image']['error'] == 0
以确保代码仅在文件已成功上传后运行。代替
if(isset($_FILES['image'])){
With an error check:
通过错误检查:
if($_FILES['image']['error'] == 0){
Which will mean that only an OK uploaded file will then run the IF
statement contents
这意味着只有一个 OK 上传的文件才会运行IF
语句内容
Stop adding slashes, it's not needed.
Use prepared statements for your SQL queries.
Move_uploaded_file
should in a perfect world be given an absolute path rather than a relative path.Do you realise that you're
file_get_contents
is getting the datain a file, not a referece but the actual binary file data. This looks like it's not what you need to be doing at this stage. Your$image
value isn't clearly used in the code you provide and as rightly pointed out by apokryfos, you're actually adding slashes to the retrieved filedata of the image. This is going to simply make your$image
a garbled mess.
停止添加斜杠,这是不需要的。
为您的 SQL 查询使用准备好的语句。
Move_uploaded_file
应该在完美世界中给出绝对路径而不是相对路径。您是否意识到您正在
file_get_contents
获取文件中的数据,而不是引用,而是实际的二进制文件数据。这看起来不是你在这个阶段需要做的。您$image
提供的代码中没有明确使用您的值,正如apokryfos正确指出的那样,您实际上是在检索到的图像文件数据中添加斜杠。这只会让你$image
的乱码一团糟。
回答by MosesK
For the file_get_contents($_FILES['image']['tmp_name'])to give you an error of cannot be emptyit means that it has has no file or nothing has been attached to it in the html part of the code. So check if first of all you have attached anything to it by echoing the value file_get_contents($_FILES['image']['tmp_name']).
对于file_get_contents($_FILES['image']['tmp_name'])给你一个不能为空的错误,这意味着它没有文件或在代码的 html 部分没有附加任何文件。因此,首先检查您是否通过回显值file_get_contents($_FILES['image']['tmp_name'])附加了任何内容 。
回答by Arunprasanth
<?php
ini_set( 'display_errors', 0 );
error_reporting( E_ALL );
?>
回答by NKol
If others still have this issue. That fixed it for me. Change your php.ini file to
如果其他人还有这个问题。那为我修好了。将您的 php.ini 文件更改为
; Maximum allowed size for uploaded files.
upload_max_filesize = 40M
; Must be greater than or equal to upload_max_filesize
post_max_size = 40M
Explanation: The default setting for the php.ini file is
说明:php.ini 文件的默认设置是
; Maximum allowed size for uploaded files.
upload_max_filesize = 2M
That's why you can't upload images larger than 2 MB. For Windows you can just search for php.ini in the search bar in the menu bar at the bottom. Then edit the properties with an editor.
这就是您不能上传大于 2 MB 的图像的原因。对于 Windows,您只需在底部菜单栏中的搜索栏中搜索 php.ini。然后使用编辑器编辑属性。