PHP文件上传和覆盖同名的文件 [英] PHP File upload and overwrite file with same name

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

问题描述



我遇到问题,因为它不允许我用文件覆盖文件。

我正在做一个应用程序,允许用户通过PHP上传文件。同名。
例如,我有一个名为text.php的文件,我现在上传它,现在当我回去改变文件text.php的内容,我再次上传到服务器上,我仍然没有编辑的版本。但是,如果我上传另一个文件的作品。所以我不能覆盖文件。

$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ //设置本地PHP变量($ _POST [greg] =='true'){
从使用数组
发送的POST变量中,$ _FILES全局变量为这个上传的文件包含的数据
$ fileName = $ _FILES [file1] [name]; //文件名
$ fileTmpLoc = $ _FILES [file1] [tmp_name]; //在PHP tmp文件夹中的文件
$ fileType = $ _FILES [file1] [type]; //文件类型是
$ fileSize = $ _FILES [file1] [size]; //以字节为单位的文件大小
$ fileErrorMsg = $ _FILES [file1] [error]; // 0为false ...和1为真

//特定错误处理如果您需要运行错误检查
if(!$ fileTmpLoc){//如果文件未选中
echo错误:在点击上传按钮之前,请浏览文件。;
exit();
} else if($ fileSize> 90000000000000){//如果文件大于我们想要允许的
echo错误:文件大小超过50kb。
unlink($ fileTmpLoc);
exit();
} else if(!preg_match(/。(doc | docx | xls)$ / i,$ fileName)){
//这个条件只有当你希望允许上传特定的文件类型
回声错误:您的文件不是正确的格式联系页面的主人澄清。;
unlink($ fileTmpLoc);
exit();

//使用move_uploaded_file()函数
move_uploaded_file($ fileTmpLoc,documenti / $ fileName)将其放到上传文件夹中。
//检查以确保上传的文件在你想要的地方
if(!file_exists(documenti / $ fileName)){
echoERROR:File not uploaded< br />< br />;
echo检查目标上传文件夹中的文件夹权限是0755还是更宽松。< br />< br />;
echo检查您的php.ini设置是否设置为允许超过2 MB文件,默认情况下是2MB。
exit();
}
//在页面上显示内容,以便您可以查看正在进行的测试
echo名为< strong> $ fileName< / strong>的文件已经成功上传< br />< br />;
echo它是< strong> $ fileSize< / strong>字节的大小。< br />< br />
echo这是一个< strong> $ fileType< / strong>文件类型< br />< br />
echo这个上传的错误信息输出是:< br /> $ fileErrorMsg;






如何更改此代码,以便当我上传用相同的名字覆盖现有的文件?

解决方案

试试这个(把它放在上传文件之前)



$ p $ //检查文件exsists
if(file_exists(documenti / $ fileName))unlink(documenti / $ fileName );

//使用move_uploaded_file()函数
move_uploaded_file($ fileTmpLoc,documenti / $ fileName)将其放到上传文件夹中。


I am making an application that allows users to upload a file in a directory via PHP.

I am having problems because it dose not allow me to overwrite files with the same name. Eg I have a file called text.php and I upload it, now when I go back and change the content of file text.php and I upload it again on the server I still have the version without the edits. However if I upload another file it works. So I just can't overwrite files.

if ($_POST["greg"]=='true'){
// Set local PHP vars from the POST vars sent from our form using the array
// of data that the $_FILES global variable contains for this uploaded file
$fileName = $_FILES["file1"]["name"]; // The file name
$fileTmpLoc = $_FILES["file1"]["tmp_name"]; // File in the PHP tmp folder
$fileType = $_FILES["file1"]["type"]; // The type of file it is
$fileSize = $_FILES["file1"]["size"]; // File size in bytes
$fileErrorMsg = $_FILES["file1"]["error"]; // 0 for false... and 1 for true

// Specific Error Handling if you need to run error checking
if (!$fileTmpLoc) { // if file not chosen
    echo "ERROR: Please browse for a file before clicking the upload button.";
    exit();
} else if($fileSize > 90000000000000) { // if file is larger than we want to allow
    echo "ERROR: Your file was larger than 50kb in file size.";
    unlink($fileTmpLoc);
    exit();
} else if (!preg_match("/.(doc|docx|xls)$/i", $fileName) ) {
     // This condition is only if you wish to allow uploading of specific file types    
     echo "ERROR: Your file is not the right format contact the master of the page for clarification.";
     unlink($fileTmpLoc);
     exit();
}
// Place it into your "uploads" folder mow using the move_uploaded_file() function
move_uploaded_file($fileTmpLoc, "documenti/$fileName");
// Check to make sure the uploaded file is in place where you want it
if (!file_exists("documenti/$fileName")) {
    echo "ERROR: File not uploaded<br /><br />";
    echo "Check folder permissions on the target uploads folder is 0755 or looser.<br /><br />";
    echo "Check that your php.ini settings are set to allow over 2 MB files, they are 2MB by default.";
    exit();
}
// Display things to the page so you can see what is happening for testing purposes
echo "The file named <strong>$fileName</strong> uploaded successfuly.<br /><br />";
echo "It is <strong>$fileSize</strong> bytes in size.<br /><br />";
echo "It is a <strong>$fileType</strong> type of file.<br /><br />";
echo "The Error Message output for this upload is: <br />$fileErrorMsg";

}

How can I change this code so that when I upload a file with the same name it overwrites the existing file?

解决方案

Try this (put it before upload a file)

//checking if file exsists
if(file_exists("documenti/$fileName")) unlink("documenti/$fileName");

//Place it into your "uploads" folder mow using the move_uploaded_file() function
move_uploaded_file($fileTmpLoc, "documenti/$fileName");

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

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