How to replace a webfile content with a file content? - php

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");

Related

Copy a file from one folder to another in php

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

Copy & rename a file to the same directory without deleting the original file [duplicate]

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!

copy a file from one dir to another dir

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.

how i can open a file in zip file without extract it by the PHP

i just want making a system that can open a file in a zip archive
like i have archive.zip
in this file.text
i want make a class to makr this page
zip.php?archiver=archive.zip&file=file.text
i want this page show me the file.text contents
any ideas?
From the PHP manual: ZipArchive::getFromName(). This is exactly what you need.
<?php
$z = new ZipArchive();
if ($z->open(dirname(__FILE__) . '/test_im.zip')) {
$string = $z->getFromName("mytext.txt");
echo $string;
}
?>
Best wishes,
Fabian
you could probably do this:
http://www.php.net/manual/en/function.ziparchive-getfromname.php
it's a function in http://php.net/manual/en/book.zip.php

How to map file path from different folder location easily?

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.

Categories