Forum Moderators: coopster

Message Too Old, No Replies

PHP unlink under windows

         

vevs

3:08 pm on Jan 18, 2006 (gmt 0)

10+ Year Member



I am doing a CMS for a client web site but cannot make the unlink work on a windows based server. I have no problem uploading a new file on the server. But when I try to upload a new version of that file with the same name, the move_uploaded_file does not overrite the existing file.
I added a check to see if there is a file and delete it but still no luck.

Any help will be appreciated. Thanks

if (is_uploaded_file($HTTP_POST_FILES['fileupload']['tmp_name'])) {
if (is_file($path.$SOQname)) unlink($path.$SOQname);
if (move_uploaded_file($HTTP_POST_FILES['fileupload']['tmp_name'], $path.$SOQname)) {
$message = '<strong>New SOQ uploaded.</strong><br>';
} else {
$message = '<strong>uploaded but not moved to the correct folder.</strong><br>';
};
} else {
$message = '<strong>not uploaded.</strong><br>';
};

coopster

3:44 pm on Jan 18, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Sounds like you are able to get the file over there in the first place so I don't foresee any permissions issues, but you never know. From the manual pages for move_uploaded_file() [php.net]:


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.

Make sure error_reporting [php.net] is turned up and you aren't getting any messages.

Two other things I note. First, the old $HTTP_POST_VARS array, although still available, has been deprecated. You should really start using the new superglobals [php.net]. Second, I always like to incorporate the $_FILES error array during an upload. It will offer you the ability to tell your end user exactly why a file upload error occurred in a nice message you can format yourself and it makes it a lot easier to edit check and troubleshoot a file upload script.

vevs

3:52 pm on Jan 18, 2006 (gmt 0)

10+ Year Member



Thanks coopster,

error_reporting(9) is set. No error message.

this is the $HTTP_POST_FILES
Array ( [fileupload] => Array ( [name] => 02.jpg [type] => image/pjpeg [tmp_name] => C:\PHP\uploadtemp\php328.tmp [error] => 0 [size] => 24892 ) )

this is the $_FILES
Array ( [fileupload] => Array ( [name] => 02.jpg [type] => image/pjpeg [tmp_name] => C:\PHP\uploadtemp\php328.tmp [error] => 0 [size] => 24892 ) )

I do not think that the problem is because of using HTTP_POST_FILES and not FILES because I am able to upload the file the first time but not second time. I believe that there is something to do with file permissions and not being able the move_uploaded_file to move the file.
Do you know some *working* way to delete a file under windows, as unlink does not work?

I tried system("del filename") but still no luck :(

Vevs

dreamcatcher

4:12 pm on Jan 18, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



is the $path variable correct?

coopster

5:02 pm on Jan 18, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member




error_reporting(9) is set. No error message.

There will be no warning error messages displayed as that setting is too low. E_WARNING is a level 2 and you are at 9 which is

E_ERROR ¦ E_NOTICE, which is to say (1¦8)
. I highly recommend reporting ALL errors in a development environment as it aids in troubleshooting [webmasterworld.com]. Don't forget to turn all errors off when promoting to your live server though.

Yes, even though both the superglobal and HTTP_*_VARS can exist at the same time and are indeed going to contain the same thing I'm just letting you know that the old HTTP_*_VARS are deprecated which means you should get in the practice of using the new superglobals.

Lastly, since your $_FILES['error'] value is returning '0' it seems your upload is working fine so indeed it is in the moving of the file that your troubles start.

Crank up the error_reporting to see what kind of message you are getting.

vevs

5:05 pm on Jan 18, 2006 (gmt 0)

10+ Year Member



thanks again coopster.

Warning: unlink(D:\users\*****\SOQ_web.jpg) [function.unlink]: Permission denied in D:\users\fecontracting.com\admin.php on line 33

Warning: move_uploaded_file(D:\users\*****\SOQ_web.jpg) [function.move-uploaded-file]: failed to open stream: Permission denied in D:\users\fecontracting.com\admin.php on line 34

I am trying to chmod (checking via FTP is says the permission is 777) and actually I do not think chmod works under windows anyway :)

any other ideas?

coopster

7:49 pm on Jan 18, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I'm not certain what might be causing your issue but a couple things to check.

First, are you sure the path and filename look correct as dreamcatcher asked? Dump them to your browser to be sure, and pay close attention to the DIRECTORY_SEPARATOR to make sure it looks like a WINDOWS directory separator. Make sure this directory and path exist, too. After you dump the value to your browser, cut and paste the directory into a windows explorer address to make sure you can navigate to it.

If that all checks out then you might try testing by running the server as an admin to see if that resolves the issue. If this does it, then you certainly have a Windows OS permissions issue.

vevs

8:15 pm on Jan 18, 2006 (gmt 0)

10+ Year Member



yes, it was a permission issue. Contacted the person who administrates the server and he fixed it. Thanks to everyone who looked at my post.

coopster

8:20 pm on Jan 18, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Out of curiosity, vevs, and for future readers, if you might tell us what has to happen on a PHP/Windows implementation to overcome this issue we would be deeply grateful.