0

I am trying to save files from a file upload which has more than one file using a foreach loop. But the move_uploaded_file is only executing once. I tried printing $file['tmp_name'], but it's printing two array values in one single line as text. How can fix this .. Thanks ..

  public function uploadFile($filename)
    {

        $file_ary = $this->reArrayFiles($_FILES[$filename]);

        foreach ($file_ary as $file) {

           move_uploaded_file($file['tmp_name'] , '../uploads/' . '.txt');

        }




    }


   function reArrayFiles(&$file_post) {
        $file_ary = array();
        if(!is_array($file_post['name']))
            return array($file_post);
        $file_count = count($file_post['name']);
        $file_keys = array_keys($file_post);
        for ($i=0; $i<$file_count; $i++) {
            foreach ($file_keys as $key) {
                $file_ary[$i][$key] = $file_post[$key][$i];
            }
        }
        return $file_ary;
    }


 $file_ary :- Array ( [0] => Array ( [name] => customer care.txt [type] =>     text/plain [tmp_name] => C:\Program Files (x86)\EasyPHP-DevServer- 14.1VC11\binaries\tmp\php7B21.tmp [error] => 0 [size] => 11 ) [1] => Array ( [name] => ss.txt [type] => text/plain [tmp_name] => C:\Program Files (x86)\EasyPHP-DevServer-14.1VC11\binaries\tmp\php7B32.tmp [error] => 0 [size] => 0 ) [2] => Array ( [name] => [type] => [tmp_name] => [error] => 4 [size] => 0 ) )

$file['tmp_name']  : C:\Program Files (x86)\EasyPHP-DevServer-     14.1VC11\binaries\tmp\phpA38A.tmpC:\Program Files (x86)\EasyPHP-DevServer- 14.1VC11\binaries\tmp\phpA38B.tmp

enter image description here

8
  • Paste your $file_ary here Commented Jun 21, 2015 at 13:19
  • C:\Program Files (x86)\EasyPHP-DevServer-14.1VC11\binaries\tmp\phpA38A.tmpC:\Program Files (x86)\EasyPHP-DevServer-14.1VC11\binaries\tmp\phpA38B.tmp ... literally i am expecting an array , but it's showing plain text with no separator.. Commented Jun 21, 2015 at 13:22
  • Looks like string.Is this an array? Commented Jun 21, 2015 at 13:23
  • Consider doing the following in order to debug it: 1) echo $file['error']; in order to check if an error occurred during the upload. 2) $res = move_uplo..; var_dump($res) to check if an error occurred during the upload. Share with us the output of them both. Commented Jun 21, 2015 at 13:27
  • see my updated post :) The one i posted in the comment is the output i got from inside the foreach .. I am using the foreach for saving the multiple files to the destination folder .. Thanks Commented Jun 21, 2015 at 13:28

1 Answer 1

1

This function checks to ensure that the file designated by filename is a valid upload file (meaning that it was uploaded via PHP's HTTP POST upload mechanism). If the file is valid, it will be moved to the filename given by destination.

Taken from the docs.

Basically, after you move it, the file will no longer exist. Note, that move differs from copy in the fact that it removes the source file. You need to create a file resource, copy the needed content there and then save it wherever and whenever you want.

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

2 Comments

I can see only one file in the saved directory. I am expecting more than one file . The move_uploaded function is only executing once .. Thanks though
OK, let me put it this way: you want to save file1 and file2. When you call move_uploaded_file for file1, then file1 is successfully created, but the source file is DELETED. When you call it for file2, the source file no longer exists, hence you can no longer move it. This is why I suggested that you should create an image resource and save it instead of your current (wrong) strategy.

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.