Jump to content

Renaming files


dbo

Recommended Posts

To avoid naming collisions and add a bit of security I'm generating filenames based off of an md5 hash. It works all fine and dandy until I try to use the rename function on the hash. Apparently there is a maximum length to a filename or something in rename because the rename is failing. I try it on a file representing the first 8 digits of the hash... same directory... same permissions and it works, but will not work on the full hash. Any ideas?

Link to comment
Share on other sites

I'm completely baffled... here's the filename:

 

This fails:

rename('./upload/5b4875453cbcb59713701e17e495de4c', './upload/32876fc3df25fb68fb9e398b5d7d7977');

 

This works:

rename('./upload/5b487545', './upload/32876fc3');

 

Certainly less than the 250ish characters I've been seeing as an OS limit.

Link to comment
Share on other sites

Warning: rename(./upload/5b4875453cbcb59713701e17e495de4c,./upload/32876fc3df25fb68fb9e398b5d7d7977) [function.rename]: No such file or directory in /home2/firebees/public_html/cmsbackend2/rename.php on line 4

 

The file exists though... I've tested on a Linux development box and production server... same results.

Link to comment
Share on other sites

Weird stuff, maybe php has the limit and not the OS? not sure.

 

Anyhow you could use the exec and use the Unix commands to rename the file that way. Not ideal, but it should work.

 

A side note I would do this:

 

<?php
if (file_exists('./upload/5b4875453cbcb59713701e17e495de4c')) {
    rename('./upload/5b4875453cbcb59713701e17e495de4c', './upload/32876fc3df25fb68fb9e398b5d7d7977');
}else {
    echo 'Unable to rename the dang file cause it does not exist.';
}
?>

 

On a lighter side, I am sure it is some stupid little mistep and when you figure it out you will rip your hair out at how easy it was to find.

Link to comment
Share on other sites

Yeah it's definitely a file length issue. I did a 777 on the parent directory and the file to be sure.

 

I know a workaround that doesn't involve the hash... it just kind of sucks because I was pretty pleased with this solution. Too bad I got pwnd by one of the dumbest things evah!

Link to comment
Share on other sites

/home2/firebees/public_html/cmsbackend2/upload/5b4875453cbcb59713701e17e495de4c

 

 

Is just 79 chars....  I don't see why it would have issues with that.  That's weird.

 

 

I'm guessing the problem is specific to linux.  If so, is it just the distro that you use?  Or all distros?  Never heard of that issue before.

 

 

Edit:

 

ext/stand/file.c

/* {{{ proto bool rename(string old_name, string new_name[, resource context])
   Rename a file */
PHP_FUNCTION(rename)
{
char *old_name, *new_name;
int old_name_len, new_name_len;
zval *zcontext = NULL;
php_stream_wrapper *wrapper;
php_stream_context *context;

if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|r", &old_name, &old_name_len, &new_name, &new_name_len, &zcontext) == FAILURE) {
	RETURN_FALSE;
}

wrapper = php_stream_locate_url_wrapper(old_name, NULL, 0 TSRMLS_CC);

if (!wrapper || !wrapper->wops) {
	php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to locate stream wrapper");
	RETURN_FALSE;
}

if (!wrapper->wops->rename) {
	php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s wrapper does not support renaming", wrapper->wops->label ? wrapper->wops->label : "Source");
	RETURN_FALSE;
}

if (wrapper != php_stream_locate_url_wrapper(new_name, NULL, 0 TSRMLS_CC)) {
	php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot rename a file across wrapper types");
	RETURN_FALSE;
}

context = php_stream_context_from_zval(zcontext, 0);

RETURN_BOOL(wrapper->wops->rename(wrapper, old_name, new_name, 0, context TSRMLS_CC));
}
/* }}} */

 

It looks like it just parses the args and hands it off to the system rename function, like I would expect.  I wonder where the error is with the file name length.  Weird.

Link to comment
Share on other sites

I'm thinking there is something going on with the actual characters in the file name and not the length. Try renaming your first short file name to the second long one. If that works, it would indicate that the length is not the problem. Have you tried this with different long source and destination names or just the ones you have posted?

Link to comment
Share on other sites

Nah... I'm not sure what changed but it seems to be working?

 

I essentially wrote a script that loops through all the files and renames the file adding a - counter to the end of the file.

 

ie

 

file => file-1

 

And the rename is working. This test was completely separate from the rest of the project, so perhaps I overlooked something else.... or maybe I just happened to look at it just right so it decided to work.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.