Can't copy file with php copy

I get this message:

Warning: copy(http://www.sprinklerrepairpearland.com/contractor_seal2.gif) [function.copy]: failed to open stream: HTTP wrapper does not support writeable connections in /home/content/03/6424703/html/site_clone.php on line 43

Line 43 is copy($fip, $tip);

fip = http://www.sprinklerrepairkaty.com/contractor_seal2.gif
tip = http://www.sprinklerrepairpearland.com/contractor_seal2.gif

What's wrong? Do I need the absolute file path on the server?
Richard KortsAsked:
Who is Participating?
 
gr8gonzoConnect With a Mentor ConsultantCommented:
To expand on what I was saying in my first comment, when you work with many of the file functions (copy, rename, file_get_contents, etc...), PHP will look at the filename and determine how to work with that particular filename.

So for example, if you say:
$contents = file_get_contents("test.txt");

Open in new window

Then PHP will look in the current directory on the current server's filesystem for a file called test.txt.

$contents = file_get_contents("/path/to/test.txt");

Open in new window

PHP will look in a folder called /path/to on the current server's filesystem for a file called test.txt.

$contents = file_get_contents("http://www.site.com/test.txt");

Open in new window

PHP sees "http://" and instead of checking the current server's filesystem, it gets a helping hand from another library that knows how to deal with URLs. So PHP passes the URL to that library (the HTTP wrapper), and the wrapper allows PHP to download the file contents.

However, there is no way to simply save a file to an HTTP address (you can use WebDAV, but even that is a different technology than plain ol' HTTP). You can pass data to HTTP like a form post or even a form with an attachment, but unless the receiving script is built to handle the attachments, that data will simply vanish after you upload it. That said, there's really nothing the HTTP wrapper can do to allow you to "write" files to a URL, which is why you get that error.

But you CAN write to the local filesystem, so if you were to create a PHP script on the destination server that looked like this:

copy("http://www.fromsite.com/from/file.jpg", "/path/to/file.jpg");

Open in new window

Then PHP will use the HTTP wrapper to read/download the file from "www.fromsite.com", and then once it has it in memory, it will save the file to the current server in /path/to/file.jpg.
0
 
gr8gonzoConsultantCommented:
What's wrong is that you cannot copy a file to a URL and just have it show up there. Otherwise, I could run the same script and then start putting files onto YOUR server (big security risk, right?).

You need to run the script ON the server that hosts the sprinklerrepairpearland.com, and then specify a local directory/folder on that server.
0
 
Marco GasiFreelancerCommented:
If you're working with Apache, you must set permissions correctly for that folder: you can set them to 0777, but I see it is the root folder. I suggest you to store those images in another folder and set permissions for this new folder only to 0777. Giving 0777 to the root folder is bad, bad , bad practice :)

Cheers
0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

 
Robert SaylorSenior DeveloperCommented:
giving 777 to the root folder will also cause php to fail if your host is running phpsuexec. Create a new directory and set it to 777 then you will be able to write to it.

Give this example a try:

http://stackoverflow.com/questions/724391/saving-image-from-php-url-using-php
0
 
gr8gonzoConsultantCommented:
@experts - read the question more carefully. It is not a permissions issue (at least not yet):

"HTTP wrapper does not support writeable connections"
0
 
Dave BaldwinFixer of ProblemsCommented:
@gr8gonzo is right.  You can copy From a URL but not To a URL.  Even if WebDAV was enabled on the destination server, you would have to be logged in to use it.  PHP thru the web server is not going to be allowed to do that.
0
 
Richard KortsAuthor Commented:
To gr8gonzo.

How do I get the "to" path; I know I can do that somehow in php; I tried realpath, it gave me nothing.

Thanks
0
 
gr8gonzoConsultantCommented:
Hi rkorts,

That's not something I can answer without actually looking at your server's setup. You should have a folder structure somewhere on the server that holds the files for the "http://www.sprinklerrepairpearland.com" site. It's up to you to know where that folder is, and then simply set the path to it.

For example, if you had /www/html/sprinklerrepairpearland.com/htdocs, I would say that's your "to" path.
0
 
Ray PaseurCommented:
Are both of these web sites hosted on the same server?  If so, you may be able to get around this issue by using a file path.  But you will never be able to write to a URL.

If you cannot get to both files via file paths, you will need to write a web service that can run on the "to" server.  It will request the data from the "from" server and write it into its own directory tree.  You should be able to poke this web service with the "from" server to initiate the transfer whenever the updates are needed.
0
 
Richard KortsAuthor Commented:
I thought of another way around this.

The sites ARE hosted on the same server. And bunches more to come.

Thanks
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.