Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 7548
  • Last Modified:

how to move a file/photo to an folder using php?

Hi experts,

how to move a file to  a new folder using php?

thx.
0
tjome
Asked:
tjome
1 Solution
 
Loganathan NatarajanLAMP DeveloperCommented:
0
 
Marco GasiFreelancerCommented:
You have to do it in two steps: first you have to copy the file using copy function , then you have to delete the source file using unlink function:
$source = "path_to_file/image.jpg";
$dest = "new_path_to_file/image.jpg";
copy($source, $dest);
unlink($source);

Open in new window

Please, be sure the new destination directory exist and be sure to have write permission on it.

Cheers
1
 
stevejacob68Commented:
Hi,

This code will move file to another folder in php:

if(isset($_POST['next_page'])) {
  if (!is_dir('../images/uploads/listers/'.$_SESSION['loggedin_lister_id'])) {
    mkdir('../images/uploads/listers/'.$_SESSION['loggedin_lister_id']);
  }

  foreach($_SESSION['uploaded_photos'] as $key => $value) {
    $target_path = '../images/uploads/listers/'.$_SESSION['loggedin_lister_id'].'/';
    $target_path = $target_path . basename($value);

    if(rename($value, $target_path)) {
      echo "The file ".  basename($value). " has been uploaded<br />";
    } else{
      echo "There was an error uploading the file, please try again!";
    }

  } //end foreach

}
0
 
tjomeAuthor Commented:
thank you  for your help
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now