How do i move a file to another directory in php?

I need to allow users on my website to delete their images off the server after they have uploaded them if they no longer want them. I was previously using the unlink function in PHP but have since been told that this can be quite risky and a security issue. (Previous code below:)

if(unlink($path.'image1.jpg')){ 
     // deleted
}

Instead i now want to simply move the file into a different folder. This must be able to be done a long time after they have first uploaded the file so any time they log into their account. If i have the main folder which stores the users image(s):

user/

and then within that a folder called del which is the destination to put their unwanted images:

user/del/

Is there a command to move a file into a different folder? So that say:

user/image1.jpg

moves to/becomes

user/del/image1.jpg

asked Oct 2, 2013 at 14:28

The rename function does this

docs rename

rename('image1.jpg', 'del/image1.jpg');

If you want to keep the existing file on the same place you should use copy

docs copy

copy('image1.jpg', 'del/image1.jpg');

If you want to move an uploaded file use the move_uploaded_file, although this is almost the same as rename this function also checks that the given file is a file that was uploaded via the POST, this prevents for example that a local file is moved

docs move_uploaded_file

$uploads_dir = '/uploads';
foreach ($_FILES["pictures"]["error"] as $key => $error) {
    if ($error == UPLOAD_ERR_OK) {
        $tmp_name = $_FILES["pictures"]["tmp_name"][$key];
        $name = $_FILES["pictures"]["name"][$key];
        move_uploaded_file($tmp_name, "$uploads_dir/$name");
    }
}

code snipet from docs

answered Oct 2, 2013 at 14:33

MKroedersMKroeders

7,3443 gold badges23 silver badges38 bronze badges

2

Use the rename() function.

rename("user/image1.jpg", "user/del/image1.jpg");

answered Oct 2, 2013 at 14:32

How do i move a file to another directory in php?

Ben FortuneBen Fortune

30.7k10 gold badges79 silver badges78 bronze badges

0

If you want to move the file in new path with keep original file name. use this:

$source_file = 'foo/image.jpg';
$destination_path = 'bar/';
rename($source_file, $destination_path . pathinfo($source_file, PATHINFO_BASENAME));

answered Jan 24, 2018 at 16:38

Nabi K.A.Z.Nabi K.A.Z.

8,7966 gold badges53 silver badges71 bronze badges

2

I using shell read all data file then assign to array. Then i move file in top position.

i=0 
for file in /home/*.gz; do
    $file
    arr[i]=$file
    i=$((i+1)) 
done 
mv -f "${arr[0]}" /var/www/html/

answered Mar 22, 2019 at 9:52

1

Some solution is first to copy() the file (as mentioned above) and when the destination file exists - unlink() file from previous localization. Additionally you can validate the MD5 checksum before unlinking to be sure

answered Nov 6, 2018 at 13:25

quardasquardas

6013 gold badges10 silver badges22 bronze badges

Create a function to move it:

function move_file($file, $to){
    $path_parts = pathinfo($file);
    $newplace   = "$to/{$path_parts['basename']}";
    if(rename($file, $newplace))
        return $newplace;
    return null;
}

answered Mar 19, 2019 at 16:49

MarceloMarcelo

731 silver badge6 bronze badges

use copy() and unlink() function

$moveFile="path/filename";
if (copy($csvFile,$moveFile)) 
{
  unlink($csvFile);
}

answered Mar 20, 2019 at 10:00

Use file this code

function move_file($path,$to){
   if(copy($path, $to)){
      unlink($path);
      return true;
   } else {
     return false;
   }
 }

answered Jan 17, 2021 at 18:03

How do i move a file to another directory in php?

function xmove($src,$dst)
{

  if(is_dir($src)===true)
  { $dir=opendir($src);
    while (($file = readdir($dir)) !== false) {
        if ($file === '.' || $file === '..') {
            continue;
        }

        if (is_dir($src."/".$file) === true) {
            if (is_dir("$dst/$file") === false) {
                mkdir("$dst/$file");
            }
            //echo basename("$file")."<br>";
                xmove($src."/".$file,$dst."/".$file);
            
        }
        else {
            copy("$src/$file", "$dst/$file");
            unlink("$src/$file");
            
        }
    }

    closedir($dir);
    rmdir($src);
    return true;

  }else
  return false;

}

answered Nov 1, 2021 at 17:29

1

shell_exec('mv filename dest_filename');

answered Sep 21, 2018 at 6:28

1

How do I move a file from one directory to another in PHP?

If you need to copy file from one folder to another using php code then you can use “copy()” function of php. php provide copy function to move your file from one place to another.

How do I move a folder to another directory in PHP?

Linked.
Move folders and files to a folder..
Using scandir() to find folders in a directory (PHP).
Copy all files and folder from one directory to another directory PHP..

How do I move a file to another folder?

Cut and paste files to move them Select the file you want to move by clicking on it once. Right-click and pick Cut, or press Ctrl + X . Navigate to another folder, where you want to move the file. Click the menu button in the toolbar and pick Paste to finish moving the file, or press Ctrl + V .

How do I move a file from one directory to another command line?

To move a file or directory from one location to another, use the command mv. Common useful options for mv include: -i (interactive) — Prompts you if the file you have selected overwrites an existing file in the destination directory. -f (force) — Overrides the interactive mode and moves without prompting.