Warning: filesize(): stat failed for img.jpg
Asked Answered
P

3

8

I am trying to get the file size of an image and I keep getting 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 this but it did not solve the problem. Any help is much appreciated!

Precedent answered 3/2, 2015 at 15:42 Comment(2)
Did you try echo filesize($path); ? If yes, what did it give and if no, than please try it out.Factoring
You did not post valid php code.Woodenhead
E
7

['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:

$path = $_FILES['profile']['tmp_name'];
                          ^^^^^^^^^^^^
Elayne answered 3/2, 2015 at 15:49 Comment(0)
S
4

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

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

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

also you can access size of file with

$_FILE['profile']['size']
Soniasonic answered 3/2, 2015 at 15:53 Comment(0)
A
2
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)

Please next time post valid PHP code.

Luca

UPDATE

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

Antonetta answered 3/2, 2015 at 16:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.