user1370632
user1370632

Reputation:

move_uploaded_file file not work when it will call two time

i have a function to upload file in php Like:

function upload_file($folder,$name)
{
$dest = "../product/". $folder."/". $name;
$src       = $_FILES['image']['tmp_name'];
if (move_uploaded_file($src, $dest)) {
    } else {
        echo "<script> alert('" . $src . "');
            window.history.go(-1); </script>\n";
        exit();
    }
}

whene i call function only one time like upload_file('small','abc.jpg') it works fine but i have call two time like:

upload_file('small','abc.jpg')
upload_file('big','abc.jpg')

it not work on second folder 'big'

any solution for that ?

Upvotes: 2

Views: 951

Answers (1)

bitoshi.n
bitoshi.n

Reputation: 2318

move_uploaded_file has moved to the first destination. So, the solution is get the first destination path, and copy with it for the other.

It will be a lot of solutions. One of them, change the param into array and iterate it.

<?php
$filesDes = array(
           array('folder'=>'small','name'=>'abc.jpg'),
           array('folder'=>'big','name'=>'abc.jpg'));

function upload_file($arrayfile)
{
    $firstfile = null;
    foreach($arrayfile as $val)
    {
        $dest = "../product/". $val['folder']."/". $val['name'];
        if(!isset($firstfile))
        {
            $firstfile = $dest;
            $src       = $_FILES['image']['tmp_name'];
            if (!move_uploaded_file($src, $dest)) {
               echo "<script> alert('" . $src . "');
               window.history.go(-1); </script>\n";
               exit();
            }
        }
    }
    else
    {
        if(!copy ( $firstfile , $dest ))
        {
           echo "<script> alert('" . $dest . "');
           window.history.go(-1); </script>\n";
           exit();
        }
    }
}
upload_file($filesDes)

Upvotes: 1

Related Questions