[英]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.");
}
}
?>
嗨,上面是我的php脚本的一部分,$ target_file假设引用了一个选择上传到服务器的文件,但它始终引用“ uploads /”目录。
由于$ target_file引用“ uploads /”,无论我选择上传哪个文件,它都会不断生成错误消息,表明服务器上已存在该文件。
该脚本是从w3school网页上完全复制的,我正在使用chrome浏览器检查该脚本是否正常工作。
有人可以帮我解决这个简单的错误吗? 如果可以在选择要上传的文件之后引用$ target_file,则最有可能解决我的问题。
谢谢。
我一直在寻找和解决错误的方法,因此我决定从头开始重新编写脚本,新脚本可以像编码所要求的那样完美地工作。
但是,我仍然不知道上面的脚本出了什么问题。 如果以后有人从我的原始脚本中捕获了任何错误,请随时给我评论。
这是我新创建的用于上传的php脚本。
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.