我只是想创建一个简单的图片上传脚本。
这是我使用的,但似乎有一些问题并显示错误
<?php
define("FILEREPOSITORY", "./");
if(isset($_POST['submit'])){
$user =$_POST['user'];
$ext_boo = FALSE;
$size_boo = FALSE;
$user_boo = FALSE;
if(strlen($user)<=0){
echo "No Username";
$user_boo = FALSE;
}
if($_FILES['picture']['size'] <=1024000){
$size_boo = TRUE;
}
else{
echo "Too large";
$size_boo = FALSE;
}
if(is_uploaded_file($_FILES['picture']['tmp_name'])){
//mime type
switch($_FILES['picture']['type']){
case "image/jpeg":
$extension = ".jpeg";
$ext_boo = TRUE;
break;
case "image/gif":
$extension = ".gif";
$ext_boo = TRUE;
break;
case "image/png":
$extension = ".png";
$ext_boo = TRUE;
break;
}
if($ext_boo && $size_boo && $user_boo){
$result = move_uploaded_file($_FILES['picture']['tmp_name'], FILEREPOSITORY."/images/".$user."".$extension."");
if($result)
echo "Uploaded";
else
echo "Some problems";
}
echo "Wrong file type";
}
}
else{
echo "<table>
<form enctype=\"multipart/form-data\" action=\"\" method=\"post\">
<tr>
<td>User:</td>
<td><input type=\"test\" name=\"user\" /></td>
</tr>
<tr>
<td>File:</td>
<td><input type=\"file\" name=\"picture\" /></td>
</tr>
<input type=\"submit\" name=\"submit\" value=\"upload\" />
</form>
</table>";
}
?>
有人可以帮我找出问题吗?
尝试var_dump($_FILES);
时错误:
array (size=1)
'picture' =>
array (size=5)
'name' => string 'sample.png' (length=10)
'type' => string 'image/png' (length=9)
'tmp_name' => string 'C:\wamp\tmp\php74BB.tmp' (length=23)
'error' => int 0
'size' => int 7575
Wrong file type
答案 0 :(得分:1)
您永远不会将$user_boo
设置为TRUE。我把它改为TRUE
开始(假设它是真的,直到你后来检查它是否在第10行是假的。)
同样在第37行,我更改了文件路径。您的上传位置额外增加了/
。以下代码适用于我的机器。确保将www用户设置为您尝试上传到的任何位置的所有者,并且它具有写入权限。
<?php
define("FILEREPOSITORY", "./");
if(isset($_POST['submit'])){
$user =$_POST['user'];
$ext_boo = FALSE;
$size_boo = FALSE;
$user_boo = TRUE;
if(strlen($user)<=0){
echo "No Username";
$user_boo = FALSE;
}
if($_FILES['picture']['size'] <=1024000){
$size_boo = TRUE;
}
else{
echo "Too large";
$size_boo = FALSE;
}
if(is_uploaded_file($_FILES['picture']['tmp_name'])){
//mime type
switch($_FILES['picture']['type']){
case "image/jpeg":
$extension = ".jpeg";
$ext_boo = TRUE;
break;
case "image/gif":
$extension = ".gif";
$ext_boo = TRUE;
break;
case "image/png":
$extension = ".png";
$ext_boo = TRUE;
break;
}
var_dump($ext_boo,$size_boo,$user_boo);
if($ext_boo && $size_boo && $user_boo){
$result = move_uploaded_file($_FILES['picture']['tmp_name'], FILEREPOSITORY."images/".$user."".$extension."");
if($result)
echo "Uploaded";
else
echo "Some problems";
}
echo "Wrong file type";
}
}
else{
echo "<table>
<form enctype=\"multipart/form-data\" action=\"\" method=\"post\">
<tr>
<td>User:</td>
<td><input type=\"test\" name=\"user\" /></td>
</tr>
<tr>
<td>File:</td>
<td><input type=\"file\" name=\"picture\" /></td>
</tr>
<input type=\"submit\" name=\"submit\" value=\"upload\" />
</form>
</table>";
}
?>