Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How can I use PHP to move files from one directory to another.

Posted on 2016-07-15
4
Medium Priority
?
80 Views
Last Modified: 2016-07-17
I've got a database with a life of mp3 file names. I want to go through that database, pick the mp3 file from the directory it resides in and move it to another directory.

I've seen PHP move_uploaded_file, but I can't tell where the starting directory is and where the directory is that you're moving things to.

Check it out:

Here's my select statement:

$jorja="select * from mp3_fix order by id";
$jorja_query="mysqli_query($cxn, $jorja);
while($jorja_row=mysqli_fetch_assoc($jorja_query))
{
...right here. What do I do?

I've seen this:

$uploads_dir = '/mp3_folder';
foreach ($_FILES["pictures"]["error"] as $key => $error) {
    if ($error == UPLOAD_ERR_OK) {
        $tmp_name = $_FILES["pictures"]["tmp_name"][$key];
        $name = $_FILES["pictures"]["name"][$key];
        move_uploaded_file($tmp_name, "$uploads_dir/$name");
    }
}

...but I couldn't tell where I would put my original directory (mp3) and the directory where I'm moving things to (mp3_fix).

How do I pull this off?
0
Comment
Question by:brucegust
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 53

Assisted Solution

by:Scott Fell, EE MVE
Scott Fell,  EE MVE earned 600 total points
ID: 41713764
In this case, your files are already on the server.  Can you just use copy?  http://php.net/manual/en/function.copy.php

<?php
$file = 'example.txt';
$newfile = 'example.txt.bak';

if (!copy($file, $newfile)) {
    echo "failed to copy $file...\n";
}
?>

Open in new window

0
 
LVL 22

Accepted Solution

by:
Kim Walker earned 1200 total points
ID: 41713786
Moving files in PHP is done with the rename function.
rename("/path1/filename.ext","/path2/filename.ext");

Open in new window

1
 
LVL 59

Assisted Solution

by:Julian Hansen
Julian Hansen earned 200 total points
ID: 41714475
Any particular reason you want to do this with PHP?
Is this a once off process or do you need to repeat it
What does your target folder look like - all MP3's in one folder all in sub folders?
What OS are you targeting?

The reason I ask is we do this quite often using various approaches including create batch files in Excel.
0
 

Author Closing Comment

by:brucegust
ID: 41716187
Thanks, fellas!
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to count occurrences of each item in an array.
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…

688 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