Solved

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

Posted on 2016-07-15
4
47 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 52

Assisted Solution

by:Scott Fell, EE MVE
Scott Fell,  EE MVE earned 150 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 300 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 52

Assisted Solution

by:Julian Hansen
Julian Hansen earned 50 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

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

These days socially coordinated efforts have turned into a critical requirement for enterprises.
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…
The viewer will learn how to dynamically set the form action using jQuery.
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…

861 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