Symfony UploadedFile not getting uploaded

1.1k views Asked by At

I am trying to create a UploadedFile from an existing file.But it is not getting uploaded successfully,neither it shows any error.

Here is my code

   $encoded_data = "This is a huge string";
   $filename = "tempFile";
   $handle = fopen($_SERVER['DOCUMENT_ROOT'].$filename, "a+")
   file_put_contents($_SERVER['DOCUMENT_ROOT'].$filename, $encoded_data);
   $file = new UploadedFile($_SERVER['DOCUMENT_ROOT'].$filename, $filename, null, filesize($_SERVER['DOCUMENT_ROOT'].$filename));
   var_dump($file->getClientSize());
   var_dump($file->getError());
   var_dump($file->isValid());
   var_dump(is_uploaded_file($file));

The result is

int 21
int 0
boolean false
boolean false

I am sure the tempFile exists at the document root!

1

There are 1 answers

0
Carlos Granados On

is_uploaded_file() will check that the file has actually been uploaded, so it will not work with your code, since the file has not been uploaded. That is why isValid() also returns false, as it is using is_file_uploaded() to check the file. Once this said, UploadedFile does have a $test parameter which if set to true, will make isValid() not check if the file is actually uploaded. This is used for testing, where no files are actually uploaded. You could set this parameter to true and then isValid() would return true for your file. It is a hack, but maybe is what you need