Without using PHP copy function, move file from one folder to another - php

How can we move the one file from one folder to another folder without copy function in PHP.
In php copy function used to copy the file from one folder to another folder. But in my case I don't use copy function for move the file from folder to another folder.

You can try and make use of php rename function. It will move the file if a new directory was specified in the new move to filename. Just ensure that the permission are correct when moving and creating new folders and files.
rename("path1/file.txt", "path2/file.txt");
PHP RENAME
Attempts to rename oldname to newname, moving it between directories
if necessary. If newname exists, it will be overwritten.

If you do not want use PHP copy() function for some reason, you can do this like:
system('cp /path/to/src_file /path/to/dst_file');
for *nix systems, and
system('copy /path/to/src_file /path/to/dst_file');
for Windows

Suggest using rename().
http://php.net/manual/en/function.rename.php
rename('/dir1/old_dir/old_name.txt','/dir1/new_dir/zzz_name.bak');

Related

How to move a folder to another folder PHP? [duplicate]

I have two folders
myappdemo.com/VueGuides/services/iclean
myappdemo.com/VueGuides/services/pics
I need to move iclean folder into pics folder using PHP.
Use rename(). Note that if this runs on a web server, the web server user must have access to write to the target directory.
rename("oldpath", "newpath");
// in your case, assuming the script calling rename()
// resides in the directory above 'myappdemo.com'
rename("myappdemo.com/VueGuides/services/iclean", "myappdemo.com/VueGuides/services/pics/iclean");
// Or use full absolute paths
rename("/path/myappdemo.com/VueGuides/services/iclean", "/path/myappdemo.com/VueGuides/services/pics/iclean");
There's a specific PHP function for it
http://php.net/manual/en/function.rename.php
If you are worried about SEO i recommend you using redirects 301 in your .htaccess.
That must be something like that:
RewriteRule ^/VueGuides/services/iclean http://myappdemo.com/VueGuides/services/pics [NS,R=301,L]
I required a different solution in my case as I was moving a sub folders contents into the parent folder. rename wouldn't work in my instance because the path was the same.
(For Linux based machines):
exec('mv '.$this->useFolder.'/'.$sub_folder.'/*'.' '.$this->useFolder);
This uses the inbuilt mv function through exec.

How to delete a file which is made by using file creation operations in php in a particular path?

I have created code to create file automatically in a particular path folder. I wanted to delete that file using php code. I have used the function
unlink("../game_page/$fname.php");
here the filename is stored in the variable $fname and it is a php file. But this code is not working. Can anyone say how to delete that particular file in that path?
Try this:
$_SERVER['DOCUMENT_ROOT'] after you folder path.
unlink($_SERVER['DOCUMENT_ROOT']."/game_page/".$fname.".php");
Use
unlink("../game_page/".$fname.".php");
Also check the permission if that user has to delete that file and set by using chmod

Move file from temporary folder to uploads folder

In my WordPress the visitors can upload files like texts and this files go in this path
C:\xampp\htdocs\www\wordpress\wp-content\uploads\temporary, After validating I need to insert this files in this path C:\xampp\htdocs\www\wordpress\wp-content\uploads\2014\10 like attachment , Should I using wp_insert_attachment or wp_handle_upload() ?
I don't know if this is related to wordpress because I do not have a slightest idea about it. But in php, you use the copy() function to copy a file from one folder to another. And if you want to remove the file from the temporary folder, you are looking for the unlink() function for deleting a file

How to move one directory to another directory?

I have two folders
myappdemo.com/VueGuides/services/iclean
myappdemo.com/VueGuides/services/pics
I need to move iclean folder into pics folder using PHP.
Use rename(). Note that if this runs on a web server, the web server user must have access to write to the target directory.
rename("oldpath", "newpath");
// in your case, assuming the script calling rename()
// resides in the directory above 'myappdemo.com'
rename("myappdemo.com/VueGuides/services/iclean", "myappdemo.com/VueGuides/services/pics/iclean");
// Or use full absolute paths
rename("/path/myappdemo.com/VueGuides/services/iclean", "/path/myappdemo.com/VueGuides/services/pics/iclean");
There's a specific PHP function for it
http://php.net/manual/en/function.rename.php
If you are worried about SEO i recommend you using redirects 301 in your .htaccess.
That must be something like that:
RewriteRule ^/VueGuides/services/iclean http://myappdemo.com/VueGuides/services/pics [NS,R=301,L]
I required a different solution in my case as I was moving a sub folders contents into the parent folder. rename wouldn't work in my instance because the path was the same.
(For Linux based machines):
exec('mv '.$this->useFolder.'/'.$sub_folder.'/*'.' '.$this->useFolder);
This uses the inbuilt mv function through exec.

How do I copy files repository to new folder with PHP

I have a folder named "repository" in my admin folders. This folder holds 2 files: index.html and content.php. When a user creates a new page the php creates a new folder specified by the user then will need to copy the two files into that folder while leaving them in the repository.
copy(file,dest) does not work.
rename(file,dest) moves the files to the new folder but I lose them in the repository.
How do I copy the files in one folder to the new folder without losing the files in the original folder?
$dest = '../'.$menuLocation.'/'.$pageName;
$file1= "repository/index.html";
$file2= "repository/content.php";
mkdir($dest,0777);
rename($file1,$dest.'/index.html');
rename($file2,$dest.'/content.php');
$menuLocation and $pageName are supplied by the user. The files are there, file_exists returns back true. Also, the directory is created with no issues. rename() also works I just lose the files in repository.
For anyone hunting for the solution to this:
when using copy() in php you also need to include the file name.
copy(orginalfile,destination_with_filename);
for example:
wrong:
copy('/temp/sports/basketball.jpg','/images/sports/')
Correct:
copy('/temp/sports/basketball.jpg','/images/sports/basketball.jpg')
Use copy(). Make sure you capture the return value so that your program knows if it worked or not. Also check permission of the files and directory to confirm that the user executing the PHP script is able to create the new file in the place you specified. You might want use is_writable() on the directory to confirm these assumptions.

Categories