1

I have searched far and wide on this one, but haven't really found a solution.

Got a client that wants music on their site (yea yea, I know..). The flash player grabs the single file called song.mp3 and plays it.

Well, I am trying to get functionality as to be able to have the client upload their own new song if they ever want to change it.

So basically, the script needs to allow them to upload the file, THEN overwrite the old file with the new one. Basically, making sure the filename of song.mp3 stays intact.

I am thinking I will need to use PHP to 1) upload the file 2) delete the original song.mp3 3) rename the new file upload to song.mp3

Does that seem right? Or is there a simpler way of doing this? Thanks in advance!


EDIT: I impimented UPLOADIFY and am able to use

'onAllComplete' : function(event,data) {
      alert(data.filesUploaded + ' files uploaded successfully!');
    }

I am just not sure how to point THAT to a PHP file....

 'onAllComplete' : function() {
      'aphpfile.php'
    }

???? lol

PaulHanak
  • 729
  • 2
  • 9
  • 21
  • DON'T PUT MUSIC ON A WEBSITE :) With that said, what have you tried? – Paul Dessert Jan 19 '12 at 01:07
  • 1
    Use `move_uploaded_file`. The upload will overwrite existing filenames; all 3 of your steps in 1. – Josh Jan 19 '12 at 01:08
  • 1
    Josh's statement is valid, that will def be a function you will need to use. I am still not entirely sure what the question is. Do you need to use UPLOADIFY? – malonso Jan 19 '12 at 01:18
  • can't you just add a redirect to the complete function? 'window.location = "aphpfile.php";' – Silvertiger Jan 19 '12 at 01:30

2 Answers2

5

a standard form will suffice for the upload just remember to include the mime in the form. then you can use $_FILES[''] to reference the file.

then you can check for the filename provided and see if it exists in the file system using file_exists() check for the file name OR if you don't need to keep the old file, you can use perform the file move and overwrite the old one with the new from the temporary directory

<?PHP
// this assumes that the upload form calls the form file field "myupload"
$name  = $_FILES['myupload']['name'];
$type  = $_FILES['myupload']['type'];
$size  = $_FILES['myupload']['size'];
$tmp   = $_FILES['myupload']['tmp_name'];
$error = $_FILES['myupload']['error'];
$savepath = '/yourserverpath/';
$filelocation = $svaepath.$name.".".$type;
// This won't upload if there was an error or if the file exists, hence the check
if (!file_exists($filelocation) && $error == 0) {
    // echo "The file $filename exists";
    // This will overwrite even if the file exists
    move_uploaded_file($tmp, $filelocation);
}
// OR just leave out the "file_exists()" and check for the error,
// an if statement either way

?>
Silvertiger
  • 1,680
  • 2
  • 19
  • 32
  • This is excellent. Thank you so much. Love it. The only real reason I decided to use "uploadify" is because these MP3s are rather large and a progress bar would be very beneficial to the client, rather than sitting there waiting... BUT there comes a time when "time is money" :) – PaulHanak Jan 19 '12 at 02:00
0

try this piece of code for upload and replace file

if(file_exists($newfilename)){
        unlink($newfilename);
    }

 move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $newfilename); 
Mani
  • 3,394
  • 3
  • 30
  • 36