2

我编写了一个代码来上传和解压缩图像的 zip 文件到文件夹。这个文件是upload2.php,还有一个upload1.php,我用来输入文件夹名。

我正在尝试添加一个函数,其中脚本还将在解压缩文件并将它们保存到目标文件夹后,将它们转换为缩略图,然后还将这些缩略图保存到另一个文件夹中。

该脚本还将有关所有单独文件的各种数据插入到 mysql 数据库中。

这是代码:

<?php // actual code for upload
$dirname = trim($_POST['dirname']);
$taken = trim($_POST['taken']);
$location = trim($_POST['location']);
$subject = trim($_POST['subject']);
$rooturl = "http://example.com/test";
$dateurl = $dirname.'/';
$mainurl = $rooturl.$dateurl;

require_once 'connect.php'; 
$sqlselect = "SELECT * from learning2 WHERE location='test2';";
$result = mysql_query($sqlselect) or die(mysql_error());
$thumbwidth = 100;
$thumbheight = 100;

function makeThumbnail($sourcefile, $endfile, $thumbwidth, $thumbheight, $quality) {
    ini_set( "memory_limit","192M");
// Takes the sourcefile (path/to/image.jpg) and makes a thumbnail from it
// and places it at endfile (path/to/thumb.jpg).
// Load image and get image size.
    if (file_exists($sourcefile)) {

        $img = imagecreatefromjpeg($sourcefile);
        $width = imagesx( $img );
        $height = imagesy( $img );

            if ($width > $height) {

            $newwidth = $thumbwidth;
            $divisor = $width / $thumbwidth;
            $newheight = floor( $height / $divisor);
            } else {

            $newheight = $thumbheight;
            $divisor = $height / $thumbheight;
            $newwidth = floor( $width / $divisor );
            }

        // Create a new temporary image.
        $tmpimg = imagecreatetruecolor( $newwidth, $newheight );

        // Copy and resize old image into new image.
        imagecopyresampled( $tmpimg, $img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height );

        // Save thumbnail into a file.
        imagejpeg( $tmpimg, $endfile, $quality);

        // release the memory
        imagedestroy($tmpimg);
        imagedestroy($img);
    } else {
    echo "The file " . $sourcefile . " does not exist";
    }  
}
function makeDirectory($dirname) { //This function makes both the directory the photos will be unzipped into, and a directory nested within that for the thumbnails of those photos. 
    mkdir($dirname, 0777);
    mkdir($dirname.'/thumbs', 0777);
    } 

