Move file to other directory

OK, I am trying to move an image file from one directory to another. I am using the code below


	$rename1 = "SpecialPictures/Advertisers/CampBC.png";
	$rename2 = "SpecialPictures/Advertisers/OLDBusinessCards/CampBC.png"];

but get the following error:

Warning: rename(SpecialPictures/Advertisers/CampBC.png,SpecialPictures/Advertisers/OLDBusinessCards/CampBC.png) [<a href=‘function.rename’>function.rename</a>]: The system cannot find the path specified. (code: 3) in SACCC567\View.php on line 84

All the directories exist.

and line 84 is:


		rename($rename1, $rename2);

So, what is wrong?

Also, I am using PHP 5.3.13

Thanks
E

The $rename2 has a trailing square bracket. Could that be the problem?

I tried the following code and it worked the first time then failed because the file had been moved.

Try this:

Using: PHP 5.3.13


    error_reporting(-1);
    ini_set('display_errors',1);


     $rename1 = "SpecialPictures/Advertisers/CampBC.png"; 
     $rename2 = "SpecialPictures/Advertisers/OLDBusinessCards/CampBC.png";  


    $msg = 'No file: ' .$rename1;
    if(file_exists($rename1))
    {
           $ok  = rename($rename1, $rename2);  


       $msg = 'Failed to rename???';
       if($ok)
       {
          $msg = 'Renamed ' .$rename1 .' to ' .$rename2;
       }
    }
    echo $msg;

If you’re using this code:


    $rename1 = "SpecialPictures/Advertisers/CampBC.png"; 
    $rename2 = "SpecialPictures/Advertisers/OLDBusinessCards/CampBC.png"];  
    rename($rename1, $rename2);  

Like John said, there is a “]” at the end of the 2nd line, which is weird. I’m not sure PHP would even want to compile this but anyway, if it’s there in your script remove it :wink:
Then, “CampBC.png” is the actual picture that you want to move into “OLDBusinessCards”. Is it correct?
There’s a lot of Caps here and there, are you sure you got that right? (if you’re on Linux or a Mac. It should work on Windows)
The directory SpecialPictures should exists at the same place as the executed PHP file is (if I’m not mistaken), is it the case? Did you try to specify the entire path like “/var/www/…/SpecialPictures/etc…”?

If nothing works, copy the file “CampBC.png” into your Advertisers folder and the OLDBusinessCards folder manually. Then, try:

$rename1 = "SpecialPictures/Advertisers/CampBC.png"; 
$rename2 = "SpecialPictures/Advertisers/OLDBusinessCards/CampBC.png";  
var_dump(file_exists ( $rename1 ));
var_dump(file_exists ( $rename2 ));

What do you get?

Xmog, I can see that the file exists in one of the directories.

However, if you have to go up one or more directories, then the error is again triggered. Any idea why? Any workaround suggestions?

Try putting in “…/…/SpecialPictures…” and see what you get if the php script is down a couple of directories.

Thanks

OK, I guess no one understands my dilemma or I just described it rather poorly - which is usually the case.

Here is what I am trying to do:
1 - the rename script is in a folder down from the root of my site. ie, root->folder1->folder2/myrename.php
2 - pictures/files to be renamed are in a different folder down from root. i3 root->pic1->pic2/picture.jpg
3 - running myrename.php has to go back up to the root then down the picture folder tree to find the picture and then rename it in a folder down 1 more level in the picture tree. ie, root->pic1->pic2->renamefolder/picture.jpg.

So, picture.jpg is the file I want to move using myrename.php script. However, if I have to traverse back up the folder tree to go to the picture.jpg down yet another folder tree, the error I have gets displayed and the file never gets moved.

Anyone understand and/or have some “fix” for this? Workaround? whatever?

Thanks
e

Fred (I can call you Fred?), did you try just specifying the entire path (aka the absolute path)? Like “/var/www/html/…etc” ?

To know the path, you could put a file “test.php” right in your images path and call it.
Put this code in it:

<?php echo dirname(__FILE__); ?>

Then, declare 2 constants like:

define("IMAGE_FOLDER",     "/var/www/html/images");
define("ARCHIVE_IMAGE_FOLDER",    "/var/www/html/old_images");

Instead of playing with a lot of “…”, it should be easier :wink:

xMog,

Sure, call me fred.

I am trying this on my local development machine for now.

I tried your tip but still get the error “Warning: rename(C:\wamp\www\root\SpecialPictures\Advertisers\CampBC.png,C:\wamp\www\root\SpecialPictures\Advertisers\AdvertisersOLDBusinessCards\CampBC.png): The system cannot find the path specified. (code: 3) in C:\wamp\www\root\admin\ 3st\ViewerPDO3.php on line 100”

You should notice that the pictures file is a different branch off of the root than the php script. So, not sure why rename function will not find the path - it does exist.

E

Maybe a security problem? The error message doesn’t suggest this, but who knows. I don’t know how WAMP setup its stuff :expressionless:

Did you try your code in the same “directory” as your PHP script?

