This code below is using the FWRITE function. however i wish to save the file to a specific location but always get 'cant open file' as expected. i have setup the directory listed in mypath but it still wont write to that location.
$mypath = "http://www.mysite.com/test/data/";
$myFile = $mypath."data.txt";
$fh = fopen($myFile, 'w') or die("can't open file");
$stringData = "Some text";
fwrite($fh, $stringData);
fclose($fh);
The HTTP wrapper does not support writing. If mysite.com is the same server you're running on, and that's a physical directory, you would use a regular file path (no HTTP). What filename that URL corresponds to depends entirely on how your server is set up.
If it's a different machine, you need to use curl or another solution to do a PUT or POST.
Try using a logical path to save the file such as:
/home/mysitefolder/public_html/test/data/
also check to make sure that PHP is running as Apache and has permissions to write to that folder.
Your path is wrong; you can't use http:// as a path, you need something like /var/www/test/data as $mypath.
Related
i'm writing a simple php script which made some stuffs every hour and write a log file.
The issue is cronjob doesnt write to log file, is it a permission issue? is it a path issue?
I have an hosting service by siteground. Script file has permission to write, read and execute.
I've already try to set a cronjob which just send me an email without write a file and it's running.Thanks
This is my code:
//
//some stuffs here
//
$file='www.domainname.com/logfile.csv';
$handle = fopen ($file, "w");
fwrite($handle, "hello, i done some stuffs");
fclose($handle);
You should use the absolute or relative path to the file on the file system.
example: -
$handle = fopen( $_SERVER['DOCUMENT_ROOT'] . '/logfile.csv' );
Generally having a writable file in the web servers directory is a bad idea. If your program can write to the file, then the web server can write to the file too. That opens the possibility for someone to overwrite the file through the web server. At best you lose data from being over written. At worst it allows remote code execution.
It would be better to put the writable files outside the web server directory.
<Parent directory>
<Place for writable files>
<Web server directory>
In your programs run from the webserver you just need to specify the path to the files.
Relative Path
$relitivePath = '/../writeDir';
$handle = fopen( $_SERVER['DOCUMENT_ROOT'] . $relitivePath . '/logfile.csv' );
Full Path
$fullPath = '/somedir/ParentDir/writeDir'
$handle = fopen( $fullPath . '/logfile.csv' );
Running from cron a bit different than running it from shell. The environment you are use to is not set up like yo have when sshing in. Your going to have to list the full path or set up the environment.
$fullPath = '/somedir/ParentDir/writeDir'
$handle = fopen( $fullPath . '/logfile.csv' );
To get your full path
When ssh'ed in:
cd to the directory where the file located.
Do a pwd this will print the current working directory path.
Use that path in like above.
In the program it is a good idea to turn error reporting on.
You also don't want to have the program you call from cron in the web directory.
I see your service provider has a way to email any output from cron. That will help with error reporting.
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.
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.
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);
?>
I'm trying to load data from a CSV on my Windows PC into a database, something I've successfully done previously. fopen can't find my input file.
Here's the specific code I'm having trouble with:
<?php
ini_set('track_errors', '1');
$handle = fopen("C:/Users/Sam/Documents/test.csv", 'r') or die("can't open file: $php_errormsg");
?>
The error printed is:
[function.fopen]: failed to open stream: No such file or directory
The file definitely exists, and I get the same problem on Unix machines. How do I fix this?
Windows 7 (and Vista?) only lets a user access his own home directory and does not allow Apache (or other users) to. Unfortunately, this is a major headache, and I would suggest that you just move the file somewhere public.
This type of behavior is easier to fix in Linux, but you're still better off moving the file out of your directory into some path where Apache has read access.