views:

112

answers:

3

I need a little help here:

I get a file from an HTML upload form. And I have a "target" filename in $File.

When I do this:

copy($_FILES['binfile']['tmp_name'], $File);
echo '<hr>' . filesize($_FILES['binfile']['tmp_name']);
echo '<hr>' . filesize($File);

Everything works fine. I get the same number twice.

However when I delete the first call of filesize(), I get "0" (zero).

copy($_FILES['binfile']['tmp_name'], $File);
echo '<hr>' . filesize($File);

Any suggestions? What am I doing wrong? Why do I need to get the filesize of the "original" file before I can get the size of the copy?

(That's actually what it is: I need to call the filesize() for the original file. Neither sleep() nor calling filesize() of another file helps.)

System:

  • Apache 2.0
  • PHP 5.2.6
  • Debian Linux (Lenny)
+2  A: 

How big is this file? You are doing a copy and then stating the file straight away. Could this be the problem?

Does the builtin move_uploaded_file() function give the same behavior?

zaf
It's just 300kb. It doesn't seem to be a timing problem, because "sleep" doesn't help.
BlaM
And copying a file should block until it's done anyways.
Matti Virkkunen
That is strange. It would interesting to know whats going on here.
zaf
Accepted this answer, because "move_uploaded_file" solves my problem.
BlaM
A: 

Try this:

copy($_FILES['binfile']['tmp_name'], $File);
clearstatcache();
$filesize = $_FILES['binfile']['size'];
echo '<hr>' . $filesize;
SoLoGHoST
Just believe me that it has a value - and it has another value than tmp_name, because obviously I don't want to copy the file to itself.
BlaM
Because this code is just what I condensed my problem to. I use a library that needs the filename to have an ".jpg" extension.
BlaM
Ok, edited my answer.
SoLoGHoST
clearstatcache() seems to help. I wouldn't be able to use $_FILES...['size'] though, because - as I said - there is another library and that library uses the filesize somewhere deep inside.
BlaM
Well, you could create a function that could sleep until the file_exist returns true. If the `file_exists($File)` function returns true, than do `filesize($File);`
SoLoGHoST
A: 

How about this:

copy($_FILES['binfile']['tmp_name'], $File);

clearstatcache();
while (empty(filesize($File)))
    sleep(2);

echo '<hr>' . filesize($File);

OR try this:

copy($_FILES['binfile']['tmp_name'], $File);

clearstatcache();
while (!file_exists($File))
    sleep(2);

echo '<hr>' . filesize($File);
SoLoGHoST