Solved

rename function does't work!

Posted on 2008-06-19
5
203 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

932 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

8 Experts available now in Live!

Get 1:1 Help Now