How i can copy one file from one directory to another directory using php
You can use
copy — Copies file
Example from Manual:
$file = 'example.txt';
$newfile = 'example.txt.bak';
if (!copy($file, $newfile)) {
echo "failed to copy $file...\n";
}
Use rename().
rename ("/var/www/files/file.txt", "/var/www/sites/file.txt");
rename('path/to/file/file.ext', 'path2/to2/file2/file.ext');
If you are moving a file just uploaded, use http://php.net/manual/en/function.move-uploaded-file.php instead of rename(). It gives you some added security.
Related
I have a problem when I want to copy a file from one folder to another on my website, I don't know what I'm doing wrong,the file is called "installed" and dont have extension, is only archive,I need to copy this file from the "ins" folder to the "var" folder this is the php code I'm using:
<?php
$file = '/../domains/travianforce.com.es/public_html/ins/installed';
$new_file = '/../domains/travianforce.com.es/public_html/var/';
if (!copy($file, $new_file)) {
echo "Error to copy $file...\n";
}
?>
but when I run the script it gives me an error, I don't know what I'm doing wrong but I need your help, thank you very much! =)
Give full path in $file and $new_file
And ans already given in this
I wanto to copy a pdf file to another folder, and it works, but the file that I open in the destination folder is decoded incorrectly and I can not open.
My code:
$fsrc = fopen($srcz,'r');
$fdest = fopen($destz,'w+');
copy($fsrc,$fdest)
Thanks
Try this:
copy($srcz,$destz);
The copy function in PHP needs the source and destination. Consult the php manual: Php copy
I don't know how that code you have works... see the function copy takes the names of the files:
copy($srz,$destz);
If you want to copy the files opened with fopen you use stream_copy_to_stream, like so:
$fsrc = fopen($srcz,'r');
$fdest = fopen($destz,'w+');
stream_copy_to_stream($fsrc, $fdest);
fclose($fsrc);
fclose($fdest);
Do not forget to close the files!
you should use copy without using fopen because fopen create a resource and copy too .
$old = '/tmp/yesterday.txt';
$new = '/tmp/today.txt';
copy($old, $new) or die("Unable to copy $old to $new.");
This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
Clone + Rename file with PHP
This should be pretty easy. I wan't to copy & rename images that already exist on the server while still retaining the original image.
Here's the original image location:
images/
folder/
one.jpg
This is what I want:
images/
folder/
one.jpg
one_thumb.jpg
How can I achieve this? You can see I'm not just simply renaming an existing file / image. I want to copy it and rename it to the same directory.
Just use the copy method: http://php.net/manual/en/function.copy.php
Ex:
<?php
$file = 'images/folder/one.jpg';
$newfile = 'Images/folder/one_thumb.jpg';
if (!copy($file, $newfile)) {
echo "failed to copy";
}
PHP has a function, copy built-in that can do this. Here's an example:
<?php
$file = 'one.jpg';
$newfile = 'one_thumb.jpg';
if (!copy($file, $newfile)) {
echo "failed to copy $file...\n";
}
?>
The function returns a boolean indicating whether the copy was successful. It's as simple as that!
I have a file called filecontent.txt which includes some php code.
And i have another file called index.php,
I want to replace the index.php content with the filecontent.txt content.
Is there any solution for it??
Please help me on this.
You can use php copy function
eg:
<?php
$file = 'example.txt';
$newfile = 'example.txt.bak';
if (!copy($file, $newfile)) {
echo "failed to copy $file...\n";
}
?>
PHP COPY
You may also delete the index.php and rename the filecontent.txt to index.php afterwards, if its just a replacement.
unlink ("index.php");
rename ("filecontent.txt", "index.php");
how to map the path to the file easily?
public_html/api/function.php
<?php
function writetologfile($content)
{
$filename = 'logfile/testing_randomstring.txt';
if (!$handle = fopen($filename, 'a'))
{
echo "Cannot open file ($filename)";
exit;
}
fclose($handle);
}
?>
the actual path of the text file is in public_html/r/admin/logfile/testing_randomstring.txt
so if I run the script at public_html/folder1/folder2/addlog.php, it won't be able to find the path to the testing_randomstring.txt
addlog.php
<?php
include("../../api/function.php");
writetologfile('hahaha');
?>
How I can able to easily point to this text file path, no matter where my php calling script is from.
I tried to change $filename = 'logfile/testing_randomstring.txt'; inside writetologfile function by enforcing it to absolute fix path,
something like $filename='/r/admin/logfile/testing_randomstring.txt',
but it is not working
Instead of using a relative path, you could specify an absolute path. Assuming public_html is in your home directory, try this:
$filename = '/public_html/r/admin/logfile/testing_randomstring.txt';
fopen(getenv('HOME') . $filename, 'a');
This uses getenv to read the contents of the environment variable $HOME.