I updated the original script slightly (Post #2) - try this:


error_reporting(-1);
ini_set('display_errors',1);




/* Original rename images */
if(0)
{
 $rename1 = "SpecialPictures/Advertisers/CampBC.png"; 
 $rename2 = "SpecialPictures/Advertisers/OLDBusinessCards/CampBC.png";  
}else{
 $rename1 = 'c:\\wamp\\www\\root\\SpecialPictures\\Advertisers\\CampBC.png'; 
 $rename2 = 'c:\\wamp\\www\\root\\SpecialPictures\\Advertisers\\AdvertisersOLDBusinessCards\\CampBC.png';  
}


$msg = 'File does not exist: ' .$rename1;
if(file_exists($rename1))
{
  $ok = createPathIfAndOnlyIfItDoesNotExist($rename2);
  if($ok)
  {
    $ok  = rename($rename1, $rename2);  


    $msg = 'Failed to rename???';
    if($ok)
    {
       $msg = 'Renamed ' .$rename1 .'<br />> to <br />' .$rename2;
    }
  }
}


echo $msg;


function createPathIfAndOnlyIfItDoesNotExist($image)      
{
  $result = true; /* PATH EXISTS */


  $path = dirname($image);
  if ( ! is_dir($path) )
  {
    $result = mkdir($path, 0777, TRUE);


    $msg = 'Failed to create: ' .$path;
    if($result)
    {
      $msg = 'Just Created New $path: ' . $path;
    }
    echo '<br />' .$msg .'<br />';
  } 


  return $result;
}


?>    


Thanks and this seems to work.

Now could you explain what you did for my education?

e

Hi Fred,

I have revised the script and the [b] function moveImageFile($src, $dst) [/b]could be stored in a common library file:

Also added some messages, basically the script:

  1. ensure the $src_path exists
  2. ensure the $src_image exists
  3. ensure the $dst_path exists
  4. ensure the $dst_image exists
  5. ensure the move was successful
  6. if DEVELOPER mode is set then display progress messages.


<?php 
/* BEWARE: if TRUE THEN SHOW ALL PROGRESS MESSAGES */
   defined('DEVELOPER') ?: define('DEVELOPER', TRUE);




/* Note:  If and only if DEVELOPER mode, always display errors on the screen */
  error_reporting(0); 
  ini_set('display_errors', FALSE);
  if(DEVELOPER)
  {
    error_reporting(-1); 
    ini_set('display_errors',1);
  }




  /* Use paths specied in your script */
  /* Original rename images - post #1 */ 
  $rename1 = "SpecialPictures/Advertisers/CampBC.png"; 
  $rename2 = "SpecialPictures/Advertisers/OLDBusinessCards/CampBC.png";  
  if(TRUE)
  {
   /* post #7 */
   $rename1 = 'c:\\wamp\\www\\root\\SpecialPictures\\Advertisers\\CampBC.png'; 
   $rename2 = 'C:\\wamp\\www\\root\\SpecialPictures\\Advertisers\\AdvertisersOLDBusinessCards\\CampBC.png';  
  }






/* Call function to rename image files */ 
  $msg = '<br />FAILED TO MOVE IMAGE: ' .$rename1;
  if(  moveImageFile($src=$rename1, $dst=$rename2) )
  {
    $msg = '<br />Successfully moved $image file; '. $src;
  }
  echo $msg;








//========================
// could be stored in a common library file
//========================
function moveImageFile($src, $dst)
{
  $result = FALSE; /* Failed to Move $src image file  */




  /* Note: 1 - common function to create either or both $src and $dst image paths */
  //==================================================
  function createPathIfAndOnlyIfItDoesNotExist($image)      
  {
    $result = FALSE; /* PATH DOES NOT EXIST */




    $path   = dirname($image);




    $result = is_dir($path);
    if ( ! $result)
    {
      /* @ suppress warning if path exists */
      $result = @mkdir($path, 0777, TRUE);




      $msg = 'Failed to create: ' .$path;
      if($result)
      {
        $msg = 'Just Created New $path: ' . $path;
      }
      echo DEVELOPER ? '<br />'.$msg.'<br />' : NULL;
    } 

    return $result;
  }//endfunc






    /* Note: 2 - Test Source image path and create if it does not exist */
    $result = createPathIfAndOnlyIfItDoesNotExist($src);
    if($result)
    {
      /* Note: 3 - Create default message */
      $msg = '<br />File does not exist: ' .$src;
      $result = file_exists($src);
      if($result)
      {
        /* Note: 4 - Test Destination image path and create if it does not exist */
        $result = createPathIfAndOnlyIfItDoesNotExist($dst);
        if($result)
        {


          /* Note: 5 - Test rename() result */
          $result = rename($src, $dst);  




          /* Note: 6 - Set default msg */
          $msg = 'Failed to rename???';
          if($result)
          {
            $msg = '<br />Success</b><br />Renamed ' .$src .'<br /> to <br />' .$dst;
          }
        }
      }
      /* Note: 7 - Display Success or failure result */
      echo DEVELOPER ? $msg : NULL;
    }


  return $result;  
}// endfunc moveImageFile


?>