Solved

rename function does't work!

Posted on 2008-06-19
5
202 Views
Last Modified: 2009-09-23
Hello group,

I need to rename a file as soon as it is uploaded on server so I'm using rename function as following:

rename($flash_file, $_SERVER["DOCUMENT_ROOT"] . "/apps/game/flash/" . $modified_name);

What have I missed here?!

Thanks.
0
Comment
Question by:akohan
  • 3
  • 2
5 Comments
 
LVL 24

Expert Comment

by:glcummins
ID: 21825747
$new_filename = $_SERVER["DOCUMENT_ROOT"] . "/apps/game/flash/" . $modified_name;

if (file_exists($flash_file))
{
  if (is_writable($flash_file))
  {
      rename($flash_file, $new_filename);
  }
  else
  {
      die("$flash_file is not writable.");
  }
}
else
{
   die("$flash_file does not exist.");
}
0
 
LVL 24

Accepted Solution

by:
glcummins earned 125 total points
ID: 21825763
Sorry, this may be a little better:
$new_filename = $_SERVER["DOCUMENT_ROOT"] . "/apps/game/flash/" . $modified_name;
 

if (file_exists($flash_file) && is_readable($flash_file))

{

  if (is_writable($new_filename))

  {

      rename($flash_file, $new_filename);

  }

  else

  {

      die("$newFilename is not writable.");

  }

}

else

{

   die("$flash_file does not exist or is not readable.");

}

Open in new window

0
 

Author Comment

by:akohan
ID: 21826037


Yes, thank you so much. So what was wrong with calling rename() by itself? was the checking block?


Regards.
0
 
LVL 24

Expert Comment

by:glcummins
ID: 21826120
The way you posted the first time, there was no checking (at least according to the way the question was posted). This method merely makes sure that everything is in place before actually trying to run the rename function. If any one of the checks fails, the rename() function will never run.
0
 

Author Comment

by:akohan
ID: 21826797

Thanks for sharing it.

0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This article discusses how to create an extensible mechanism for linked drop downs.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now