我正在为我父亲制作一个网站,允许人们上传他们自己的个人照片,因为他是一名窗户清洁技术。他想让人们能够上传他们的图片,这样他就可以给他们一个成本估计。下面,我有两段代码。第一位成功上传图片。那我就不用担心了。但是,第二位没有成功上传图像。我想要完成的是每次用户上传一组图像(最多5个)时,它会在将当前时间设置为文件夹名称的目录中创建一个新文件夹,这样会更容易他跟踪谁发送了什么。我' 我仍然在研究如何仅根据图像识别一个人的一些细节(也许一个人会在发送图像后打电话并验证提交时间?)。我试图尽可能少地使用数据库来做到这一点,因为我不知道如何使用 MySQL 并将其绑定到 PHP。
这是可以正常工作并成功将图像上传到 /upload 的代码:
<?php
$uploadpath = 'upload/';
$max_size = 4000;
$allowtype = array('bmp', 'gif', 'jpg', 'jpe', 'png');
if(isset($_FILES['fileup']) && strlen($_FILES['fileup']['name']) > 1){
$uploadpath = $uploadpath . basename( $_FILES['fileup']['name']);
$sepext = explode('.', strtolower($_FILES['fileup']['name']));
$type = end($sepext);
list($width, $height) = getimagesize($_FILES['fileup']['tmp_name']);
$err = '';
if(!in_array($type, $allowtype)) $err .= 'The file: <b>'. $_FILES['fileup']['name']. '</b> does not have an allowed extension type.';
if($_FILES['fileup']['size'] > $max_size*1000) $err .= '<br/>Your file exceeds the '. $max_size. ' KB. size limit.';
if($err == ''){
if(move_uploaded_file($_FILES['fileup']['tmp_name'], $uploadpath)){
echo 'File: <b>'. basename( $_FILES['fileup']['name']). '</b> successfully uploaded:';
echo '<br/>File type: <b>'. $_FILES['fileup']['type'] .'</b>';
}
else echo '<b>Unable to upload the file.</b>';
}
else echo $err;
}
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST" enctype="multipart/form-data">
Upload File: <input type="file" name="fileup" /><br/>
Upload File: <input type="file" name="fileup" /><br/>
Upload File: <input type="file" name="fileup" /><br/>
Upload File: <input type="file" name="fileup" /><br/>
Upload File: <input type="file" name="fileup" /><br/>
<input type="submit" name='submit' value="Upload" />
</form>
这是我试图更改以创建一个以时间戳为名称的新文件夹的代码:
<?php
$uploadpath = 'upload/';
$max_size = 4000;
$allowtype = array('bmp', 'gif', 'jpg', 'jpe', 'png');
if(isset($_FILES['fileup']) && strlen($_FILES['fileup']['name']) > 1){
$uploadpath = $uploadpath . basename( $_FILES['fileup']['name']);
$sepext = explode('.', strtolower($_FILES['fileup']['name']));
$type = end($sepext);
list($width, $height) = getimagesize($_FILES['fileup']['tmp_name']);
$err = '';
if(!in_array($type, $allowtype)) $err .= 'The file: <b>'. $_FILES['fileup']['name']. '</b> does not have an allowed extension type.';
if($_FILES['fileup']['size'] > $max_size*1000) $err .= '<br/>Your file exceeds the '. $max_size. ' KB. size limit.';
if($err == ''){
$uploadpath = "upload/" . time();
$count = 0;
foreach ($_FILES['fileup']['name'] as $filename){
$temp = $_FILES['fileup']['tmp_name'][$count];
move_uploaded_file($temp, $uploadpath . '/' . $filename);
$count++;
}
if(move_uploaded_file($_FILES['fileup']['tmp_name'], $uploadpath)){
echo 'File: <b>'. basename( $_FILES['fileup']['name']). '</b> successfully uploaded:';
echo '<br/>File type: <b>'. $_FILES['fileup']['type'] .'</b>';
}
else echo '<b>Unable to upload the file.</b>';
}
else echo $err;
}
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST" enctype="multipart/form-data">
Upload File: <input type="file" name="fileup" /><br/>
Upload File: <input type="file" name="fileup" /><br/>
Upload File: <input type="file" name="fileup" /><br/>
Upload File: <input type="file" name="fileup" /><br/>
Upload File: <input type="file" name="fileup" /><br/>
<input type="submit" name='submit' value="Upload" />
</form>
两者的主要区别在于if($err == '')
,第二位代码在这里多了一个块来替换原来的“移动文件”命令