Re: write with cURL



Ylva Poelman wrote:

Tim Greer wrote:
Ylva Poelman wrote:

Tim Greer wrote:
user@xxxxxxxxxxxxxx wrote:

Hi,

I want do create a new file and write something in it like:

$file = fopen("test.txt","w");
fwrite($file,"Hello World. Testing!");
fclose($file);

My provider doesn't allow this action for security reasons.
However they mention using cURL to do the same thing.

I have been searching the Internet to find out how to do this, but
did not find the answer. A did find a lot of answers to others
questions, but not this one. ;-))

So any help is appreciated very much.

Cheers,

Ylva
If your host says they deny opening and writing to a local file
because it's insecure, then it's time you find a new host that
knows what they
are doing. There's not much point to hosting on someone that
offers PHP, if they don't allow PHP to open (or create) and write
to files. Are you sure there's not some other specific issue of why
it's failing?
How exactly is is failing? I don't see the reason for them to
mention Curl as a replacement method for your task (that doesn't
make sense). Perhaps it's a permissions/ownership issue, or you're
trying to open a
file over FTP/http or something on another site or system? A lot
of people deny open for URL's, which can be a good thing, so it
might depend.

I have just this little script:
<?php
$file = fopen("test.txt","w");
fwrite($file,"Hello World. Testing!");
fclose($file);
?>

nothing else

Sorry, I wasn't implying that you had anymore to the situation, but
was just curious if perhaps there was a URL you were trying to open
at some
point or it there was more to the script. Anyway, this could be a
permissions (or ownership/permissions) issue. I'd be surprised if
any qualified web host denied people from using fopen in PHP to
create or
write to a file. Check the permissions on the directory the file is
to be created in (or written to in) as well as the file itself, if
test.txt exists already.

Consider testing world write permissions, such as chmod 0666 test.txt
or
chmod 0777 test.txt and see if you can write to it then. If the file
doesn't exist, ensure the directory itself is set to chmod 0777 and
test it. I'd recommend putting it back to chmod 0755 or 0711 or 0750
or 0710, depending on what it is now, because you'd not want your
main web root world writable if that's what you have to do to test
it. Which is also another point, you can just change the path and use
a test directory with world write permissions, so it doesn't risk
anything else being at risk from too open permissions, in case you
don't remember to change it back -- this also depends on how secure
their server configuration is, too, though.

The web host staff might have been confused about the issue or
question that you've reported, especially if they claim it's a
security issue
and then recommend using curl (that really doesn't make sense). I'm
thinking it's a permissions issue for the global web server user that
PHP is likely running as, rather than them somehow disabling fopen
itself.


This was the reply (in Dutch) from the helpdesk:

Remote PHP execution (allow_url_fopen) betekent eigenlijk dat je vanaf
afstand PHP uitvoert op een server.
Dus de PHP code staat op server B en die wordt gelezen en uitgevoerd
op server A.
Op deze manier worden heel veel website misbruikt (gehacked).

It means:

Remote PHP execution (allow_url_fopen) means that you remotely run PHP
on a server. So the PHP code is on server B and is executed on server
A. This is exactly the way in which a lot of websites are hacked.

If I already have a file present


$file=fopen("test.txt","r");
fclose($file);

is ok, but

$file=fopen("test.txt","w");
fclose($file);

gives me
Warning: fopen(attachment.txt) [function.fopen]: failed to open
stream: Permission denied in /home/ypo...

Right, opening a URL is something a lot of hosts disable for good
reason, but fopen itself is unrelated if you're just opening a local
file and is a different issue altogether.

Since you can read the file, it already exists, so it's a write issue.
I'm certain your host wouldn't try and modify PHP to only allow fopen
read on local files but deny write on local files, so it must be a
permissions/ownership issue.

As requested previously, please try changing the permissions on the
text.txt file you're writing to, to world write (chmod 0666 or chmod
0777). See if that allows you to write to the file without error. If
you're using a GUI for setting permissions, set it to rw-rw-rw or
rwx-rw-rw or rwxrwxrwx and see if that allows you to write.

I'm assuming you've uploaded the file over FTP or created it in some GUI
editor that saved it. It's likely defaulting to the (umask file
creation/upload) permissions of (0)644, which only allows your user
access for write (6), while group and other (world) are only allowed to
read (4). Set it to write for world/other and I bet this solves the
problem.
--
Tim Greer, CEO/Founder/CTO, BurlyHost.com, Inc.
Shared Hosting, Reseller Hosting, Dedicated & Semi-Dedicated servers
and Custom Hosting. 24/7 support, 30 day guarantee, secure servers.
Industry's most experienced staff! -- Web Hosting With Muscle!
.



Relevant Pages

  • Re: write with cURL
    ... execute permissions. ... This is assuming that the PHP script runs ... of potential security risks from other users on the same server. ... Apache as the global web server user and thus needs world write ...
    (alt.php)
  • Re: write with cURL
    ... execute permissions. ... This is assuming that the PHP script runs ... of potential security risks from other users on the same server. ... web server itself is part of the group. ...
    (alt.php)
  • Re: PHP chmod Newbie Question
    ... How does the server keep ... how do I create user groups for chmod to recognize? ... CHMOD is for UNIX based servers, and the PHP chmod() ... edited or removed by the PHP script, and only read by the UNIX ...
    (comp.lang.php)
  • Re: write with cURL
    ... execute permissions. ... This is assuming that the PHP script ... runs as the global web server user (and it surely does given ... Apache as the global web server user and thus needs world write ...
    (alt.php)
  • Re: setting file permissions on a web server
    ... If permissions are kept intact you can ... you transfer first and chmod on the server. ... Since I'm running Windows, I assume that I can't run the chmod line on my own computer. ... My web server uses Unix, but is it possible for me to have access to a command line for the server? ...
    (comp.lang.python)