PHP多文件上传不致命错误最大执行超时

时间:2016-09-20 13:19:01

标签: php file-upload

我正在尝试将多个图片上传到多个目录。我在这里真正做的是表单允许用户上传两个图像,它会将具有相同名称的图像重命名为两个目录并将其记录到数据库中。但我的下面的代码在处理时没有显示任何错误,180秒后我在第11行(while (file_exists($target_full)) {)超过最大执行时间时收到错误。图像是100%拍摄但在服务器上找不到,也没有在数据库中记录。

我是以最简单的方式做到的。我认为问题出现在重命名文件中的图像存在。有人可以帮我解决这个问题吗?

function.php

<?php

$target_full = "/test/av-full/";
$target_tp   = "/test/av-tp/";
$tmp         = explode(".", $_FILES["photo_full"]["name"]);
$photo_full  = time() . '_' . rand(100, 999) . '.' . end($tmp);
$photo_tp    = "$photo_full";
$target_full = ($_SERVER['DOCUMENT_ROOT'] . "/test/av-full/");
$target_tp   = ($_SERVER['DOCUMENT_ROOT'] . "/test/av-tp/");

while (file_exists($target_full)) {
    $tmp         = explode(".", $_FILES["photo_full"]["name"]);
    $photo_full  = time() . '_' . rand(100, 999) . '.' . end($tmp);
    $target_full = ($_SERVER['DOCUMENT_ROOT'] . "/test/av-full/");
}
while (file_exists($target_tp)) {
    $photo_tp  = "$photo_full";
    $target_tp = ($_SERVER['DOCUMENT_ROOT'] . "/test/av-tp/");
}

$con = mysqli_connect("localhost", "username", "password") or die("MySQL Login problem!");
mysqli_select_db($con, "database") or die("Could not connect to database!");


if (move_uploaded_file($_FILES['photo_full']['tmp_name'], $target_full)) {
    if (move_uploaded_file($_FILES['photo_tp']['tmp_name'], $target_tp)) {

        mysqli_query($con, "INSERT INTO table (photo_full,photo_tp)
            VALUES ('$photo_full','$photo_tp')");

    } else {
        echo "photo 340px error.";
    }
    echo "The file <b>" . basename($_FILES['photo_full']['name']) . "</b> & <b>" . basename($_FILES['photo_tp']['name']) . "</b> has been uploaded, and renamed to <b>$photo_tp</b>. This is for your information.";
} else {
    echo "error occured";
}
?> 

的index.php

<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>
    <form action="function.php" enctype="multipart/form-data" method="post">
        <label>upload image to database</label><br>
        <br>
        <label>full size image :</label> <input name="size" type="hidden" value="350000"> <input name="photo[]" type="file"><br>
        <br>
        <label>340px size image :</label> <input name="size" type="hidden" value="350000"> <input name="photo[]" type="file"><br>
        <br>
        <br>
        <input name="upload" title="Add data to the Database" type="submit" value="Add Member">
    </form>
</body>
</html>

0 个答案:

没有答案