Skip to content

Instantly share code, notes, and snippets.

@code-boxx
Last active May 26, 2023 03:39
Show Gist options
  • Save code-boxx/5e6e1801b648f042b07f6affbd3e6b21 to your computer and use it in GitHub Desktop.
Save code-boxx/5e6e1801b648f042b07f6affbd3e6b21 to your computer and use it in GitHub Desktop.
PHP Move Files

WAYS TO MOVE FILES IN PHP

https://code-boxx.com/move-files-php/

LICENSE

Copyright by Code Boxx

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

<?php
// NOTE: IF THERE IS AN EXISTING NEW.TXT, IT WILL BE OVERRIDEN.
echo rename("old.txt", "new.txt") ? "OK" : "ERROR";
<?php
$source = "old.txt";
$destination = "d:/temp/new.txt";
echo rename($source, $destination) ? "OK" : "ERROR" ;
<?php
// (A) MOVE WITHOUT OVERRIDE
function safemove ($src, $dest) {
if (file_exists($dest)) { return false; }
return rename($src, $dest);
}
// (B) GO!
echo safemove("old.txt", "new.txt") ? "OK" : "ERROR" ;
<?php
function movetype ($ext, $src, $dest) {
// (A) CREATE DESTINATION FOLDER
if (!file_exists($dest)) {
mkdir($dest);
echo "$dest created\r\n";
}
// (B) GET ALL FILES
$files = glob($src."*.{".$ext."}", GLOB_BRACE);
// (C) MOVE
if (count($files)>0) { foreach ($files as $f) {
$moveTo = $dest . basename($f);
echo rename($f, $moveTo)
? "$f moved to $moveTo\r\n"
: "Error moving $f to $moveTo\r\n";
}}
}
movetype("jpg,png,gif", "d:/from/", "d:/to/");
<?php
// (A) CURL INIT + OPTIONS
$file = "test.txt"; // File to move
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, "http://localhost/4b-recv.php");
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, [
"up" => curl_file_create($file), // NOTE: PHP 5.5+ ONLY
"key" => "value" // OPTIONAL EXTRA FIELDS
]);
// (B) SEND FILE
$results = curl_exec($curl);
curl_close($curl);
// (C) DELETE LOCAL FILE AFTER SUCCESSFUL SEND
if ($results == "OK") {
unlink($file);
print_r($results);
}
<?php
// (A) YOU MIGHT WANT TO SECURE THIS SCRIPT BY CHECKING THE REQUESTOR
/*
if ($_SERVER['REMOTE_ADDR'] != "SERVER IP ADDRESS") {
http_response_code(403); // forbidden
exit("NO PERMISSION TO ACCESS");
}
*/
// (B) WORKS THE SAME AS REGULAR FILE UPLOAD
// move_uploaded_file(SOURCE, DESTINATION)
echo move_uploaded_file($_FILES["up"]["tmp_name"], $_FILES["up"]["name"])
? "OK" : "ERROR" ;
// (C) FOR DEBUGGING
// print_r($_SERVER);
// print_r($_POST);
// print_r($_FILES);
<?php
// WINDOWS
echo exec("move source.txt destination.txt");
// LINUX
echo exec("mv source.txt destination.txt");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment