简体   繁体   English

PHP检查上传文件是否名称已经存在

[英]php checking upload file if the name exist already

<?php
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES['fileToUpload']['name']);
$uploadOk = 1;
$FileType = pathinfo($target_file,PATHINFO_EXTENSION);

// Check if file already exists
if (file_exists($target_file)) {
echo "Sorry, $target_file already exists.";
$uploadOk = 0; 
}

//check file size
if ($uploadFile_size > 1) {
echo nl2br("sorry, exceeded file size
\r\n check your file size.
\r\n Error Code: ");
var_dump($_FILES['file']['error']); 
$uploadOk = 0;
}

// check if there is error
if ($uploadOk == 0) {
echo nl2br("sorry, no files are uploaded
\r\n check the file and try again
\r\n Error Code: ");
var_dump($_FILES['file']['error']); 

// if no error
} else { 
if (move_uploaded_file($_FILES["uploadFile"]["tmp_name"], $target_dir)) {
    echo "your ". basename( $_FILES["uploadFile"]["name"]). " file is successfully uploaded.";
    echo nl2br("\r\n thank you.");
    echo nl2br("\r\n you may close the window.");       
} 
else {
    echo  nl2br("-file upload fail- \r\n Error Code: ");
    var_dump($_FILES['file']['error']); 
    echo nl2br("\r\n \r\n please, check your file one more time.");
}
}
?>

Hi, the above is part of my php script and $target_file suppose to refer a file that is selected to be upload onto the server but it keep refers "uploads/" directory. 嗨,上面是我的php脚本的一部分,$ target_file假设引用了一个选择上传到服务器的文件,但它始终引用“ uploads /”目录。

Since $target_file is referring "uploads/" no matter which file i selected to upload, it keeps generating error that the file is already exist on the server. 由于$ target_file引用“ uploads /”,无论我选择上传哪个文件,它都会不断生成错误消息,表明服务器上已存在该文件。

The script is copied exactly same from w3school webpage and I am using chrome browser to examine if the script works correctly. 该脚本是从w3school网页上完全复制的,我正在使用chrome浏览器检查该脚本是否正常工作。

Can someone help me to fix this simple error? 有人可以帮我解决这个简单的错误吗? My problem will most likely to be solved if the $target_file can be referred after file that is selected to be uploaded. 如果可以在选择要上传的文件之后引用$ target_file,则最有可能解决我的问题。

Thank you. 谢谢。

I have been searching and studying for myself to solve the error and I decided to start over my script from the beginning and the new script work as perfectly as it is coded for. 我一直在寻找和解决错误的方法,因此我决定从头开始重新编写脚本,新脚本可以像编码所要求的那样完美地工作。

However, I still don't know what was wrong with the above script. 但是,我仍然不知道上面的脚本出了什么问题。 If anyone, later, captures any errors from my original script feel free to comment me. 如果以后有人从我的原始脚本中捕获了任何错误,请随时给我评论。

Here is my newly created php script for uploading. 这是我新创建的用于上传的php脚本。

ps Thanks to those who tried to help me to figure out the error. ps感谢那些试图帮助我找出错误的人。

<?php
$my_folder = "uploads/";
$uploadOk = 1;
$FileType = pathinfo($target_file,PATHINFO_EXTENSION);

// Check if file already exists
if (file_exists($my_folder . $_FILES['file']['name'])) {
echo 'Sorry,' . $_FILES['file']['name'] . 'already exists.';
$uploadOk = 0; 
}

//check file size
if ($_FILES['file']['size'] > 100000000) {
echo 'Sorry, your file exceeded upload size limit.  Please check your file size and try again'; 
$uploadOk = 0;
}

// check if there is error
if ($uploadOk == 0) {
echo 'NO FILES ARE UPLOADED'; 
} 
else { 
if (move_uploaded_file($_FILES['file']['tmp_name'], $my_folder . $_FILES['file']['name'])) {
    echo 'Received file' . $_FILES['file']['name'] . ' with size ' . $_FILES['file']['size'];
} else {
    echo 'Upload failed!';

    var_dump($_FILES['file']['error']);
}
}

?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM