简体   繁体   English

如何在每个会话php中将图像上传到新的唯一文件夹

[英]How to upload images to a new unique folder every session php

I'm in the process of making a website for my dad that allows people to upload their own personal images because he is a window cleaning tech. 我正在为我的父亲创建一个网站,该网站允许人们上传自己的个人图像,因为他是窗户清洁技术。 He wants to allow people to be able to upload their images so he can give them an estimate on the cost. 他想让人们能够上传他们的图像,以便可以给他们估算成本。 Below, I have two bits of code. 下面,我有两位代码。 The first bit successfully uploads images. 第一位成功上传图像。 That I won't have to worry about. 我不用担心。 The second bit, however, does not successfully upload images. 但是,第二位不能成功上传图像。 What I want to accomplish is that every time a user uploads a set of images (maximum of 5), it will make a new folder in a directory that sets the current time as the name of the folder, that way it will be easier for him to keep track of who sends what. 我要实现的是,每当用户上传一组图像(最多5张)时,它将在目录中创建一个新文件夹,该文件夹将当前时间设置为该文件夹的名称,这样一来,他跟踪谁发送了什么。 I'm still working out little details on how he can identify a person solely based on images (perhaps a person will call after sending the images in and verify the time of submission?). 关于他如何仅根据图像识别一个人的信息,我仍在制定一些细节(也许一个人在发送图像后会打电话并确认提交时间?)。 I am trying to do this with as little use for a database as possible, being I don't know how to use MySQL and tie it to PHP. 我正在尝试使用尽可能少的数据库来做到这一点,因为我不知道如何使用MySQL并将其绑定到PHP。

This is the code that works and successfully uploads the images to /upload: 这是可以成功将图片成功上传到/ 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>

And here's the code I'm trying to change to make a new folder with the timestamp as the name: 这是我要更改的代码,以创建一个以时间戳为名称的新文件夹:

<?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>

the main difference between the two is after the if($err == '') , the second bit of code has one more block here to replace the original "move file" command 两者之间的主要区别是在if($err == '') ,代码的第二位在此处还有一个代码块,用以替换原始的“移动文件”命令

I just did something similar for my uncle's company. 我刚刚为叔叔的公司做了类似的事情。 To solve your issue of not knowing a good way to have the person submit there info you can create a form that takes info and then sends an email to your dad's work email. 为了解决您不知道让该人在此提交信息的好方法的问题,您可以创建一个接收信息的表格,然后将电子邮件发送到您父亲的工作电子邮件中。 Here's an example of the php mail function. 这是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);

Email address can be anything you want. 电子邮件地址可以是您想要的任何内容。 Only requirement is that you have a mail server running where your website is (pretty much all hosting companies provide an email server in addition to the webserver). 唯一的要求是您要在网站所在的位置运行邮件服务器(几乎所有托管公司除了提供Web服务器外,还提供电子邮件服务器)。 in the header section, you will notice the text in front of . 在标题部分,您会注意到前面的文本。 That text shows up as the name of the 'person' who sent the email and the text between the <> is the 'address' it comes from which again doesn't have to be real. 该文本显示为发送电子邮件的“人”的名字,而<>之间的文本就是它的“地址”,也不必是真实的。

In my body I left the text that generates tickets for my uncle to provide an example of what you may or may not want your tickets to look like. 在我的身体上,我留下了为叔叔生成门票的文字,以举例说明您可能希望或不希望自己的门票看起来像什么。

For your upload error at first glance it looked like you never created the dynamic folder the content is being uploaded into. 乍一看,您的上传错误似乎从未创建过要向其中上传内容的动态文件夹。 Try: 尝试:

mkdir($uploadpath);

before you create or move any files in the directory. 在目录中创建或移动任何文件之前。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM