How do you rename a file in PHP?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: How do you rename a file in PHP?

Post by simonmlewis »

Sorry.... where? It doesn't go in $image_dir....?
And I don't know wht a "directory_separator" is.

This "file renaming" is a whole new thing for me. As you can likely tell!!
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: How do you rename a file in PHP?

Post by Celauran »

Windows uses backslashes instead of forward slashes like *nix systems do. DIRECTORY_SEPARATOR is a constant mapped to the OS's, well, directory separator.

Code: Select all

$image_dir = dirname(__DIR__) . '/images/productphotos/';
becomes

Code: Select all

$image_dir = dirname(__DIR__) . DIRECTORY_SEPARATOR . 'images' . DIRECTORY_SEPARATOR . 'productphotos' . DIRECTORY_SEPARATOR;
You can, of course, define DIRECTORY_SEPARATOR to something less obnoxious to type, like DS
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: How do you rename a file in PHP?

Post by simonmlewis »

So "DIRECTORY_SEPARATOR" kind of let's the OS choose the appropriate / or \ itself.

Either way it's still not working

Code: Select all

$photoprimaryname = isset($_POST['photoprimaryname']) ? $_POST['photoprimaryname'] : null;
$photoprimarynewname = isset($_POST['photoprimarynewname']) ? $_POST['photoprimarynewname'] : null;
if (isset($photoprimarynewname))
{
$image_dir = dirname(__DIR__) . DIRECTORY_SEPARATOR . 'images' . DIRECTORY_SEPARATOR . 'productphotos' . DIRECTORY_SEPARATOR;
define('IMAGE_DIR', $image_dir);

mysql_query ("UPDATE products SET photoprimary = '$photoprimarynewname' WHERE id = '$id'");

$renameResult = rename(IMAGE_DIR . $photoprimaryname, IMAGE_DIR . $photoprimarynewname);
// Evaluate the value returned from the function if needed
if ($renameResult == true) {
    echo $photoprimaryname . " is now named " . $photoprimarynewname;
} else {
     echo "Could not rename that file";
}
}
[text]Warning: rename(C:\xampp\phpmyadmin\site\images\productphotos\,C:\xampp\phpmyadmin\site\images\productphotos\7461658.jpg): The process cannot access the file because it is being used by another process. (code: 32) in C:\xampp\phpmyadmin\site\includes\a_productedit.inc on line 218
Could not rename that file [/text]

I don't understand "is being used by another process"... when it is THIS process that is trying to use it !?!!?
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: How do you rename a file in PHP?

Post by Celauran »

There's still no file specified in your first argument.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: How do you rename a file in PHP?

Post by simonmlewis »

Sorry I am not with you.

The filename is there in the rename() part. It's not needed in the dirname segment is it?
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: How do you rename a file in PHP?

Post by simonmlewis »

Bingo I see it.
The problem was, in the FORM, I am showing the old and new filenames and passing them thru. But didn't want the old one to be renamed so "disable" it. So it didn't pass thru!! I've used read only now.
I could always just hide the USED field for the old file.

Works tho. :) Thanks for all your help.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: How do you rename a file in PHP?

Post by simonmlewis »

What about renaming files in two locations?

Code: Select all

$image_dir = dirname(__DIR__) . DIRECTORY_SEPARATOR . 'images' . DIRECTORY_SEPARATOR . 'productphotos' . DIRECTORY_SEPARATOR;
define('IMAGE_DIR', $image_dir);

$image_dirsmall = dirname(__DIR__) . DIRECTORY_SEPARATOR . 'images' . DIRECTORY_SEPARATOR . 'productphotos' . DIRECTORY_SEPARATOR . 'small' . DIRECTORY_SEPARATOR;
$renameResult = rename(IMAGE_DIR . $photoprimaryname, IMAGE_DIR . $photoprimarynewname);
This isn't working.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: How do you rename a file in PHP?

Post by Celauran »

You've got two new path variables, neither of which you're using, and you're only calling rename() once.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: How do you rename a file in PHP?

Post by simonmlewis »

Sure I get that, and I've been modifying, but the define section can only be used once because of the $image_dir variable name.
So how do I use that for two folder areas?

Code: Select all

if (isset($photoprimaryname))
{
$image_dir = dirname(__DIR__) . DIRECTORY_SEPARATOR . 'images' . DIRECTORY_SEPARATOR . 'productphotos' . DIRECTORY_SEPARATOR;
define('IMAGE_DIR', $image_dir);

$image_dirsmall = dirname(__DIR__) . DIRECTORY_SEPARATOR . 'images' . DIRECTORY_SEPARATOR . 'productphotos' . DIRECTORY_SEPARATOR . 'small' . DIRECTORY_SEPARATOR;
define('IMAGE_DIRSMALL', $image_dirsmall);

$renameResult = rename(IMAGE_DIR . $photoprimaryname, IMAGE_DIR . $photoprimarynewname);
$renameResult2 = rename(IMAGE_DIRSMALL . $photoprimaryname, IMAGE_DIRSMALL . $photoprimarynewname);

// Evaluate the value returned from the function if needed
if ($renameResult == true) {
    echo $photoprimaryname . " is now named " . $photoprimarynewname;
    mysql_query ("UPDATE products SET photoprimary = '$photoprimarynewname' WHERE id = '$id'");
} else {
     echo "Could not rename that file";
}
}
I tried this, but it doesn't work at all.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: How do you rename a file in PHP?

Post by Celauran »

The point of defining the constant was to make it available throughout your application. If you want to use sub-directories of it, you can always define a common root and append subs or just ignore the define altogether.
it doesn't work at all.
That's not exactly helpful. What happens? What have you checked? Are we back to having missing filenames?
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: How do you rename a file in PHP?

