0

我正在为我父亲制作一个网站,允许人们上传他们自己的个人照片,因为他是一名窗户清洁技术。他想让人们能够上传他们的图片,这样他就可以给他们一个成本估计。下面,我有两段代码。第一位成功上传图片。那我就不用担心了。但是,第二位没有成功上传图像。我想要完成的是每次用户上传一组图像(最多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 == ''),第二位代码在这里多了一个块来替换原来的“移动文件”命令

4

1 回答 1

0

我刚刚为我叔叔的公司做了类似的事情。要解决您不知道让该人在那里提交信息的好方法的问题,您可以创建一个表单,该表单获取信息,然后将电子邮件发送到您父亲的工作电子邮件。这是 php 邮件功能的示例。

$to = 'service@anywhere.com';
$subject = 'New Service Request:';
$time = date('l jS \of F Y h:i:s A');
$body = "New Service Request Submited - $time (time is coming from webserver)\nFirst   Name: ".$_POST['fname']."
Last Name:\n ".$_POST['lname']."
Title:\n ".$_POST['title']."
Business:\n ".$_POST['business']."
Phone:\n ".$_POST['phone']."
Email:\n ".$_POST['email']."
Address:\n ".$_POST['address']."
Preferred Method of Contact:\n ".$method."
Equipment Type:\n ".$_POST['type']."
Equipment Issue:\n ".$_POST['issue']."
Additional Comments:\n ".$_POST['comments'];
$headers = 'From: Request Generator <noreply@anything.com>';
mail($to, $subject, $body, $headers);

电子邮件地址可以是任何你想要的。唯一的要求是您在网站所在的位置运行邮件服务器(几乎所有托管公司都提供除了网络服务器之外的电子邮件服务器)。在标题部分,您会注意到 . 前面的文本。该文本显示为发送电子邮件的“人”的姓名,而 <> 之间的文本是它来自的“地址”,不必再是真实的。

在我的正文中,我留下了为我叔叔生成门票的文本,以提供一个示例,说明您可能希望或可能不希望您的门票看起来像什么。

对于您的上传错误,乍一看,您似乎从未创建过要上传内容的动态文件夹。尝试:

mkdir($uploadpath);

在创建或移动目录中的任何文件之前。

于 2013-07-16T00:49:09.307 回答