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

Posted on 2014-07-22
Last Modified: 2014-07-22
Hi experts,

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

Question by:tjome
LVL 36

Expert Comment

by:Loganathan Natarajan
ID: 40210978
LVL 31

Accepted Solution

Marco Gasi earned 500 total points
ID: 40210986
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);

Open in new window

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


Expert Comment

ID: 40210993

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'])) {

  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


Author Closing Comment

ID: 40211153
thank you  for your help

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now