PHP-上传并覆盖文件(或上传并重命名)?

PHP-上传并覆盖文件(或上传并重命名)?,php,upload,Php,Upload,我已经在这方面做了广泛的研究,但还没有真正找到解决办法 有一个客户希望在他们的网站上播放音乐(是的,我知道…)。flash播放器抓取名为song.mp3的单个文件并播放它 嗯,我试图获得功能,以便能够让客户端上传自己的新歌,如果他们想改变它 所以基本上,脚本需要允许他们上传文件,然后用新文件覆盖旧文件。基本上,确保song.mp3的文件名保持不变 我想我需要使用PHP来 1) 上传文件 2) 删除原始歌曲。mp3 3) 将新文件重命名为song.mp3 这看起来对吗?还是有更简单的方法?提前谢谢

我已经在这方面做了广泛的研究,但还没有真正找到解决办法

有一个客户希望在他们的网站上播放音乐(是的,我知道…)。flash播放器抓取名为song.mp3的单个文件并播放它

嗯,我试图获得功能,以便能够让客户端上传自己的新歌,如果他们想改变它

所以基本上,脚本需要允许他们上传文件,然后用新文件覆盖旧文件。基本上,确保song.mp3的文件名保持不变

我想我需要使用PHP来 1) 上传文件 2) 删除原始歌曲。mp3 3) 将新文件重命名为song.mp3

这看起来对吗?还是有更简单的方法?提前谢谢


编辑:我导入UPLOADIFY并能够使用

'onAllComplete' : function(event,data) {
      alert(data.filesUploaded + ' files uploaded successfully!');
    }
我只是不知道如何指向PHP文件

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

????lol

标准表单就足够上传了,只需记住在表单中包含mime即可。然后您可以使用$_FILES['']引用该文件

然后,您可以使用file_exists()检查提供的文件名,并查看它是否存在于文件系统中检查文件名,或者如果不需要保留旧文件,您可以使用执行文件移动并使用临时目录中的新文件覆盖旧文件

<?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

?>

一个标准的表单就足够上传了,记住在表单中包含mime。然后您可以使用$_FILES['']引用该文件

然后,您可以使用file_exists()检查提供的文件名,并查看它是否存在于文件系统中检查文件名,或者如果不需要保留旧文件,您可以使用执行文件移动并使用临时目录中的新文件覆盖旧文件

<?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

?>

尝试这段代码以上载和替换文件

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

 move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $newfilename); 

请尝试这段代码以上载和替换文件

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

 move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $newfilename); 

不要在网站上放音乐:)这么说,你试过什么?使用
move\u upload\u file
。上载将覆盖现有文件名;Josh的陈述中的所有3个步骤都是有效的,这将是您需要使用的函数。我仍然不能完全确定问题是什么。你需要使用UPLOADIFY吗?你不能只添加一个重定向到完整的函数吗window.location=“aphpfile.php”;'不要在网站上放音乐:)这么说,你试过什么?使用
move\u upload\u file
。上载将覆盖现有文件名;Josh的陈述中的所有3个步骤都是有效的,这将是您需要使用的函数。我仍然不能完全确定问题是什么。你需要使用UPLOADIFY吗?你不能只添加一个重定向到完整的函数吗window.location=“aphpfile.php”;'这太棒了。非常感谢你。我喜欢。我决定使用“uploadify”的唯一真正原因是因为这些MP3相当大,进度条对客户端非常有益,而不是坐在那里等待。。。但总有一天“时间就是金钱”:)这太棒了。非常感谢你。我喜欢。我决定使用“uploadify”的唯一真正原因是因为这些MP3相当大,进度条对客户端非常有益,而不是坐在那里等待。。。但总有一天,“时间就是金钱”: