Page 1 of 1

where and why PHP copy() fails?

Posted: Thu Jul 10, 2008 10:09 am
by krishna.p
Hi, iam new to php. please tell me the basic reasons why PHP copy() function fails?
In my application users upload a file and all the uploaded files are copied to one stage folder by giving them randomly created number as file name.
my Sample code is given below.

$tp = substr(md5(uniqid(rand(), true)),0, 10);
if(! copy($_FILES['f']['tmp_name'], "./directory/{$tp}"))
die_log("unable to copy.");
please tell me the reasons why this copy() fails? thanks in advance.

Thanks,
krishna.p

Re: where and why PHP copy() fails?

Posted: Thu Jul 10, 2008 10:28 am
by WebbieDave
Try this to see if it shed any light on the reason for failure:

Code: Select all

ini_set('track_errors', 1);
$tp = substr(md5(uniqid(rand(), true)),0, 10);
if(!copy($_FILES['f']['tmp_name'], "./directory/$tp")) 
    die_log("unable to copy: $php_errormsg");

Re: where and why PHP copy() fails?

Posted: Thu Jul 10, 2008 11:13 am
by krishna.p
Thanks for the reply..iam seeing success for the most of times but sometimes im getting the error. dont know what is the exact reason why the copy() fails sometimes ? any help would be greatly appreciated.

Thanks,
Krishna.p

Re: where and why PHP copy() fails?

Posted: Thu Jul 10, 2008 11:34 am
by WebbieDave
When it fails, $php_errormsg should contain the reason why. Common causes are bad directory and no permissions.
http://us.php.net/manual/en/reserved.va ... rormsg.php
http://us.php.net/manual/en/function.er ... orting.php
http://us.php.net/manual/en/errorfunc.c ... lay-errors