?
Solved

Can't copy file with php copy

Posted on 2013-07-01
10
Medium Priority
?
1,287 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
10 Comments
 
LVL 35

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 35

Accepted Solution

by:
gr8gonzo earned 2000 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
Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

 
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 35

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 35

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 111

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

Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

Question has a verified solution.

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

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
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 count occurrences of each item in an array.
Suggested Courses

800 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