简体   繁体   中英

move_uploaded_file not moving file to folder

I have a form from which I can upload some files. The file array shows up correct when I print it but there is no file in the destination folder. What am I missing?

This is my code:

// Fileupload
if (!is_dir('../incidentbijlagen/'.$inc['incident']['companyname'].'')) {
    mkdir('../incidentbijlagen/'.$inc['incident']['companyname'].'', 0777, true);
}

$uploads_dir = '../incidentbijlagen/'.$inc['incident']['companyname'].'/';

foreach($bijlage['incident'] as $key => $file){
  $tmp_name = $file['incident']['tmp_name']['bijlage'][$key];
  $name = basename($file['incident']['name']['bijlage'][$key]);
  move_uploaded_file($tmp_name, "$uploads_dir/$name");
}

There are no errors in my error_log so I am not sure why the file is not moved. This is what my array looks like when I print it:

Array
(
    [incident] => Array
        (
            [name] => Array
                (
                    [bijlage] => Array
                        (
                            [0] => fblogosnm.jpg
                        )

                )

            [type] => Array
                (
                    [bijlage] => Array
                        (
                            [0] => image/jpeg
                        )

                )

            [tmp_name] => Array
                (
                    [bijlage] => Array
                        (
                            [0] => /tmp/phpnh5aHW
                        )

                )

            [error] => Array
                (
                    [bijlage] => Array
                        (
                            [0] => 0
                        )

                )

            [size] => Array
                (
                    [bijlage] => Array
                        (
                            [0] => 20573
                        )

                )

        )

)

Get rid of the loop and just try using 0 like this:

  $tmp_name = $file['incident']['tmp_name']['bijlage'][0];
  $name = basename($file['incident']['name']['bijlage'][0]);
  move_uploaded_file($tmp_name, "$uploads_dir/$name");

UPDATE

OK for multiple, try this:

foreach($bijlage['incident']['name']['bijlage'] as $key => $file) {
  $tmp_name = $file['incident']['tmp_name']['bijlage'][$key];
  move_uploaded_file($tmp_name, "$uploads_dir/$file");
}

Try to replace: $uploads_dir = '../incidentbijlagen/'.$inc['incident']['companyname'].'/';

with: $uploads_dir = realpath('../incidentbijlagen/'.$inc['incident']['companyname'].'/');

This returns the full path (eg: /var/www/yourwebsite/incidentbijlagen/ etc)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM