Link to home
Start Free TrialLog in
Avatar of RavenClaw
RavenClaw

asked on

Apache and PHP copy() function

I have apache 1.37 and php installed im using the test script to start with:

-------------------
Test.php
-------------------
<HTML>
<HEAD>
<TITLE>File Uploading Interface</TITLE>
</HEAD>
<BODY>
<TABLE>
<FORM ENCTYPE="multipart/form-data" NAME=MyForm ACTION=testupload.php METHOD="POST">
<TR><TD>Choose File</TD><TD><INPUT NAME="MyFile"
TYPE="File"></TD></TR>
<TR><TD COLSPAN="2"><INPUT NAME="submit" VALUE="Submit"
TYPE="submit"></TD></TR>
</TABLE>
</BODY>
</HTML>

and

----------------
testupload.php
----------------
<?
If($MyFile != "none") {
copy($MyFile,"$MyFile_name");
unlink($MyFile);
}
else {
echo"You did not upload any file";
}
?>








when i try to upload a file i get an error saying:

Warning: Unable to open '' for reading: Permission denied in d:\htdocs\testupload.php on line 3

Warning: unlink() failed (Permission denied) in d:\htdocs\testupload.php on line 4

any ideas?

and how to set permissions????
Avatar of jervis
jervis

make sure you have the directory permissioned as writable. If it is just a permission problem:

chmod og+rw dir_name should work just fine.  
Avatar of RavenClaw

ASKER

i donnt no what that means! im using apache 1.37 on windows XP
- There is no Apache 1.37 - The current Apache 1 version is 1.3.27.
- You should also state your PHP version.
- You should ensure you have set "upload_tmp_dir" in your php.ini to a valid directory.
- Your testupload.php script is flawed. Look at the example here http://www.php.net/manual/en/features.file-upload.php and work from there.
Try using the PHP function move_uploaded_file() instead of copy().

move_uploaded_file -- Moves an uploaded file to a new location.

bool move_uploaded_file (string filename, string destination)

This function checks to ensure that the file designated by filename is a valid upload file (meaning that it was uploaded via PHP's HTTP POST upload mechanism). If the file is valid, it will be moved to the filename given by destination.

If filename is not a valid upload file, then no action will occur, and move_uploaded_file() will return FALSE.

If filename is a valid upload file, but cannot be moved for some reason, no action will occur, and move_uploaded_file() will return FALSE. Additionally, a warning will be issued.

This sort of check is especially important if there is any chance that anything done with uploaded files could reveal their contents to the user, or even to other users on the same system.


ASKER CERTIFIED SOLUTION
Avatar of Zlatin Zlatev
Zlatin Zlatev
Flag of Bulgaria image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi, one guess is that your php is one of the latest version, which does not use $MyFile notation to get MyFile input item any more.  You have to use either $_POST['MyFile'] or $_REQUEST['MyFile'] (I prefer the latter, catches both POST and GET).
@RavenClaw, yep I agree with inq123 that you may not ride the right horse.
check:
print_r($MyFile);

Also note that personally I preffer using _FILES array like this:

if (($_FILES['MyFile']['size']<>0) and ($_FILES['MyFile']['error']==0)) {
  if (substr($_FILES['MyFile']['type'], 0, 6)=='image/') {
     move_uploaded_file ($_FILES['MyFile']['tmp_name'], "./".$_FILES['MyFile']['name']);
  } else {
     unlink($_FILES['MyFile']['tmp_name']);
  }
}

This will discard any uploaded file except it is valid MIME image according to client browser.

HTH
@RavenClaw, Any progress on this?
This question has been classified abandoned. I will make a recommendation to the
moderators on its resolution in a week or two. I appreciate any comments
that would help me to make a recommendation.

<note>
Unless it is clear to me that the question has been answered I will recommend delete.  It is possible that a Grade less than A will be given if no expert makes a case for an A grade. It is assumed that any participant not responding to this request is no longer interested in its final disposition.
</note>

If the user does not know how to close the question, the options are here:
https://www.experts-exchange.com/help/closing.jsp


Cd&

According to me... My comments will solve the issue... Well maybe other experts will disagree.
It sould be problem with file-system rights (on NTFS partition in Windows XP)
or problem with the fact that th variable is not initialized properly, due to misconfiguration of the PHP engine (in this case a global collection like _FILES may solve the problem)

However the asker never returned to confirm or reject this solution.
It is time to clean this abandoned question up.

I am putting it on a clean up list for CS.

<recommendation>
points to zlatev

</recommendation>

If anyone participating in the Q disagrees with the recommendation,
please leave a comment for the mods.

Cd&