PHP Copy function not working

不羁岁月 提交于 2019-12-24 04:17:11

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!