PHP在多个文件上传上创建缩略图并将缩略图和图像保存在两个不同的目录中

时间:2018-09-01 07:11:12

标签: php image thumbnails

我正在尝试将多个图像上传到我网站上的文件夹中。虽然文件上传没有问题,但我试图集成为同一功能不起作用的所有图像上传创建缩略图。

HTML表单:

<form class="navbar-form pull-left"  action="fileupload.php" name="fileupload" method="post" enctype="multipart/form-data">
        <label for="file" style="cursor: pointer;padding:5px 0 0 0;font-weight:normal" title="Add Images">IMAGES</label>
        <input type="file" name="file[]" multiple id="file"><br>
      </form>

Javascript:

function _id(e) {return document.getElementById(e)}    
_id('file').onchange = function() {
  var theFile = this.files;
  if(theFile.length === 1) {
    var uploader = new XMLHttpRequest();
    var file = new FormData();
    file.append("file", theFile[0]);
    uploader.onreadystatechange = function() {
      if(uploader.readyState === 4 && uploader.status === 200) {
        show_message('Uploading Image');
        console.log(uploader.responseText);

      }
    }
    uploader.open('POST','fileupload.php',true);
    uploader.send(file);
  } else {
    var start = 0,
    setter,
    buff = true;
    setter = setInterval(function() {
      if (buff === true) {
        show_message('Uploading Images');
        buff = false;
        var uploader = new XMLHttpRequest();
        var file = new FormData();
        file.append('file', theFile[start]);
        uploader.onreadystatechange = function() {
          if(uploader.readyState === 4 &&  uploader.status === 200) {
            console.log(uploader.responseText);
            start++;
            buff = true;
          }
        }
        uploader.open('POST','fileupload.php',true);
        uploader.send(file);
      }
      if(start >= theFile.length) {
        clearInterval(setter);
      }
    }, 200)
  }
}

fileupload.php

<?php
session_start();
$database = $_SESSION['folder'];
$match = $_SESSION['matchLst'];
$tardir = "/var/www/html/projects/" . $database . "/" . $match . "/";
$thumb = $match . "_thumb/";
$thumbdir = $tardir . $thumb;

function createThumbnail($filename) {
    if($filename) {
        $im = imagecreatefromjpeg($tardir . $filename);
    }
    $x = imagesx($im);
    $y = imagesy($im);

    $nx = 100;
    $ny = 100;

    $nm = imagecreatetruecolor($nx, $ny);
    imagecopyresized($nm, $im, 0, 0, 0, 0, $nx, $ny, $x, $y);
    if(!file_exists($thumbdir)) {
        if(mkdir($thumbdir)) {
            imagejpeg($nm, $thumbdir . $filename);
        } else {
            die("Thumbnail directory could not be created");
        }
    } else {
            imagejpeg($nm, $thumbdir . $filename);
    }
}


if(!isset($_FILES['file'])){die();}

if(isset($_FILES['file'])) {
    if($_FILES['file']['name']) {
        $filename = $_FILES['file']['name'];
        $source = $_FILES['file']['tmp_name'];
        $target = $tardir . $filename;
        move_uploaded_file($source, $target);
        createThumbnail($filename);  
        echo "Images uploaded";
    }
}
?>

我面临2-3个问题:

在fileupload.php文件中,如果我注释掉createThumbnail($filename),那么我可以上传多个文件而不会出现问题。当我包含createThumbnail函数时,文件上传和缩略图创建都不会发生。我尝试了此处和git给出的各种示例,但是无法创建缩略图,也无法将其保存到文件夹中。

1 个答案:

答案 0 :(得分:0)

在此处未设置

$ tardir

function createThumbnail($filename) {
    if($filename) {
        $im = imagecreatefromjpeg($tardir . $filename);
    }

您应该替换函数调用:

createThumbnail($filename);  

通过:

createThumbnail($tardir . $filename);  

并且不要在函数中使用$ tardir。

设置绝对路径时要小心:

$tardir = "/var/www/html/projects/" . $database . "/" . $match . "/";

如果路径更改,您的脚本将被破坏。相反,您可以使用magic-constants like DIR和/或realpath()