How do you rename a file in PHP?
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?
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!!
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.
All the best from the United Kingdom.
Re: How do you rename a file in PHP?
Windows uses backslashes instead of forward slashes like *nix systems do. DIRECTORY_SEPARATOR is a constant mapped to the OS's, well, directory separator.
becomes
You can, of course, define DIRECTORY_SEPARATOR to something less obnoxious to type, like DS
Code: Select all
$image_dir = dirname(__DIR__) . '/images/productphotos/';Code: Select all
$image_dir = dirname(__DIR__) . DIRECTORY_SEPARATOR . 'images' . DIRECTORY_SEPARATOR . 'productphotos' . DIRECTORY_SEPARATOR;-
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?
So "DIRECTORY_SEPARATOR" kind of let's the OS choose the appropriate / or \ itself.
Either way it's still not working
[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 !?!!?
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";
}
}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.
All the best from the United Kingdom.
Re: How do you rename a file in PHP?
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?
Sorry I am not with you.
The filename is there in the rename() part. It's not needed in the dirname segment is it?
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.
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?
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.
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.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
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?
What about renaming files in two locations?
This isn't working.
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);Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
All the best from the United Kingdom.
Re: How do you rename a file in PHP?
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?
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?
I tried this, but it doesn't work at all.
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";
}
}Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
All the best from the United Kingdom.
Re: How do you rename a file in PHP?
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.
That's not exactly helpful. What happens? What have you checked? Are we back to having missing filenames?it doesn't work at all.
-
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?
No sorry i don't think I am being clear enough.
This defines the image directory, based on the $image_dir variable for:
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;
??
[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]
Code: Select all
define('IMAGE_DIR', $image_dir);Code: Select all
$image_dir = dirname(__DIR__) . DIRECTORY_SEPARATOR . 'images' . DIRECTORY_SEPARATOR . 'productphotos' . DIRECTORY_SEPARATOR;$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";
}
}
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.
All the best from the United Kingdom.
Re: How do you rename a file in PHP?
simonmlewis wrote:No sorry i don't think I am being clear enough.This defines the image directory, based on the $image_dir variable for:Code: Select all
define('IMAGE_DIR', $image_dir);So how do I use IMAGE_DIR for a second directory with the same filename (thumbnails)Code: Select all
$image_dir = dirname(__DIR__) . DIRECTORY_SEPARATOR . 'images' . DIRECTORY_SEPARATOR . 'productphotos' . DIRECTORY_SEPARATOR;
Code: Select all
$small = IMAGE_DIR . DIRECTORY_SEPARATOR . 'small';Re: How do you rename a file in PHP?
And we're back to missing file names.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]
-
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?
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 ??
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.
All the best from the United Kingdom.
Re: How do you rename a file in PHP?
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";
}
}