/    Sign up×
Community /Pin to ProfileBookmark

Can’t copy or rename file with exact name as another file

I’ve come across a curious problem. The short story is that I have a form where a user uploads a photo. I move that photo into a files folder and then copy that photo into an images folder. For some reason I’m not able to copy the image into that specific folder unless I change the name of it.

Here’s the copy code:

[CODE]copy(‘/absolute/path/to/files/file.jpg’,’/absolute/path/to/images/file.jpg’);[/CODE]

I don’t get any errors or anything, but it just doesn’t work. Also, I have permissions set to 777 on everything.

The more curious part is I CAN copy the image several other ways. All the examples below work:

// Copy to parent folder

[CODE]copy(‘/absolute/path/to/files/file.jpg’,’/absolute/path/to/file.jpg’);[/CODE]

// Copy with a different name

[CODE]copy(‘/absolute/path/to/files/file.jpg’,’/absolute/path/to/images/file_2.jpg’);[/CODE]

// Copy to parent folder with a different name, rename to original name

[CODE]copy(‘/absolute/path/to/files/file.jpg’,’/absolute/path/to/images/file_2.jpg’);
rename(‘/absolute/path/to/file_2.jpg’,’/absolute/path/to/file.jpg’);[/CODE]

I even tried copying the image to the correct folder with a different name and then renaming it to the original name like this:

[CODE]copy(‘/absolute/path/to/files/file.jpg’,’/absolute/path/to/images/file_2.jpg’);
rename(‘/absolute/path/to/images/file_2.jpg’,’/absolute/path/to/images/file.jpg’);[/CODE]

But that doesn’t work. I can rename it to something else in the intended folder (say file_2a.jpg) but I can’t rename it to what it originally was.

I’ve tried deleting the folder (/images/) and recreating it. I’ve tried several different files and file types. I’ve tried usting file_get_contents/fopen/fwrite instead of copy, but nothing works.

Any ideas?

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@Jarrod1937May 08.2010 — You said you deleted the images folder, but i'd just like to make sure that you've checked and the images directory doesn't contain a file already called file.jpg that has permissions that prevent you from overwriting it?

And do you get any type of specific error when trying to copy the file? How about when you copy and then try to rename the file? This may not be reported by php (depending on if its a server error or a php error and depending on your php error level set) so you may want to check your server logs too.
Copy linkTweet thisAlerts:
@tirnaMay 08.2010 — This is from the online PHP manual for copy().


The destination path. If [I]dest[/I] is a URL, the copy operation may fail if the wrapper does not support overwriting of existing files.
[/QUOTE]


Maybe try using a relative path instead of an absolute path if it is a url.

This test code works fine on my local server overwriting the existing destination file on subsequent script executions

[code=php]<?php

if(copy('pic1.jpg','./junk/pic1.jpg'))
echo 'success';
else
echo 'fail';

?>[/code]


copy() returns true or false
Copy linkTweet thisAlerts:
@lonelycastleauthorMay 11.2010 — Tried relative paths, but it didn't help. Check to make sure file didn't exist (it doesn't). Checked server error logs (nothing). Turned on all PHP errors (nothing).

I was playing around with the folder names a bit and was able to get it to work though.

Copy did NOT work:

uploads/photos/o/

uploads/photos/t/

Copy did work:

uploads/photos/

uploads/photos2/

uploads/photos2/o/

uploads/photos2/t/

I guess that's a solution, but this is on a CMS I use for dozens of sites and I really don't want have to go around changing all the paths. Tried deleting the /photos/ folder and recreating it to see if there was something wrong with it, but still doesn't work.

Any idea of what could cause this problem?
×

Success!

Help @lonelycastle spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.25,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...