php 警告:文件大小():img.jpg 统计失败

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28302953/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 23:47:42  来源:igfitidea点击:

Warning: filesize(): stat failed for img.jpg

phpfilesize

提问by yuval

I am trying to get the file size of an image and I keep getting Warning: filesize(): stat failed for img.jpg

我正在尝试获取图像的文件大小,但我不断收到 Warning: filesize(): stat failed for img.jpg

This is what I did:

这就是我所做的:

$path = $_FILES['profile']['name'];
$path = iconv('UTF-8', 'ISO-8859-1',$path);
if (!in_array(pathinfo($path,PATHINFO_EXTENSION),$allowed)) {
    return "file";
} elseif (filesize($path)>(1024*600))

I am able to get the file extension no problem but the filesize()just doesn't seem to work. I have been reading a bit and did find thisbut it did not solve the problem. Any help is much appreciated!

我能够毫无问题地获得文件扩展名,但filesize()似乎不起作用。我一直在阅读并确实找到了这个,但它并没有解决问题。任何帮助深表感谢!

采纳答案by Marc B

['name']in the $_FILES array is the name of the file on the CLIENT machine. It is information only, and has absolutely no relevance to what's actually stored on your server. You need to look at ['tmp_name'], which is where PHP has stored the file temporarily on the server, after the upload completed:

['name']$_FILES 数组中是客户端计算机上的文件名。它只是信息,与服务器上实际存储的内容完全无关。['tmp_name']上传完成后,您需要查看,这是 PHP 在服务器上临时存储文件的位置:

$path = $_FILES['profile']['tmp_name'];
                          ^^^^^^^^^^^^

回答by geekman

$_FILES['profile']['name']has just name of the file.. you need to access

$_FILES['profile']['name']已经将文件名的..你需要访问

$_FILES['profile']['tmp_name'] 

will give you the temporary pathof the file on your system. Here is http://php.net/manual/en/reserved.variables.files.php

将为您提供系统上文件的临时路径。这是 http://php.net/manual/en/reserved.variables.files.php

also you can access size of file with

你也可以访问文件的大小

$_FILE['profile']['size']

回答by Luca Abbati

echo "---- NULL ---------------\n";
$path = null;
echo "File size is: " . filesize($path) . "\n";

echo "---- FILE EXISTS --------\n";
$path = '/home/luca/Scrivania/file_that_exists.jpg';
echo "File size is: " . filesize($path) . "\n";

echo "---- FILE NOT EXISTS ----\n";
$path = 'file/does/not/exists.jpg';
echo "File size is: " . filesize($path) . "\n";

Would result in:

会导致:

---- NULL ---------------
File size is: 
---- FILE EXISTS --------
File size is: 78953
---- FILE NOT EXISTS ----

Warning: filesize(): stat failed for file/does/not/exists.jpg in /home/luca/Scrivania/test.php on line 13

Call Stack:
    0.0001     642120   1. {main}() /home/luca/Scrivania/test.php:0
    0.0002     642448   2. filesize() /home/luca/Scrivania/test.php:13

This means that your variable

这意味着你的变量

$_FILES['profile']['name'];

is NOT pointing to a valid file location on the SERVER and is instead one among:

不指向服务器上的有效文件位置,而是其中之一:

  1. The file path on the client PC (not on the server) and in that case for sure you have not access to it directly
  2. Something that is not a string
  3. BUT it is not null (otherwise you simple would have returned null(see first example)
  1. 客户端 PC 上的文件路径(不在服务器上),在这种情况下,您肯定无法直接访问它
  2. 不是字符串的东西
  3. 但它不为空(否则你很简单就会返回null(见第一个例子)

Please next time post valid PHP code.

请下次发布有效的 PHP 代码。

Luca

卢卡

UPDATE

更新

As Marc B suggested you have to use $_FILES['profile']['tmp_name'];

正如马克 B 建议你必须使用 $_FILES['profile']['tmp_name'];