最奇怪的事情發生在我身上,我正在開發一個簡單的文件上傳腳本,它的工作,但後來停止工作? 所以我troubleshooted和隔離問題的事實,服務器不接受一個特定的文件夾實際存在的事實。代碼的基本要點如下所示(請原諒的代碼的混亂,這只是一個粗略的草稿在這一點...):PHP File Exists返回false;當文件不實際存在
基本的文件創建:
$date = date("y-m-d");
$category = "tech";
$url_m = $_SERVER['DOCUMENT_ROOT']. "/" . $category . "/articles/" . $date . "/images/";
if(!file_exists($url_m)){
mkdir($url_m, 0777, true);
$segment = "/" . $category . "/articles/" . $date . "/";
}
else{
for($i = 1; $i <= 100; $i++){
$url_new = $_SERVER['DOCUMENT_ROOT']. "/" . $category . "/articles/" . $date . "-" . $i . "/images/";
$segment = "/" . $category . "/articles/" . $date . "-" . $i . "/";
if(!file_exists($url_new)){
echo $segment;
$url_m = $url_new;
mkdir($url_m, 0777, true);
echo $url_m;
break;
}
else{
continue;
}
}
}
$name = $_FILES['file']['name'];
$url = "http://" . $_SERVER['SERVER_NAME'] . $segment;
$upload_url = str_replace("/images/", "", $url_m);
$pic_url = uploadFile($segment, $name, $_FILES['file']['type']);
Now the UPLOAD script
function uploadFile($dir, $name, $type){
if(isset($_POST['submit'])){
$allowed = array('gif','png','jpg','JPG','jpeg');
$filename = $name;
$ext = pathinfo($filename, PATHINFO_EXTENSION);
if(!in_array($ext,$allowed)) {
echo 'error';
}
$tmp_name = $_FILES['file']['tmp_name'];
$error = $_FILES['file']['error'];
if (isset ($name)) {
if (!empty($name)) {
$location ="<br>". $_SERVER['DOCUMENT_ROOT'].$dir. "images/";
echo $location. "<br>";
if(!file_exists($location)){echo "no";}
if (!is_writeable($location.$name)) {
die("Cannot write to destination file");
}
if (move_uploaded_file($tmp_name, $location.$name)){
$path = "http://" . $_SERVER['SERVER_NAME'] . $dir ."/images/". $name;
return $path;
}
else { return "WRONG";}
} else {
echo 'please choose a file';
}
}
}
else{return 0;}
}
像往常一樣,請知道我不會問這個問題,如果我還沒有先掙扎好幾個小時,並且非常感謝所有幫助!
編輯: 這裏是表單的HTML代碼:
<form method="post" action="../site-resources/db-config/db-post.php" enctype= "multipart/form-data">
News Category:
<select name = "category" id = "category" required>
...
</select><br>
Subcategory:
<select name = "subcategory" required >
...
</select>
<br>
Article Title: <input type="text" name="title" placeholder="Article Title" required /><br>
Article Subtitle: <input type="text" name="subtitle" placeholder="Article Subtitle" required /><br>
Author's Name: <input type="text" name="author_name" placeholder="Author Name" required /><br>
Upload Image for Article: <input type = "file" name = "file" id = "image"><br>
Main Text:<br><textarea cols = "100" rows = "30" name = "article_body">Article Body Goes Here</textarea>
<br><br>
<h4>Security Question: Please Enter your Editor Code to post to this website</h4>
<input type = "text" name = "security_question" placeholder="Security Question" required />
<input type="submit" name = "submit" value="Post Article" />
如果'file_exists()'表示文件不存在,那麼它幾乎肯定不存在。從操作系統獲取完整路徑,在腳本中打印出路徑,並將它們的字符進行比較。 –
@ SverriM.Olsen是的,我意識到這一點。我比較了兩個字符串,它們完全相同。一個來自我的資源管理器,一個來自PHP。這真的很怪異...... –
還應該確保文件權限不會混亂。最近有PHP創建一個具有000權限的文件。 –