0

I cannot google anywhere that one cannot use absolute path in copy() destination. However,

$baseUrl_master_MM = "http://mysite.öä/MM/";
$img_dir = 'img_1';
$img = '01.jpg';

$orig_online = $baseUrl_master_MM.$img_dir.'/'.$img;
$dest_online = '../../mm_img/'.$img_dir.'-online.jpg';
$copy = copy($orig_preview, $dest_preview);

works fine, but the same with absolute path

$baseUrl_master_MM = "http://mysite.öä/MM/";
$baseUrl_master_MM_online = "http://mysite.öä/mm_img/";
$img_dir = 'img_1';
$img = '01.jpg';

$orig_online = $baseUrl_master_MM.$img_dir.'/'.$img;
$dest_online = $baseUrl_master_MM_online.$img_dir.'-online.jpg';
$copy = copy($orig_preview, $dest_preview);

will give no errors and copies no files.

Destination directory exists, and rights are 777. Am I missing something?

Konservin
  • 997
  • 1
  • 10
  • 20
  • 1
    Yes, you're missing something. When the source is an URL, PHP is smart enough to download it for you (it actually uses HTTP for this - it has no idea that it's your website/the current website) and then when you try to use it in the destination, it obviously gives up as it can't just upload to a website. Just use an absolute *server* path (`eg. /home/user/www/mm_img/` or `C:\wamp\www\mm_img\ `). `dirname(__FILE__)` will give you the directory of the current file. An absolute path is not the same as an absolute URL. – h2ooooooo Jun 03 '14 at 11:54
  • What you're using is not the server's absolute path, it's the public web address.If you really want the absolute path see this question: http://stackoverflow.com/questions/4645082/get-absolute-path-of-current-script – Arthur Silva Jun 03 '14 at 11:56
  • I'd mark H2o*n's comment as correct answer, if it wasn't a comment.. – Konservin Jun 03 '14 at 16:21

2 Answers2

1

Please do the following :

   if(!@copy($orig_preview, $dest_preview))
    {
        $errors= error_get_last();
        echo "COPY ERROR: ".$errors['type'];
        echo "<br />\n".$errors['message'];
    } else {
        echo "File copied from remote!";
    }

Tell what the error's you see?

Also absolute path should look like :

   $abs =  $_SERVER['PHP_SELF'];

Sorry for post as answer , couldn't post as comment

Ravg
  • 219
  • 4
  • 18
  • Yes, it gives: COPY ERROR: 2 copy(httu://www.mysite.öä/mm_img/MM-1167-preview.jpg): failed to open stream: HTTP wrapper does not support writeable connections. After getting less ignorant on absolute and public paths, it gives File copied from remote! Thank you all! – Konservin Jun 03 '14 at 12:10
1

You can't use the HTTP protocol to copy a file on a server. Taken from the PHP documentation on HTTP wrapper

Allows read-only access to files/resources via HTTP 1.0, using the HTTP GET method.

To copy to your local server, use an absolute path.

Elwinar
  • 9,103
  • 32
  • 40
  • You might want to explain to OP what an "absolute path" is, as OP said "*works fine, but the same with absolute path*". – h2ooooooo Jun 03 '14 at 12:03