8

I am executing the following commands:

<?php
copy ("http://localhost/.../DSCF8253.JPG" , "sites/default/files/DSCF8253.JPG"); // Success!
copy ("http://localhost/.../DSCF8260.JPG" , "sites/default/files/DSCF8260.JPG"); // Success!
copy ("http://localhost/.../HERMAN 085.jpg" , "sites/default/files/HERMAN 085.jpg" ); // Fail!
?>

The first two copy fine, but not the last one. Why?

It must have something to do with the filenames (the last one has a SPACE before the 085).

Any help would be greatly appreciated!

rockstardev
  • 13,479
  • 39
  • 164
  • 296
  • What does a `error_reporting(E_ALL);` say? – Pekka Oct 13 '10 at 16:18
  • Have you tried escaping the space by replacing it with `%20` in the URL? – Justin Ethier Oct 13 '10 at 16:19
  • Are the source directories the same? – chigley Oct 13 '10 at 16:19
  • Also, you are aware that when on localhost, instead of using slow http transport, you could use a file path (that would probably work with the spaces as well)? – Pekka Oct 13 '10 at 16:19
  • @Pekka: I am actually copying from one website to another. I wish I could use filepaths instead, but I can't for what I'm doing. – rockstardev Oct 13 '10 at 16:49
  • @Justin: I need a generic solution that will work for all weird scenarios. I.e. I need to pinpoint the real problem, and not just solve it for the one case. – rockstardev Oct 13 '10 at 17:10
  • Understood, what I was getting at was that if this was the underlying solution (which does not appear to be the case), you could identify it as such. From there, implementing a generic solution is a straightforward task. – Justin Ethier Oct 13 '10 at 17:20

3 Answers3

9
http://localhost/.../HERMAN 085.jpg

Should be

http://localhost/.../HERMAN%20085.jpg

Copy & the http wrappers are less forgiving then browsers / user-agents when it comes to invalid urls. A space in an url is invalid, so it should be urlencode'd.

Wrikken
  • 69,272
  • 8
  • 97
  • 136
  • Still doesn't work, but ur definitely on the right path. I used urlencode with no luck. Any other suggestions? – rockstardev Oct 13 '10 at 17:23
  • If that doesn't work, I'd check the webserver's access logs to check whether the request is OK & returns data. If that still fails, I'd look into packages like `cURL` & `httprequest` to see if they can manage. – Wrikken Oct 13 '10 at 18:04
  • CURL ended up being the best solution. – rockstardev Oct 14 '10 at 07:50
  • Sort of. If you fully urlencode the whole URL, it will fail as well. As far as I can tell from testing, the space is one of the few characters that it will not interpret properly. Ghetto, but I'd suggest doing a str_replace to change spaces to %20. – roktechie Dec 11 '12 at 20:03
  • Check out `rawurlencode` it solved my problem in this case. http://stackoverflow.com/questions/4744888/how-to-properly-url-encode-a-string-in-php – Jazzy Nov 02 '14 at 05:27
3
//i used this code once i tried to copy images to a wordpress site and set post featured image
//i only mentioned the part you want and did not mention other parts
$image_url = 'http://example.com/images/my image with spaces.jpg';
try {
    //throw exception if can't move the file

    if (!copy(str_replace(" ","%20",$image_url),$file)) {
        $errors = error_get_last();
        throw new Exception('Could not copy file');
    }   

} catch (Exception $e) {
    echo $e->getMessage();
}
 //using urlencode will corrupt the url if used with the full url
 //it will generate something like http%dsf%swdfablablabla
 //if you need to encode you will encode anything after http://yoursite.com/{encode only works here}
ashraf mohammed
  • 1,322
  • 15
  • 20
1

Strangest thing: the %20 way doesn't seem to do the trick, but after some trying in vain (first with %20, then quoting the filename, then double-quoting, then protecting spaces, tell me if I missed something), now the original version works flawlessly. This is Windows 10, PHP 5.5.12 and we are in year 2016. Good luck with all these deterministic, finite-state systems :)

A possible solution btw, is to use exec() and do a copy on the operating system level. Then again, it's OS specific.

dkellner
  • 8,726
  • 2
  • 49
  • 47