View Full Version : How does copy() function work?
psikolog
09-02-2009, 01:48 PM
Hi,
copy(source_file,target_file)
Where should be the source file ?
On client?
On server?
An URL on web?
Can we solve this problem?
psikolog
09-02-2009, 03:17 PM
I think I got the solution :
Before PHP Version 5.2.9 "the source file" could be placed on local computer or on remote server (on the web) or it could be URL on the web.
The location of the source file doesn't make any difference if the server has the PHP Version older than 5.2.9. There will be no errors while uploading a file from the user's computer to the web server that the script is running on via a form enctyped as multipart/form-data.
But in version 5.2.9 the source file can only be either a URL on the web or a path on remote server (on the web). Copy() function interprets the POSTed file path as a path on remote server on the web.
If the above commitment is incorrect or missing some points please let me know.
Nejat
JShor
09-02-2009, 03:28 PM
Can be either one. For example ...
copy("C:/Desktop/MyFile.txt", "uploads/");
-OR-
copy("../files/MyFile.txt", "uploads/");
-OR-
copy("/home/user/files/MyFile.txt", "uploads/");
For more info, see:
http://php.net/copy
psikolog
09-02-2009, 03:36 PM
I know what www.php.net declares.
But in version 5.2.9 user can't upload a file to the server via a form.
I tested the code
1. on my local server (on my computer) => it worked.
2. on Yahoo server with version 4.3.11 => it worked.
3. on another server with version 5.2.9 => it did not worked.
Thank you. I will appreciate your comments.
Nejat
JShor
09-02-2009, 04:52 PM
Right, like posted before, sounds to me like it's a configuration problem with the server. That happens. I'll google for you and tell you what I come up with.
BTW, see:
http://www.dynamicdrive.com/forums/showthread.php?t=48086&page=2#12
JShor
09-02-2009, 04:56 PM
Problem is, we don't know WHAT exactly is going on. This code may help you:
<?php
function file_upload_error_message($error_code) {
switch ($error_code) {
case UPLOAD_ERR_INI_SIZE:
return 'The uploaded file exceeds the upload_max_filesize directive in php.ini';
case UPLOAD_ERR_FORM_SIZE:
return 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form';
case UPLOAD_ERR_PARTIAL:
return 'The uploaded file was only partially uploaded';
case UPLOAD_ERR_NO_FILE:
return 'No file was uploaded';
case UPLOAD_ERR_NO_TMP_DIR:
return 'Missing a temporary folder';
case UPLOAD_ERR_CANT_WRITE:
return 'Failed to write file to disk';
case UPLOAD_ERR_EXTENSION:
return 'File upload stopped by extension';
default:
return 'Unknown upload error';
}
}
$dest = "upload/";
copy($_FILES['file'], $dest);
// Example
if ($_FILES['file']['error'] === UPLOAD_ERR_OK)
// upload ok
else
$error_message = file_upload_error_message($_FILES['file']['error']);
?>
... taken directly from:
http://us3.php.net/manual/en/features.file-upload.errors.php
[that may or may not work. proofread the code]
But point of that code will be, if it's a server problem, you'll either get case 6 or 8.
JShor
09-02-2009, 04:58 PM
I think I found another possibility:
http://us3.php.net/manual/en/features.file-upload.common-pitfalls.php
IF the file is fairly large, there could be a timeout limit -OR- the server MAX_UPLOAD_SIZE is not letting you upload a file that big,
Try uploading a really really tiny file, like a .txt file with nothing in it. If that fails, then this idea should be knocked off the board.
psikolog
09-02-2009, 05:13 PM
All php.ini values are ok, and my filesizes are between 1 kb and 17 kb.
I also used the same file (3 kb) for all my tests.
In fact before posting this issue I did a wide research (took my 3 days), I reached to an explanation but I could not solve it.
Hoping to solve this problem alltogether.
Thank you.
Nejat
psikolog
09-02-2009, 05:17 PM
The error message is :
Warning: copy(C:\\wamp\\www\\noyaevofis\\images\\block.png) [function.copy]: failed to open stream: No such file or directory in .........
I think the server : "there is no path such 'C:\\wamp\\www\\noyaevofis\\images\\block.png' in my disk." says.
JasonDFR
09-02-2009, 05:25 PM
Why don't you guys consolidate this to just one thread.
There is definitely no path like this: C:\\wamp\\www\\noyaevofis\\images\\block.png
Now what could be causing you to have two directory separtors instead of one? I do not know...
psikolog
09-02-2009, 05:44 PM
\\ is normal now in HTML5. It acts as \.
I am sure about this because I tested both versions :
C:\\wamp\\www\\noyaevofis\\images\\block.png
and
C:\wamp\www\noyaevofis\images\block.png
The explorer takes \\ as \.
For your last post about $_FILES :
$_FILES['file_name']['name'] gives only the name and extension of the file but not the full path.
I spent all my afternoon yesterday for working on $_FILES, but no use.
I still have hope.
Nejat
JShor
09-02-2009, 05:54 PM
But this is php that's processing the file names. Just leave it as one forwardslash ("/").
psikolog
09-02-2009, 05:57 PM
Well done, but I have allready tested it also.
JShor
09-02-2009, 06:03 PM
OK, but for future reference, it should be left as only one slash.
psikolog
09-02-2009, 06:16 PM
I can imagine that.
I have the below lines in my code for this future danger :
$find =chr(92).chr(92) ;
$logoAdres = str_replace($find,chr(92),$logoAdres);
Thank you.
Nejat
psikolog
09-03-2009, 08:28 AM
I solved the problem today at 10:33 GMT+3.
It has nothing to do with neither php version nor directory structure/naming.
It is because of the way that HTML5 handles the POSTed data. Depending on this change in HTML5, the copy() function is handling the source file as : "The source file of the copy() function must be the temp_name not the full path."
$recName = $_FILES['file_name']['tmp_name']
This link may give a brief idea about new HTML5 and DOM specs. : http://www.mail-archive.com/whatwg@lists.whatwg.org/msg14076.html
The following final code is tested on 2 different remote servers for IE and FireFox. No errors occured, all the files POSTed were saved into the related directories in both explorers.
<?php
if (!isset($_REQUEST['PLogo'])) {$PLogo="";} else $PLogo = $_REQUEST['PLogo'] ;
if (!isset($_REQUEST['Pname'])) {$Pname="";} else $Pname = $_REQUEST['Pname'] ;
if (!isset($_REQUEST['action'])) {$action="";} else $action = $_REQUEST['action'] ;
?>
<form name="Fedit" method="post" enctype="multipart/form-data">
<table align="center" width="600" cellspacing="2" cellpadding="2" style="border:1px outset #ECECEC;">
<tr>
<td bordercolor="#ececec" class="urunkart">Logo : </td>
<td bordercolor="#ececec">
<input type="file" name="PLogo" size="70" style="font:7pt verdana;color:#003399" >
</td>
</tr>
<tr>
<td bordercolor="#ececec" class="urunkart">Name for Logo : </td>
<td bordercolor="#ececec"><input type="text" name="Pname" size="50" maxlength="255" style="font:7pt verdana;color:#003399"></td>
</tr>
<tr>
<td bordercolor="#ececec" align="center" colspan="2">
<input type="submit" name="action" value="Save" onmouseover="this.style.cursor='pointer';">
</td>
</tr>
</table>
</form>
<?
$action = strtolower(trim($action));
switch($action)
{
case"save" : action_update($PLogo,$Pname);
break;
}
function action_update($PLogo,$Pname)
{
$PLogo = $_FILES['PLogo']['tmp_name'] ;
if ($PLogo !=="" )
{
chdir("./logo");
if (copy($PLogo, $Pname)) { echo ("$PLogo saved"); } else { echo ("$PLogo is not saved"); }
chdir("..");
}
}
?>
I thank all of you for your valuable supports.
Nejat
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.