PHP - Move a file into a different folder on the server
Use the rename() function.
rename("user/image1.jpg", "user/del/image1.jpg");
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