Page 2 of 3

Re: How do you rename a file in PHP?

Posted: Fri Jul 10, 2015 12:40 pm
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!!

Re: How do you rename a file in PHP?

Posted: Fri Jul 10, 2015 12:45 pm
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

Re: How do you rename a file in PHP?

Posted: Fri Jul 10, 2015 12:48 pm
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 !?!!?

Re: How do you rename a file in PHP?

Posted: Fri Jul 10, 2015 12:52 pm
by Celauran
There's still no file specified in your first argument.

Re: How do you rename a file in PHP?

Posted: Fri Jul 10, 2015 12:58 pm
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?

Re: How do you rename a file in PHP?

Posted: Fri Jul 10, 2015 1:14 pm
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.

Re: How do you rename a file in PHP?

Posted: Tue Jul 14, 2015 3:49 am
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.

Re: How do you rename a file in PHP?

Posted: Tue Jul 14, 2015 6:12 am
by Celauran
You've got two new path variables, neither of which you're using, and you're only calling rename() once.

Re: How do you rename a file in PHP?

Posted: Tue Jul 14, 2015 6:31 am
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.

Re: How do you rename a file in PHP?

Posted: Tue Jul 14, 2015 6:36 am
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?

Re: How do you rename a file in PHP?

Posted: Tue Jul 14, 2015 6:50 am
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]

Re: How do you rename a file in PHP?

Posted: Tue Jul 14, 2015 7:25 am
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';

Re: How do you rename a file in PHP?

Posted: Tue Jul 14, 2015 7:26 am
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.

Re: How do you rename a file in PHP?

Posted: Tue Jul 14, 2015 7:33 am
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 ??

Re: How do you rename a file in PHP?

Posted: Tue Jul 14, 2015 7:41 am
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";
    }
}