Solved

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

Posted on 2016-07-15
4
42 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 21

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 51

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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.

746 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

11 Experts available now in Live!

Get 1:1 Help Now