where and why PHP copy() fails?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
krishna.p
Forum Newbie
Posts: 19
Joined: Fri May 23, 2008 8:12 am

where and why PHP copy() fails?

Post 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
WebbieDave
Forum Contributor
Posts: 213
Joined: Sun Jul 15, 2007 7:07 am

Re: where and why PHP copy() fails?

Post 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");
krishna.p
Forum Newbie
Posts: 19
Joined: Fri May 23, 2008 8:12 am

Re: where and why PHP copy() fails?

Post 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
WebbieDave
Forum Contributor
Posts: 213
Joined: Sun Jul 15, 2007 7:07 am

Re: where and why PHP copy() fails?

Post 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
Post Reply