krishna.p Posted July 10, 2008 Share Posted July 10, 2008 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 Quote Link to comment Share on other sites More sharing options...
lemmin Posted July 10, 2008 Share Posted July 10, 2008 Does your server have access to that directory? It looks like you are trying to move an uploaded file, did you try move_uploaded_file()? http://www.php.net/manual/en/function.move-uploaded-file.php Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted July 10, 2008 Share Posted July 10, 2008 A copy() will fail if there is a problem with the source or destination (the path does not exist or is wrong relative to the current script - source/destination, the file does not exist - source, php does not have permission to access the path or file, or any other problem that could occur, like a disk error or resource problem...) Turn on full php error reporting to get php to tell you why it is failing. Add the following two lines after your first opening <?php tag - ini_set ("display_errors", "1"); error_reporting(E_ALL); Quote Link to comment Share on other sites More sharing options...
krishna.p Posted July 10, 2008 Author Share Posted July 10, 2008 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 Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted July 10, 2008 Share Posted July 10, 2008 Can't tell you any specific information until you provide specific information that identifies what is different when it fails. My guess is that your upload fails due to the file size and you code has no error checking of the upload status to neither check if the upload worked/failed or to output a meaningful message about what happened and blindly continues executing the copy() code. Quote Link to comment Share on other sites More sharing options...
krishna.p Posted July 10, 2008 Author Share Posted July 10, 2008 It is not the case with upload failure. upload seems successful as my upload_max_filesize is set to 200MB in php.ini. To be breif let me clarify you. The code is $tp = substr(md5(uniqid(rand(), true)),0, 10); if(! $_FILES['f']['error'] == 0) die_log("uploading failed."); if(! copy($_FILES['f']['tmp_name'], "./directory/{$tp}")) die_log("unable to copy."); you have mentioned something like disk error or resource problem. what actually it is? please help me..thanks in advance.. Thanks, krishna.p Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted July 10, 2008 Share Posted July 10, 2008 Did you bother to do this - Turn on full php error reporting to get php to tell you why it is failing. Add the following two lines after your first opening <?php tag - ini_set ("display_errors", "1"); error_reporting(E_ALL); Quote Link to comment Share on other sites More sharing options...
krishna.p Posted July 10, 2008 Author Share Posted July 10, 2008 Thanks for the reply. i will surely implement that 2 lines of code.. and one more small question: whether this copy() depends on any server related problems? thanks in advance.. Thanks, krishna.p Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.