-2

Following is my piece of code which moves file to particular directory.

foreach($files as $dir_file)    //Getting Path of XML file
        {
            echo "\nDir Files: ".$dir_file."\n";

            if($dir_file != "." && $dir_file != "..")
            {
                if(strpos($dir_file,'.xml'))     
                {
                    echo "\nXML file found\n";
                    $xmlPath=$path."/".$dir_file;
                    // return ReadXML($xmlPath);
                }
                if(strpos($dir_file,'.JPG'))
                {
                    echo "\n FOund \n";
                    $ret=move_uploaded_file($dir_file, 'upload/'.$dir_file));
                     echo "\n retunr values: ".$ret;
                }  
            }

I have checked all permission and it is 777. but move upload file function is not moving my file to particular directory. it is also not returning anything. What i am doing wrong?

Muneem Habib
  • 1,046
  • 4
  • 18
  • 49
  • Can you show what $files look like? – Subedi Kishor Nov 01 '13 at 06:50
  • 2
    First, turn on error reporting: http://stackoverflow.com/questions/845021/how-to-get-useful-error-messages-in-php If you still can't identify the problem from the error message, then post the error message here. – Sam Dufel Nov 01 '13 at 06:51
  • 2
    Are these always files that have just been uploaded (i.e. in this same script)? If they're just files on the file system you might be wanting to `rename(...)` instead of `move_uploaded_file(...)` (http://www.php.net/manual/en/function.rename.php) – Clart Tent Nov 01 '13 at 06:51
  • they are just image files and i am running it through SSH terminal – Muneem Habib Nov 01 '13 at 06:53
  • Are tou trying to upload a file using xml filepath? – Krish R Nov 01 '13 at 06:55
  • sir i have files placed in a directory i want to move these files to other folders – Muneem Habib Nov 01 '13 at 06:56
  • If you are trying to move the files form one directory to another then you have a look at http://php.net/manual/en/function.copy.php and this might be helpful to you http://stackoverflow.com/questions/2082138/move-all-files-in-a-folder-to-another – Subedi Kishor Nov 01 '13 at 06:57
  • So these files have not *just been uploaded via an HTTP request?!* – deceze Nov 01 '13 at 06:59
  • In that case, you need use copy function and once it is copied after that u need to unlink the file. – Krish R Nov 01 '13 at 06:59
  • rename function cut the files and paste to desired location i dunt want it to cut the files i want to copy – Muneem Habib Nov 01 '13 at 07:08
  • Well then **use `copy`**. – deceze Nov 01 '13 at 07:17

2 Answers2

1

If you are to move files from one directory to another, you should use copy() function.

This is the example code:

$source = "YOUR_SOURCE_DIRECTORY/";
$destination = "YOUR_DESTINATION_DIRECTORY/";

foreach ($files as $file) {
    if (in_array($file, array(".","..")))
        continue;

    //If file is copied successfully then mark it for deletion
    if (copy($source.$file, $destination.$file)) {
        $delete[] = $source.$file;
    }
}
//If you want to delete the copied files then include these lines
//Delete all successfully copied files
foreach($delete as $file) {
    unlink($file);
}
Subedi Kishor
  • 5,906
  • 5
  • 35
  • 53
0

move_uploaded_file

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.

(Emphasis mine.) You can use move_uploaded_file only to move files which have been uploaded, where "uploaded" means "PHP has received the file in the current request and the file is currently in the $_FILES super global". Any other file is not movable by move_uploaded_file. This is exactly on purpose, move_uploaded_file is supposed to protect you from yourself and make sure you're only moving files uploaded by the user.

If you want to move any other file which has not just been uploaded via HTTP POST, use copy or rename.

Community
  • 1
  • 1
deceze
  • 510,633
  • 85
  • 743
  • 889