使用php捕获数据和图像,将数据保存在基于文件名动态命名的新目录中

时间:2014-09-28 17:45:01

标签: php filesystems

我对PHP有点缺乏经验,所以我很感激我能得到的任何帮助。我期待:

捕获用户的名字,姓氏和图片。在将这些数据放入表单后,它将被发送到php处理器,该处理器使用用户姓氏作为变量,将其设置为包含data.txt和avatar.jpg ||的新目录。 .png || .bmp || .gif(应该包含输入到Web表单中的所有信息)。安全性不是问题,也不是验证,因为我已经涵盖了这一部分。我只需要知道如何处理这个问题。这是一些示例代码,目前让我无处可去:

的index.html:

<form enctype="multipart/form-data" action="process.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="512000" />
<input type="text" name="fname">
Send this file: <input name="userfile" type="file" />
<input type="submit" value="Send File" />
</form>

process.php

<?php

    $uploaddir = '/home/nicholasabrams/public_html/' + $_POST['lname'] + '/'
                +  $_FILES['userfile']['name'];
    $uploadfile = $uploaddir . basename($_FILES['userfile']['name']);

    echo "<p>";


    if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
      echo "File is valid, and was successfully uploaded.\n";
    } else {
       echo "Upload failed";
    }

    echo "</p>";
    echo '<pre>';
    echo 'Here is some more debugging info:';
    print_r($_FILES);
    print "</pre>";

1 个答案:

答案 0 :(得分:0)

确保姓氏是唯一的,你可以通过多种方式做到这一点,所以我会在这里留下你如何做到这一点

<?php
//process.php
$uploaddir = '/home/nicholasabrams/public_html/' + $_POST['lname'].'/';
//TODO get the extension of a file from $_FILES['userfile']['name']
//$EXTENSION=.....
$EXTENSION = 'jpg';  
if( ! is_dir($uploaddir) ){
  mkdir($uploaddir);
}else{
// CONFLICT differen user same last name , or same user needs to change some data
}
 if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploaddir.'avatar.'.$EXTENSION ){
   $data = "Last Name:" . $_POST['lname'] ."\n";
   //$data .= ..... additional data that you need
   file_put_contents($uploaddir.'data.txt',$data);

}