4

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!

user3425344
  • 3,357
  • 4
  • 20
  • 31
  • can you also paste the content of your html file? –  Oct 17 '15 at 16:36
  • @NishchalGautam There is no html file.Please see the updated code. – user3425344 Oct 17 '15 at 16:46
  • How are you uploading a file if there is no html at all? how are you submitting a file to the server in the first place? If you're trying to write into a file, you could just use ```file_put_contents('filename','data');``` –  Oct 17 '15 at 16:49
  • But I want an UploadedFile object of the file. So I am trying to create an UploadableFile object manually. – user3425344 Oct 17 '15 at 16:52

1 Answers1

1

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

Carlos Granados
  • 11,273
  • 1
  • 38
  • 44