Solved

Can't copy file with php copy

Posted on 2013-07-01
10
1,105 Views
Last Modified: 2013-07-01
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?
0
Comment
Question by:Richard Korts
10 Comments
 
LVL 34

Expert Comment

by:gr8gonzo
ID: 39290571
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
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 39290575
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
 
LVL 34

Accepted Solution

by:
gr8gonzo earned 500 total points
ID: 39290686
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
ScreenConnect 6.0 Free Trial

Check out the updates in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI that improves session organization and overall user experience. See the enhancements for yourself!

 
LVL 7

Expert Comment

by:Robert Saylor
ID: 39290687
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
 
LVL 34

Expert Comment

by:gr8gonzo
ID: 39290695
@experts - read the question more carefully. It is not a permissions issue (at least not yet):

"HTTP wrapper does not support writeable connections"
0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 39290716
@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
 

Author Comment

by:Richard Korts
ID: 39290736
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
 
LVL 34

Expert Comment

by:gr8gonzo
ID: 39290811
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
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 39290833
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
 

Author Closing Comment

by:Richard Korts
ID: 39290891
I thought of another way around this.

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

Thanks
0

Featured Post

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question