PHP-将多张照片上传到新创建的目录中 [英] PHP - Upload multiple photos into newly created Directory

查看:66
本文介绍了PHP-将多张照片上传到新创建的目录中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我几天来一直在寻找答案,如何将多张照片上传到数据库而不会使系统超载,并决定最好的方法是将多张照片上传到新创建的目录(通过php创建)中并存储而是数据库中的目录链接.我正在研究的是一种基本上可以创建新的唯一页面的表单.这个独特的页面具有一组独特的照片,因此,每次生成页面时我都需要生成一个文件夹,并将路径链接上传到数据库!我该怎么做??

I've been digging for an answer for days now in how to upload multiple photos into a database without overloading the system, and decided the best way is to upload multiple photos into a newly created directory (created via php) and store the directory link in the database instead. What I'm working on is a form that basically creates a new unique page. This unique page has a unique set of photos, and hence I need to generate a folder each time a page is generated, and upload the path link to the database! How do I do that???

这是我的HTML:

<form method="post" action="test.php" enctype="multipart/form-data">
  <p>File:</p>
  <input type="file" name="file[]" id="file" >
  <input type="submit" value="Upload">
</form>

这是我到目前为止的PHP(我希望它应该在正确的轨道上:/):

and here is my PHP so far (should be on the right track I hope :/):

<?php
  //Connect to DB
  $conn = mysql_connect ('localhost', 'root', 'root');
  if (!$conn){
    die("Could Not Connect to MySQL!");
  }
  if(!mysql_select_db("test")){
    die("Could Not Open Database:" . mysql_error());
  }
  echo "<p>Connected</p>";

  //Upload Files
  foreach ($_FILES['file']['name'] as $f => $name) {
    $allowedExts = array("gif", "jpeg", "jpg", "png");
    $temp = explode(".", $name);
    $extension = end($temp);

    if ((($_FILES["file"]["type"][$f] == "image/gif")
    || ($_FILES["file"]["type"][$f] == "image/jpeg")
    || ($_FILES["file"]["type"][$f] == "image/jpg")
    || ($_FILES["file"]["type"][$f] == "image/png"))
    && ($_FILES["file"]["size"][$f] < 2000000)
    && in_array($extension, $allowedExts))
    {
      if ($_FILES["file"]["error"][$f] > 0){
        echo "Return Code: " . $_FILES["file"]["error"][$f] . "<br>";
      } else {
        if (file_exists("uploads/" . $name)){
          echo "<p>File Already Exists</p>";
        } else {
          //create new directory folder within /uploads

          //move the files you upload into the new folder.
          move_uploaded_file($_FILES["file"]["tmp_name"][$f], "upload/" . uniqid() . "_" . $name);
          //send the file path to the database.
          mysql_query("INSERT INTO test (idtest,testing) VALUES (','{$filepath}'");
        }
      }
    } else {
      $error =  "Invalid file";
    }
  }
?>

对于那些好奇的人,这是我的数据库列:

and for those curious, here is my database collumns:

|| idtest (AI, INT) || testing (varchart(50)) ||

感谢任何帮助!一直在干我!预先谢谢你!

Any help is IMMENSELY appreciated! It's been doing me in! Thank you in advance!

推荐答案

您应该使用mkdir()在代码顶部添加创建目录的方式.这将仅被调用一次,一旦完成,您将设置如何将文件移动到目录中,如下所示:

You should add in at the top of your code a manner to which you make a directory, by using mkdir(). This will only be called once, and once it's done you set how you move the files into the directory as such:

<?php
//Create Subdirectory
  //Set the subdirectory name
  $subdir = $_POST['folderName'];

  //set the directory path name
  $dir = ("./uploads/" . $subdir);

  //make the directory
  (mkdir($dir, 0777);

//state your file type arguments
foreach ($_FILES['file']['name'] as $f => $name) {
 $allowedExts = array("gif", "jpeg", "jpg", "png");
 $temp = explode(".", $name);
 $extension = end($temp);
 //Set file type and size
  if ((($_FILES['file']['type'][$f] == "image/gif")
  || ($_FILES['file']['type'][$f] == "image/jpeg")
  || ($_FILES['file']['type'][$f] == "image/jpg")
  || ($_FILES['file']['type'][$f] == "image/png"))
  && ($_FILES['file']['size'][$f] < 1073741824)
  && in_array($extension, $allowedExts))
  {
   if ($_FILES['file']['error'][$f] > 0){
    echo "Return Code: " . $_FILES['file']['error'][$f] . "<br>";
   } else {
    //if the file exists within the directory
     if (file_exists($dir . $name)){
      echo "<p>File Already Exists</p>";
    } else {
      $names = $_FILES['file']['tmp_name'][$f];

      //move the files you upload into the newly generated folder.
      if (move_uploaded_file($names, "$dir/$name")){
        echo "<p>Moved</p>";
      } else {
        echo "<p>not moved</p>";
      }
      //send the file path to the database.

      echo "<meta http-equiv='refresh' content='2;url=test.php'>";
    }
   }
  } else {
   $error =  "Invalid file";
  }
 }
?> 

可重复使用的部分是您继续使用$ dir变量的地方.虽然您确实应该检查安全性,但这是您可以使用的基本方法.首先编写目录的代码,然后循环遍历文件.

The part that's reusable is where you keep using the $dir variable. You should really check for security though, but this is the basic method you can do it in. The code with first make the directory, then loop the files through it.

这篇关于PHP-将多张照片上传到新创建的目录中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