Solved

Can't copy file with php copy

Posted on 2013-07-01
10
1,045 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 30

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
 
LVL 6

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
Easy Project Management (No User Manual Required)

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 82

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 108

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.

758 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now