if(isset($_POST['submit'])) {

if (file_exists($dirname) && is_dir($dirname)) { // determines whether or not this particular directory exists
    echo "the directory exists and it is called: " . $mainurl;    
    echo "<br />";     
} else {
    makeDirectory($dirname); 
}

if($_FILES["zip_file"]["name"]) { // pull the name of the zip file from the upload
    $filename = $_FILES["zip_file"]["name"];
    $source = $_FILES["zip_file"]["tmp_name"];
    $type = $_FILES["zip_file"]["type"];
    $name = explode(".", $filename); //format the filename for a variable
    $accepted_types = array('application/zip', 'application/x-zip-compressed', 'multipart/x-zip', 'application/x-compressed');
    foreach($accepted_types as $mime_type) {
        if($mime_type == $type) {
            $okay = true;
                break;
            }
        }
        $continue = strtolower($name[1]) == 'zip' ? true : false; // let user know if the zip file has not been uploaded
        if(!$continue) {
            $message = "The file you are trying to upload is not a .zip file. Please try again.";
        }
        $target_path = $dirname."/".$name; // get the $target_path variable to for the move_uploaded_file() function.
        if(move_uploaded_file($source, $target_path)) { // this block extracts the zip files and moves them to the $dirname directory

            $zip = new ZipArchive();
            $x = $zip->open($target_path);
            if ($x === true) {
                $zip->extractTo($dirname."/");
                $images = array();
                for ($i=0; $i<$zip->numFiles; $i++) {
                    $images[] = $zip->getNameIndex($i);
                }

                $zip->close();
                unlink($target_path);
            }
        $message = "Your .zip file was uploaded and unpacked.";
}
    } else {       
    $message = "There was a problem with the upload. Please try again.";
}
$newdir = scandir($dirname);
    foreach ($newdir as $key => $value) {
        if ($value!='.' && $value!='..') {
            $thumburl = $rooturl.$dateurl.'thumbs/'.$value;
                echo 'Key: ' . "$key;" . ' Value: ' . "$value" ."<br />\n";
                    $sourcefile = $value;
                    $endfile = 'http://example.com/test/'.$dirname.'/thumbs/'.'$value';
                makeThumbnail($sourcefile, $endfile, $thumbwidth, $thumbheight, $quality);
                mysql_query("INSERT INTO learning3 (taken, location, subject, rooturl, dateurl, imagename, thumburl) VALUES ('$taken', '$location', '$subject', '$rooturl', '$dateurl', '$value', '$thumburl')");
                    echo "<br />";
                    echo '<img src="'.$thumburl.'>< /img>';
                    echo "$value"." inserted successfully!";
                } else {
                    echo $value." not inserted, thumbnail not created.";
                    echo $insert_sql . '<BR>' . mysql_error();
                }
            }
        } else { echo 'Please input your data and select a zip file.';
    }
echo '<html>';    
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR...nsitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
</head>

<body>
<?php
if($message) echo "<p>$message</p>";
if($taken) echo "<p>pictures taken on: " . $taken . "</br>";
if($subject) echo "<p>subject: " . $subject . "</br>";
if($location) echo "<p>location: " . $location . "</br>";
if(($rooturl) && ($dateurl)) echo "<p>directory is called: " . $rooturl.$dateurl. "</br>";
?>
<form enctype="multipart/form-data" method="post" action="upload2.php">
         <label for="dirname">Directory to use?: </label> <input name="dirname" size="20" type="text" value="<?php echo $dirname; ?>" /><br />
<label for="taken">When was this taken?:</label> <input name="taken" size="20" type="text" value="<?php echo $dirname; ?>" /><br />
<label for="location">Where was this taken?</label> <input name="location" size="20" type="text" /><br />
<label for="subject">subject?</label> <input name="subject" size="20" type="text" /><br />
<!--< />-->
<input type=hidden name="mainurl" value="<?php echo "http://example.com/test/".'$dirname;' ?>" />
<label>Choose a zip file to upload: <input type="file" name="zip_file" /></label>
<br />
<input type=submit name='submit' value="Upload" /> 
</form>
</body>
</html>

关于脚本我无法弄清楚的是:它不会创建缩略图并将它们放入新的缩略图文件夹中,尽管它确实创建了正确的文件夹,并且 mysql 插入成功。我没有保存缩略图,而是收到简单的错误消息“文件不存在”。问题是,我知道该文件确实存在,因为脚本的早期部分创建了它。谁能告诉我我在这里做错了什么,或者甚至给我提示我应该在哪里寻找?

4

1 回答 1

1

所以,我makeThumbnail()直接测试了这个函数(都用当前目录中的文件,然后用它之外的文件),在这两种情况下,它都工作得很好。如果不能完全执行代码,很难确切地知道发生了什么,但我的猜测是它存在于对makeThumbnail(). 您是否有可能忘记$sourcefile在拨打电话之前添加路径?开头或结尾是否可能有空格$sourcefile?该函数有效,因此它必须是负责的调用代码。

只是略读那段代码,调用不应该是:makeThumbnail($dirname.$sourcefile[...])而不是makeThumbnail($sourcefile[...])(或"$dirname/$sourcefile",但你明白了)吗?

于 2013-07-24T17:43:26.537 回答