0

I am trying to copy() a file from a remote server with the copy() command as below:

<?php

    error_reporting(E_ALL);

    $url = $_GET['url'];

    if (copy( $url, '/tmp/copy_from_url.jpeg'))
    {
        echo 'copied';
    }
?>

It seems to work as copied is displayed however there is no file to be found.

Any ideas on how I can track down the problem here?

I am running MAMP on Lion both the latest versions. The file in question is a .jpeg and allow_url_fopen is on.

Edit: the folder Applications/MAMP/tmp is actually a symbolic link to /private/tmp where the files ARE being copied but are not visible.

Is there a way to change PHP tmp folder?

ian
  • 11,605
  • 25
  • 69
  • 96
  • Why did you turn notices off? Does `readfile($url)` work? Does file_exists and readfile work with the target filename after copying, in the if? – mario Dec 14 '11 at 06:57
  • `readfile($url)` works on the remote file and returns data. `file_exists()` does not seem to return anything on the `dest` value of `copy()` the file is being copied but not saved. I don't know why I had `notices` off no errors with `notices` on. – ian Dec 14 '11 at 07:08
  • What are the exact permissions `ls -l /tmp` of the target dir? Anything in the error.log? Does creating files via file_put_contents work? – mario Dec 14 '11 at 07:23
  • `lrwxr-xr-x@ 1 root wheel 11 Jul 20 23:44 /tmp -> private/tmp` – ian Dec 14 '11 at 07:28
  • Well, looks like ordinary processes don't have write permission then. – mario Dec 14 '11 at 07:32
  • Mario are you going to write this up as an answer? – ian Dec 14 '11 at 17:14

3 Answers3

0

You should change users for apache and mysql server in MAMP setting from 'www/mysql' to 'user/user' where 'user' is you mac os user - http://screencast.com/t/wzyPmFTmj6LC

Dmitrii Malyshev
  • 629
  • 9
  • 12
0

your code should be fine, but i think had better using file_get_contents() and file_put_contents() to more reliable reason, like

<?php
$source = $url;
$destination = '/tmp/copy_from_url.jpeg';

$data = file_get_contents($source);

$handle = fopen($destination, "w");
fwrite($handle, $data);
fclose($handle);
?>

see if that work ?

Khairu Aqsara
  • 1,321
  • 3
  • 14
  • 27
  • I am sure that will work but I am trying to use `copy()` rather than `fopen` or `cURL` – ian Dec 14 '11 at 07:09
  • there is some security issues about `copy()` your code is work fine, i have tried it before, using ubuntu 11.04 is work fine, maybe there is some security config in the mamp of lion, – Khairu Aqsara Dec 14 '11 at 07:14
  • Your code works without any errors but also does not write. Maybe `PHP` has the wrong permissions for the `tmp` folder? – ian Dec 14 '11 at 07:34
0

If these are your permissions ls -l /tmp

lrwxr-xr-x@ 1 root wheel 11 Jul 20 23:44 /tmp -> private/tmp – ian 9 hours 

That looks like ordinary processes don't have write permission then. The last r-x means that te other users (not root or wheel group) lack the w write right. Usually Apache runs under a separate accounts, which is why PHP also cannot access it.

mario
  • 144,265
  • 20
  • 237
  • 291