最奇怪的事情发生在我身上,我正在开发一个简单的文件上传脚本,它有效,但是它停止了工作? 所以我解决了问题并将问题隔离到服务器不接受特定文件夹实际存在的事实。代码的基本要点如下所示(请原谅代码的混乱,这只是一个粗略的草案......):
基本文件创建:
$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" />
答案 0 :(得分:0)
不知道这是否是实际问题。所以需要查看HTML表单,通常最多忘记编写enctype。
将<form id="someform" action="" enctype="multipart/form-data">
添加到表单以启用文件上传
{{1}}
答案 1 :(得分:0)
这个有效!测试
$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=isset($_FILES['file']['name'])?$_FILES['file']['name']:"";
$type=isset($_FILES['file']['type'])?$_FILES['file']['type']:"";
$url = "http://" . $_SERVER['SERVER_NAME'] . $segment;
$upload_url = str_replace("/images/", "", $url_m);
$pic_url = uploadFile($segment, $name, $type);
//Now the UPLOAD script
function uploadFile($dir, $name, $type){
if(isset($_POST['submit'])){
$allowed = array('gif','png','jpg','jpeg');
$filename = $name;
$ext = strtolower(pathinfo($filename, PATHINFO_EXTENSION));
echo $ext;
if(!in_array($ext,$allowed) ) {
echo 'error';
}
$tmp_name = $_FILES['file']['tmp_name'];
$error = $_FILES['file']['error'];
if (isset ($name)) {
if (!empty($name)) {
$location =$_SERVER['DOCUMENT_ROOT'].$dir. "images/";
echo "location:".$location. "<br>";
if(file_exists($location.$name))
{
echo "file exists";
if(!is_writeable($location.$name)) {
die("Cannot write to destination file. its not writable");
}
}
else
{
echo "file doesn't exist";
}
if (move_uploaded_file($tmp_name, $location.$name)){
$path = "http://" . $_SERVER['SERVER_NAME'] . $dir ."/images/". $name;
echo "file moved";
return $path;
}
else { return "WRONG";}
} else {
echo 'please choose a file';
}
}
}
else{return 0;}
}