Post by simonmlewis »

No sorry i don't think I am being clear enough.

Code: Select all

define('IMAGE_DIR', $image_dir);
This defines the image directory, based on the $image_dir variable for:

Code: Select all

$image_dir = dirname(__DIR__) . DIRECTORY_SEPARATOR . 'images' . DIRECTORY_SEPARATOR . 'productphotos' . DIRECTORY_SEPARATOR;
So how do I use IMAGE_DIR for a second directory with the same filename (thumbnails)
$image_dirsmall = dirname(__DIR__) . DIRECTORY_SEPARATOR . 'images' . DIRECTORY_SEPARATOR . 'productphotos' . DIRECTORY_SEPARATOR . 'small' . DIRECTORY_SEPARATOR;
??

Code: Select all

$photoprimaryname = isset($_POST['photoprimaryname']) ? $_POST['photoprimaryname'] : null;
$photoprimarynewname = isset($_POST['photoprimarynewname']) ? $_POST['photoprimarynewname'] : null;
if (isset($photoprimaryname))
{
$image_dir = dirname(__DIR__) . DIRECTORY_SEPARATOR . 'images' . DIRECTORY_SEPARATOR . 'productphotos' . DIRECTORY_SEPARATOR;
define('IMAGE_DIR', $image_dir);

$image_dirsmall = dirname(__DIR__) . DIRECTORY_SEPARATOR . 'images' . DIRECTORY_SEPARATOR . 'productphotos' . DIRECTORY_SEPARATOR . 'small' . DIRECTORY_SEPARATOR;
define('IMAGE_DIRSMALL', $image_dirsmall);

$renameResult = rename(IMAGE_DIR . $photoprimaryname, IMAGE_DIR . $photoprimarynewname);
$renameResult2 = rename(IMAGE_DIRSMALL . $photoprimaryname, IMAGE_DIRSMALL . $photoprimarynewname);

// Evaluate the value returned from the function if needed
if ($renameResult == true) {
    echo $photoprimaryname . " is now named " . $photoprimarynewname;
    mysql_query ("UPDATE products SET photoprimary = '$photoprimarynewname' WHERE id = '$id'");
} else {
     echo "Could not rename that file";
}
}
[text]Warning: rename(C:\xampp\phpmyadmin\site\images\productphotos\9234415911.jpg,C:\xampp\phpmyadmin\site\images\productphotos\): The system cannot find the file specified. (code: 2) in C:\xampp\phpmyadmin\site\includes\a_productedit.inc on line 219

Warning: rename(C:\xampp\phpmyadmin\site\images\productphotos\small\9234415911.jpg,C:\xampp\phpmyadmin\site\images\productphotos\small\): Access is denied. (code: 5) in C:\xampp\phpmyadmin\site\includes\a_productedit.inc on line 220
Could not rename that file [/text]
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: How do you rename a file in PHP?

Post by Celauran »

simonmlewis wrote:No sorry i don't think I am being clear enough.

Code: Select all

define('IMAGE_DIR', $image_dir);
This defines the image directory, based on the $image_dir variable for:

Code: Select all

$image_dir = dirname(__DIR__) . DIRECTORY_SEPARATOR . 'images' . DIRECTORY_SEPARATOR . 'productphotos' . DIRECTORY_SEPARATOR;
So how do I use IMAGE_DIR for a second directory with the same filename (thumbnails)

Code: Select all

$small = IMAGE_DIR . DIRECTORY_SEPARATOR . 'small';
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: How do you rename a file in PHP?

Post by Celauran »

simonmlewis wrote: [text]Warning: rename(C:\xampp\phpmyadmin\site\images\productphotos\9234415911.jpg,C:\xampp\phpmyadmin\site\images\productphotos\): The system cannot find the file specified. (code: 2) in C:\xampp\phpmyadmin\site\includes\a_productedit.inc on line 219

Warning: rename(C:\xampp\phpmyadmin\site\images\productphotos\small\9234415911.jpg,C:\xampp\phpmyadmin\site\images\productphotos\small\): Access is denied. (code: 5) in C:\xampp\phpmyadmin\site\includes\a_productedit.inc on line 220
Could not rename that file [/text]
And we're back to missing file names.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: How do you rename a file in PHP?

Post by simonmlewis »

Sorry I am not quite there.
I add that line, but how do I integrate that into my code - is it like a sub section or ??
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: How do you rename a file in PHP?

Post by Celauran »

Code: Select all

<?php

define('DS', DIRECTORY_SEPARATOR);

$image_dir = dirname(__DIR__) . DS . 'images' . DS . 'productphotos' . DS;

define('IMAGE_DIR', $image_dir);
define('IMAGE_DIRSMALL', IMAGE_DIR . 'small' . DS);

$photoprimaryname = isset($_POST['photoprimaryname']) ? $_POST['photoprimaryname'] : null;
$photoprimarynewname = isset($_POST['photoprimarynewname']) ? $_POST['photoprimarynewname'] : null;

if (isset($photoprimaryname) && !empty($photoprimaryname) && isset($photoprimarynewname) && !empty($photoprimarynewname))
{
    $renameResult = rename(IMAGE_DIR . $photoprimaryname, IMAGE_DIR . $photoprimarynewname);
    $renameResult2 = rename(IMAGE_DIRSMALL . $photoprimaryname, IMAGE_DIRSMALL . $photoprimarynewname);

    // Evaluate the value returned from the function if needed
    if ($renameResult == true) {
        echo $photoprimaryname . " is now named " . $photoprimarynewname;
        mysql_query ("UPDATE products SET photoprimary = '$photoprimarynewname' WHERE id = '$id'");
    } else {
         echo "Could not rename that file";
    }
}
Post Reply