0

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)
4
  • 4
    Don’t use copy; use move_uploaded_file instead. Commented Apr 9, 2010 at 17:09
  • @Gumbo: Cool. Didn't know that function existed. Seems to solve my problem, too. Commented Apr 9, 2010 at 17:12
  • Have you checked for similar behavior on another setup? Commented Apr 9, 2010 at 17:17
  • @zaf I tried to do copy() and a following filesize() with files that already exist in the system. No problem there. Didn't try on another server, however. Commented Apr 9, 2010 at 17:19

3 Answers 3

2

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?

Sign up to request clarification or add additional context in comments.

3 Comments

It's just 300kb. It doesn't seem to be a timing problem, because "sleep" doesn't help.
That is strange. It would interesting to know whats going on here.
Accepted this answer, because "move_uploaded_file" solves my problem.
0

Try this:

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

4 Comments

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.
Because this code is just what I condensed my problem to. I use a library that needs the filename to have an ".jpg" extension.
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.
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);
0

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.