[英]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.