PHP - 上传并覆盖文件(或上传并重命名)? [英] PHP - upload and overwrite a file (or upload and rename it)?

查看:63
本文介绍了PHP - 上传并覆盖文件(或上传并重命名)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在这个问题上进行了广泛的搜索,但还没有真正找到解决方案.

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

有一个客户想要在他们的网站上播放音乐(是的,我知道..).Flash 播放器抓取名为 song.mp3 的单个文件并播放.

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.

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

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.

我想我需要使用 PHP 来1)上传文件2)删除原来的song.mp33) 将上传的新文件重命名为song.mp3

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!

我使用了 UPLOADIFY 并且能够使用

I impimented UPLOADIFY and am able to use

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

我只是不确定如何将其指向 PHP 文件....

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

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

????哈哈

推荐答案

一个标准的表单就足以上传,只需记住在表单中包含 mime.然后你可以使用 $_FILES[''] 来引用文件.

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.

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

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

?>

这篇关于PHP - 上传并覆盖文件(或上传并重命名)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