How can i edit a txt file using php and ftp - php

I want to edit (write some data) to a txt file located on an ftp server using php.
I looked to this post writing to a file using php ftp
but in this case CHMOD 0777 is applied which is not understood by my server.

As simple as that!
<?php
$handle = fopen("ftp://user:password#example.com/somefile.txt", "w");
?>
It worked!

Related

unable to open file with php on ubuntu machine

I trying to create new file in Ubuntu system using PHP script,
but when I run this script the error
Unable to Open file
is appear
although I sure that the file's path is right and I have permissions to access this file I don't know where is the wrong
this is my code
$myfile = fopen('inc/users/future.php', "w")or die("Unable to open file!") ;
$text='<?
$host ="'.$host.'";
$user ="'.$db_admin.'" ;
$pass ="'.$db_password.'";
$db ="'.$database.'" ;
$myconn;?>';fwrite($myfile, $text);
fclose($myfile);
the path of this script is
/var/www/html/ghost/index.php
and the path of the file which I wish to open is
/var/www/html/ghost/inc/users/future.php
in other hand when I run this script in windows machine every thing is go fine
In your script use
fopen(dirname(__FILE__) . '/inc/users/future.php', 'w')
This will create a filepath from the directory your index.php. If you script is called from another file, php might search coming from that file.
Also check if the php process has sufficient file permissions to read and open the file. Try setting the file to chmod 777 just to test if that is the case (do not leave it on 777 though).
Keep in mind that if the file is a symbolic link, the 'w' parameter of fopen will not work.

Why is my PHP copy() function creating a blank file?

I have a copy function per say:
<?php
copy ( "$source" , "$destination" );
?>
The code does create a copy succssfully however the $destination (or copied file) is left blank. if i open it it is empty unlike the $source file.
The file size is 0b.
My situation:
nginx/1.10.0 (Ubuntu)
PHP 7.0.13-0
ubuntu0.16.04.1
The file permissions for the code processing file is 0777
The permission for the source file is 0777
The file permission for the destination file after being copyed (the blank one ) : 0644.
Obviously no control over the permission before copying.
Anyone know the reason for this and how to make the php copy() function copy the file with the original file contents?
Best regards,
AT
Ok so i seemed to solve this apparently my server ran out of disk space.
Makes sense. should have known.

fwrite function not able to find directory other than local server

I am using codeigniter with php. I am trying to write image in my file structure(Ubuntu file structure). This image is coming from url. My code to write image is
$content = file_get_contents($url);
$filename = "myImage.jpg";
$filepath = "/opt/lampp/htdocs/affiliatedSystem/assets/file/".$filename;
//Store in the filesystem.
$fp = fopen($filepath , "w");
fwrite($fp, $content);
fclose($fp);
This code works perfectly fine on localhost but when I uploaded project on server its giving error like this
Message: fopen(/opt/lampp/htdocs/affiliatedSystem/assets/file/myImage.jpg):
failed to open stream: No such file or directory I also gave file
permission to folder 777 on server.
So, How can I write image on server?
(i can't add a comment, so i write in the answer. But is it the solution?).
I had the same problem on Fedora Linux. The owner of the folder was me and no apache. Try to change the owner folder with your user apache (if you use apache).
First, active httpd_unified :
setsebool -P httpd_unified on
Second, give right to user apache :
chown yourApacheUser:yourApacheGroup /var/www/..../yourDirectory
Finally, I get the answer, I am using $_SERVER['DOCUMENT_ROOT'] function at the starting of url like this$_SERVER['DOCUMENT_ROOT']."/affiliatedsystem/assets/file/".$filename;
and it gives me right url of my server and I am able to write file in my directory on server.

PHP Unzip very large file

I have a zip file on the server. It's 1.1gb made up of thousands of small files. I do not have shell or root access to the server and can only use ftp and create php files.. so far I have tried exec and shell exec but none worked. The server is running free bsd. How can I unzip the file into the directory it is in?
For a pure PHP solution, try PclZip - this would not require you to install any PHP extensions or require shell access - you just need to write access to wherever you want to extract the files.
$filename = '/media/file.gz';
$unzipped_content = '';
$zd = gzopen($filename, "r");
while ($zip_file = gzread($zd, 10000000)){
$unzipped_content.= $zip_file;
}
gzclose($zd);
echo $unzipped_content;
Thanks for the suggestions everyone. I ended up modifying the code in this question to unzip the files.
Unzip a file with php

fwrite Across Domain?

Is the following code below possible? Or no because its a security issue? I'd like to fwrite across from one folder to another so if the fwrite function is located at /1, can I fwrite to a .txt file on /2?
<?php
$myFile = "../news/derp.txt";
$fh = fopen('derp.txt', 'w') or die("File Can't Be Written To.");
$stringData = "Yo, .";
fwrite($fh, $stringData);
fclose($fh);
?>
As long as the script is being executed in a context where it has write permissions to the file, it should work.
The security issue question requires more information about what you are doing with it and whether or not the second directory is publicly visible.
A remote file would most likely not be writeable by PHP, but if it's a local folder, it just depends on whether PHP has write access to that folder + file.
Read this:
"If PHP has decided that filename specifies a local file, then it will try to open a stream on that file. The file must be accessible to PHP, so you need to ensure that the file access permissions allow this access. If you have enabled safe mode, or open_basedir further restrictions may apply."
http://php.net/manual/en/function.fopen.php
PHP can write to anywhere on your filesystem. PHP is not limited to the domain of the server running it, which is why LFIs are so efficient.
PHP can write to any directory it has write access to. If you don't have access to it, like if it's someone else's account on a shared server, then no you probably won't be able to do that.
The answer to my question is yes. I was just unaware of the circumstances, and the correct way to ascend to the file. Here is the final working script.
Writing from /1/fwrite.php, here's the script.
<?php
$file = fopen("../2/derp.txt","w");
fwrite($file,"Hello World!");
fclose($file);
?>

Categories