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

x
?
Solved

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

Posted on 2016-07-15
4
Medium Priority
?
86 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
4 Comments
 
LVL 54

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 60

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
It’s a season to be thankful, and we’re thankful for users like you who engage on site, solve technology problems, and network with others in the industry. What tech are we most thankful for? Keep reading.
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

926 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