我使用各种编程语言编程多年。我也有一些标记和脚本语言的经验。我是PHP的新手。我正在努力为他修复我爸爸的网站,我正在学习很多东西。我已经解决了很多问题,但目前卡住了。我父亲从5.1到5.4使用的PHP主机升级打破了网站。我注意到其中一个变化是现在需要定义变量。我修复了很多功能但是我无法修复允许网站用户将图片上传到服务器的部分。该文件没有上传,我不知道为什么。 这是PHP:
$file = $_POST['file'];
$file_name = $_POST['file']['name'];
if ($file && $file_name)
{
if ($_FILES['file']['size']>$maxsize) $status = "Error: Picture size too large. Max file size is $maxsize bytes.<br>";
if (($_FILES['file']['type']!="image/gif") && ($_FILES['file'] ['type']!="image/jpeg") && ($_FILES['file']['type']!="image/jpg") && ($_FILES['file'] ['type']!="image/png")) $status .= "Error: Wrong file type. Must be JPG or GIF only.<br>";
$picext = substr($file_name,-3);
$picext = strtolower($picext);
if ((!isset($status)) && ($picext!="gif") && ($picext!="jpg") && ($picext!="png")) $status .= "Error: Wrong file type. Must be JPG or GIF or PNG only.<br>";
}
if (!isset($status)){
//die("write".$id);
$email = $_POST['email'];
$aim = $_POST['aim'];
$icq = $_POST['icq'];
$yahoo = $_POST['yahoo'];
$homepage = $_POST['homepage'];
$myip = $_POST['myip'];
if (!$myip) $myip = $ip;
$email2 = $_POST['email2'];
$password = $_POST['password'];
$title = $_POST['title'];
$download = $_POST['download'];
$approved = $_POST['approved'];
$allowdelete = $_POST['allowdelete'];
$author = $_POST['author'];
$facebook = $_POST['facebook'];
$piclink = $_POST['piclink'];
$domain = $_POST['domain'];
$option3 = $_POST['option3'];
$secret = $_POST['secret'];
$q= "insert into $table values('', '$email', '$aim', '$icq', '$yahoo', '$homepage', '0','0', '0', '0', '0', '0', '', now(),'$myip','$email2','$password','$title','$download','$approved','$allowdelete','$auth or','$facebook','$piclink','$domain','$option3','$secret')";
$result = mysql_query($q) or die("Failed: $sql - ".mysql_error());
$q = "select max(id) from $table";
$result = mysql_query($q);
$resrow = mysql_fetch_row($result);
$id = $resrow[0];
$picext = "png";
$q = "update $table set picfile = '".$id.".".$picext."' where id='$id'";
$result = mysql_query($q);
$file = $_POST['file'];
@copy($file, "../pics/".$id.".".$picext);
$picfile=$id.".".$picext;
$act = "update"; //set mode back to update
Header("Location: newadmin.php?id=$id");
}
这是表单html代码:
<td width="91"><div align="right"><font size="-1" face="Verdana, Arial, Helvetica, sans-serif">Upload
Picture:</font></div></td>
<td width="403"><font size="-1" face="Verdana, Arial, Helvetica, sans-serif">
<form name='file' method='post' enctype="multipart/form-data" action="newadmin.php">
<input name="file" type="file" size="50" maxlength="50">
</form>
</font></td>
<td width="200"> </td>
<td width="18"> </td>
</tr>
<tr>
<td width="91">
<div align="right"><font size="-1" face="Verdana, Arial, Helvetica, sans-serif">
<!-- update & create button -->
<button type='submit' name='submit' value='Submit'><img src='../images/formbutton.gif' width='40' height='40' /><?echo $tag;?>
非常感谢任何帮助。如果这有帮助,由于某种原因,在error_log $ picext中没有设置。但如果我把“$ picext =”png“;”该文件仍未上传。
答案 0 :(得分:2)
尝试使用,
$file = $_FILES['file']['name'];
move_uploaded_file($_FILES['file']['tmp_name'], "../pics/".$id.".".$picext);
而不是
$file = $_POST['file'];
答案 1 :(得分:0)
将前两行更改为:
$file = $_FILES['file'];
$file_name = $_FILES['file']['name'];
和这些行:
$file = $_POST['file'];
@copy($file, "../pics/".$id.".".$picext);
到:
$file = $_FILES['file'];
move_uploaded_file($file['tmp_name'],"../pics/".$id.".".$picext);