问题
I'm trying to use copy function in php to copy an image from a url to my server.
The function opens the right folders on the server but the file is not in the last folder. its just empty.
Here's my piece of code:
$srcfile="http://domain.com/images2/2014/01/02/1.jpg";
$dstfile="/images/2014/01/02/1.jpg";
mkdir(dirname($dstfile), 0777, true);
copy($srcfile, $dstfile);
Any idea on why could this be?
Any help would be highly appreciated.
回答1:
Try this:
$srcfile="http://domain.com/images2/2014/01/02/1.jpg";
$dstfile=$_SERVER['DOCUMENT_ROOT'] . "/images/2014/01/02/1.jpg";
mkdir(dirname($dstfile), 0777, true);
copy($srcfile, $dstfile);
回答2:
$srcfile="http://domain.com/images2/2014/01/02/1.jpg";
I would suggest to determine the above file location from the copy function file. Write the jump location from there. e.g
$srcfile="../images2/2014/01/02/1.jpg";
Same goes with your destination file.
$dstfile="../images/2014/01/02/1.jpg";
The deeper the folder the more jump you need. e.g.
$dstfile="../../../images/2014/01/02/1.jpg";
Sometime writing full URL will not work because it will be like this :
$dstfile="http://domain.com/http://domain.com/images2/2014/01/02/1.jpg";
Which is just a repeating URL.
来源:https://stackoverflow.com/questions/21511889/php-copy-function-not-working